Question

c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....

c++ data structures linked list delete node

bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using

The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate the current Node (no need to detach it’s pointers). ● Return T/F based on successful deletion or not. Note: there are two special cases, delete the head, delete the tail. They are analogous to the add Node special cases. This is left up to the reader to discern

#ifndef LINKEDLIST_H

#define LINKEDLIST_H

#include "data.h"

#include <iostream>   //take this out

using std::cout;

class LinkedList{

    public:

        LinkedList();

        ~LinkedList();

        bool addNode(int, string);

        bool deleteNode(int);

        bool getNode(int, Data*);

        void printList(bool = false);

        int getCount();

        void clearList();

        bool exists(int);

    private:

        Node *head;

};


#endif

----------------------------------------------------------------------------

#ifndef DATA_H

#define DATA_H

#include "string"

using std::string;

struct Data {

    int id;

    string data;

};

struct Node {

    Data data;

    Node *next;

    Node *prev;

};

#endif /* DATA_H */

------------------------------------------------------

bool LinkedList::deleteNode(int id){

    bool success;

    Node *current = head;

    if(current == NULL){

        success = false;

    }

   while(current != NULL){

        if(current -> data.id == id){

            current =  current -> next;

            current = nullptr;

            delete current;

        }   

        success = true;

    }

    return success;

Homework Answers

Answer #1
#ifndef LINKEDLIST_H

#define LINKEDLIST_H

#include "data.h"
#ifndef DATA_H

#define DATA_H

#include "string"


#include <iostream>   //take this out

using std::cout;

class LinkedList{

    public:

        LinkedList();

        bool addNode(int, string);

        bool deleteNode(int);

        bool getNode(int, Data*);

        void printList(bool = false);

        int getCount();

        void clearList();

        bool exists(int);

    private:

        Node *head;

};


using std::string;

struct Data {

    int id;

    string data;

};

struct Node {

    Data data;

    Node *next;

    Node *prev;

};



bool LinkedList::deleteNode(int id){

    bool success;

    Node *current = head;
    Node *n=NULL;
    if(current == NULL){

        success = false;

    }

   while(current != NULL){

        if(current -> data.id == id){

            current->data = current->next->data;  
    
             n = current->next;  
  
        // Remove the link of next node  
        current->next = current->next->next;  
  
        // free memory  
        free(n);  

            success = true;
         }
      else{
        current=current->next;
        success=false;
     }

    }

    return success;
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is written TO DO List! #include<iostream> using namespace std; template<typename T> class DoubleList{​​​​​     class Node{​​​​​     public: T value; Node* next; Node* prev;         Node(T value = T(), Node* next = nullptr, Node* prev = nullptr){​​​​​             this->value = value;             this->next = next;             this->next = next;         }​​​​​     }​​​​​;     int size;     Node* head;     Node* tail; public:     DoubleList(){​​​​​         size = 0;         head = nullptr;     }​​​​​     int length(){​​​​​         return size;     }​​​​​...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
C++ See the provided specification files. Complete the implementation for each as a separate file. void...
C++ See the provided specification files. Complete the implementation for each as a separate file. void seen(std::string); If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects. std::string getNextWord(); Returns the next word of the list and sets the currentItem pointer...
Data Structures using C++ Searching a Linked List Here are the declarations for a simple unsorted...
Data Structures using C++ Searching a Linked List Here are the declarations for a simple unsorted linked list of ints that ends in a null pointer. //=============================================================== class Cell { friend class UList; private: int data; Cell* next; Cell( int dt, Cell* nx=nullptr ) : data(dt), next(nx) {} }; //=============================================================== class UList { private: Cell* head = nullptr;    // stationary head pointer. Cell* scan = nullptr;          // for walking down the List. Cell* follow = nullptr; public: void find( int...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
C LANGUAGE IMPLEMENTATION - Writing source files implement several functions, declared in student.h, transcript.h and internal.h...
C LANGUAGE IMPLEMENTATION - Writing source files implement several functions, declared in student.h, transcript.h and internal.h (with support from common.h). There are 3 rules: 1. There are three types of students: undergraduate, MEng and PhD. 2. Undergraduates must complete 40 courses, MEng students 5 and PhD students 2. 3. Undergraduate students require a mark of 50 to pass; graduate students need a 65. ----------------Common.h-------------------------------------------------------------------------------------------------------------------------------------------------------------- #ifndef COMMON_H #define COMMON_H /*Portable macros for declaring C functions visible to C++.*/ #ifdef __cplusplus #define...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT