Question

In: Computer Science

Working on a c++ data structures assignment.   Linked List add node. I have the head case...

Working on a c++ data structures assignment.  

Linked List add node. I have the head case and the tail case working but the middle/general case I can not get to work for the life of me. I have included the header file and the data struct file below  

#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::addNode(int id, string data){

   bool success = false;

    Node *newNode = new Node;

    newNode -> data.id = id;

    newNode -> data.data = data;

    newNode -> next = NULL;

    newNode -> prev = NULL;

    Node *current;

    current = head;

    

     if(current == NULL && id > 0){                     

        head = newNode;                    

        success = true;

     }else if(newNode -> data.id <= current -> data.id && data != "" && id > 0 && id != current -> data.id){

        newNode -> next = current;

        newNode -> prev = NULL;

        head = newNode;

        success = true;

     }else {

         success = false;                 

     }

    

    while(current != NULL){

        if((current -> data.id < id) && data != "" && (current -> next == NULL)){            //tail case  

             current -> next = newNode;

             newNode -> prev = current;

             success = true;

         }else{

             current = current -> next;

         }  

    }

    while(current != NULL){

        if((current -> data.id < id) && (current -> prev -> data.id > id) && data != "" && (current -> next != NULL)){  //general or middle case

            cout << "test2" << std::endl;

            newNode -> next = current;  

            newNode -> prev -> next = newNode;

            current -> prev = newNode;

            success = true;

         }else{

             current = current -> next;

         }

    }

   return success;

}

Solutions

Expert Solution

#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::addNode(int id, string data){

   bool success = false;

    Node *newNode = new Node;

    newNode -> data.id = id;

    newNode -> data.data = data;

    newNode -> next = NULL;

    newNode -> prev = NULL;

    Node *current;

    current = head;

    

     if(current == NULL && id > 0){                     

        head = newNode;                    

        success = true;

     }else if(newNode -> data.id <= current -> data.id && data != "" && id > 0 && id != current -> data.id){

        newNode -> next = current;
        
        // Added to maintain the previous pointer of the current node
        current->prev = newNode;

        newNode -> prev = NULL;

        head = newNode;

        success = true;

     }else {

         success = false;                 

     }

    

    while(current != NULL){

        if((current -> data.id < id) && data != "" && (current -> next == NULL)){            //tail case  

             current -> next = newNode;

             newNode -> prev = current;

             success = true;

         }else{

             current = current -> next;

         }  

    }

    //Reset the current pointer
    current = head;
    
    if(data!="" && id>0)
    {
        while(current != NULL){
            
            //Check id is greater if yes then add the node to the next of current
            if(current -> data.id < id){  //general or middle case
    
                cout << "test2" << std::endl;
    
                //NewNode next is current next
                newNode-> next = current -> next;
            
                //NewNode Previous us current 
                newNode -> prev = current;
            
                //Current next is new node
                current -> next = newNode; 
                
                //If node added then making the flag as true
                success = true;
                
                //After adding the node ignores the iteration of the other nodes
                break;
    
             }else{
    
                 current = current -> next;
    
             }
        }
    }

   return success;

}

Updated the code for adding the nodes on middle of the link list and updated the code for adding the head node in order to maintain the previous pointer.


Related Solutions

Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You're given the pointer to the head node of a ordered linked list, an integer to...
You're given the pointer to the head node of a ordered linked list, an integer to add to the list. Write a function that inserts a number in the the list preserving its order. If the head pointer contains a null pointer that indicates an empty list. Function insertNode has the following parameters: head: a SinglyLinkedListNode pointer to the head of the list data: an integer value to insert as data in your new node Function prototype: SinglyLinkedListNode* insertNode(SinglyLinkedListNode* head,...
Python 3 Function which takes the head Node of a linked list and sorts the list...
Python 3 Function which takes the head Node of a linked list and sorts the list into non-descending order. PARAM: head_node The head of the linked list RETURNS: The node at the head of the sorted linked list. ''' def sort(head_node): #Code goes here ''' Test code goes here '' ' if __name__ == '__main__':
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
This is a C++ based question that involves Data Structures and Algorithms. Q. Application: Linked List...
This is a C++ based question that involves Data Structures and Algorithms. Q. Application: Linked List of Bus Transit and Passengers You are to implement a C++ program for City Bus Transit using linked list data structure to maintain record of passengers. Specifically, you are to implement the following methods/functions: For Passenger: o A function which can create a new node of the linked list using new for each newpassenger o A function that prints the time of single passenger...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_);...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_); shared_ptr<node> temp(new node); if(cursor == NULL) { temp = cursor-> next; cursor= temp; if (temp = NULL) { temp->next = NULL; } } else if (cursor-> next != NULL) { temp = cursor->next->next; cursor-> next = temp; if (temp != NULL) { temp->next = cursor; } } }
In C++, write a member method delete() that deletes a node from a linked list at...
In C++, write a member method delete() that deletes a node from a linked list at a random position. (It should first randomly generate that position. and then delete that node).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT