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
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
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:...
- 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;       ...
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined...
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined on page 257. The function takes as input a pointer to the head of a linked list consisting of numbers. The function should remove the second item in the list. If the list had only one item, the function should delete that item. If the list was empty, then let the list remain empty. In all cases return the new head of the list...
(C++) Suppose you want to use a linked list where the items stored in the list...
(C++) Suppose you want to use a linked list where the items stored in the list are strings from the standard library string class, how would you change the node1.h header file? Header File: #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <cstdlib> // Provides size_t and NULL namespace main_savitch_5 { class node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR node(const value_type& init_data=value_type( ), node* init_link=NULL) { data_field = init_data; link_field = init_link; } // MODIFICATION MEMBER FUNCTIONS node* link( )...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
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...
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:...
Add a recursive Boolean function called bool isGreater(int compare) to class IntegerLinkedList to check if any...
Add a recursive Boolean function called bool isGreater(int compare) to class IntegerLinkedList to check if any of the linked list data values is greater than the given parameter, compare. For example, isGreater(25) should return true and isGreater(100) should return false for the the linked list shown below. A main function (prob3.cpp) is given to you to add data values to the linked list and test your function. Other examples are given in the main function. // ADD ANSWER TO THIS...
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()...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT