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:...
- 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;       ...
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:...
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.”....
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
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;...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...