Question

In: Computer Science

I am trying to create my own doubly linkedlist class and here is what I have...

I am trying to create my own doubly linkedlist class and here is what I have so far. my addfirst is adding elements forever and ever, and I could use some help. This is in C++. Please help me complete my doubly linkedlist class. the addfirst method is what I would like to complete.

#ifndef DLLIST_H
#define DLLIST_H
#include "node.h"
#include
using namespace std;
template
class DLList
{
private:


    Node* head = nullptr;
    Node* tail = nullptr;
    T data;
    // private DLList stuff goes here
public:
    // public DLList stuff goes here
    DLList();
    DLList(const DLList&L);
    DLList& operator=(const DLList &L);
    ~DLList();
    void addFirst(T data);
    void addElement(int position, T data);
    void remove(int position);
    void printElements();
    void removeFirst();
    bool exists(T value);
};
template
DLList::DLList(){
    head = nullptr;
    tail = nullptr;
    this ->data = data;
}


template
DLList::DLList(const DLList&L){
     L.head = head;
     while(L.head != nullptr){
         L.data = head->data;
         head = head -> next;
     }
     L.tail = tail;
     L.data = data;
}

template
DLList&DLList::operator=(const DLList &L){
     L.head = head;
     while(L.head != nullptr){
         L.data = head->data;
         head = head -> next;
     }
     L.tail = tail;
     L.data = data;
}

template
DLList::~DLList(){

}


template
void DLList::printElements(){
      Node* curr = head;
      while(curr != nullptr){
          cout << curr->data;
      }

}
template
//create new node
//set appropriate data
//set next to current head
//set nodes prev to nullptr
//set past heads previous to node
void DLList::addFirst(T data){
     Node* node = new Node();
     node->setData(data);
     if(head == nullptr){
         head = node;
         head -> next = nullptr;
         head -> prev = nullptr;
     }
     else{
     node -> next = head;
     node -> prev = nullptr;
     head -> prev = node;
     }
}


template
//create node with data
//traverse to find the area to insert
//adjust the pointers
void DLList::addElement(int position,T data){
    Node* node = new Node();
    node->setData(data);
    Node* curr = new Node();
    curr -> head;
    for(int i = 0; i < position - 1; i++){
         curr = curr -> next;
    }
    node -> prev = curr -> prev;
    curr -> prev = node;
    node -> next = curr;
    node -> prev -> next = node;

}

template
void DLList::remove(int position){
     Node* curr = head;
     Node* next = curr;
     for(int i = 0; i < position - 1; i++){
         curr = curr -> next;
     }
     curr -> prev = nullptr;
     curr -> next = nullptr;
     curr -> prev -> next = curr -> next;
}

template
void DLList::removeFirst(){
   Node* curr = head;
   curr -> next -> prev = nullptr;
   curr -> next = nullptr;

}

template
bool DLList::exists(T value){
    Node* curr = head;
    while(curr != nullptr){
        if(curr->data == value){
            return true;
        }
       curr = curr-> next;
    }
    return false;
}


#endif // DLLLIST_H

//here is my node class for reference.

#ifndef NODE_H
#define NODE_H
using namespace std;
template
class Node{
private:
    T data;
    Node* next;
    Node* prev;
    template
    friend class DLList;
public:
    Node();
    Node(const Node& N);
    T& getData();
    void setData(T data);
    Node* getNext();
    Node* getPrev();
    /*Node():next(nullptr),prev(nullptr){}
    Node(T val):next(nullptr),prev(nullptr),data(val){}
    Node(const Node& rhs):next(nullptr),prev(nullptr),data(rhs.data){}
    ~Node();*/
};

template
Node::Node(){
    next = nullptr;
    prev = nullptr;
}

template
Node::Node(const Node& N){
    N.next = next;
    N.prev = prev;
    N.data = data;
}

template
T& Node ::getData(){
    return data;
}

template
void Node:: setData(T data){
    this -> data = data;
}

template
Node* Node::getNext(){
   return next;
}

template
Node* Node::getPrev(){
    return prev;
}

/*template
Node::Node(T data){
     this->data = data;
}

template
T Node::getData(){
    return this -> data;
}

template
Node::~Node(){

}*/


#endif // NODE_H

Solutions

Expert Solution

Assuming addFirst() adds an element to the beginning of your DLL, I found a few logical errors.

Firstly, you didn't update your tail while adding an element to your new list ( you only updated the head) which causes the tail to keep pointing to NULL. This may lead to issues elsewhere.

Secondly, whenever adding an element to an existing list, you have not updated the head.

Please see the updated snippet:

void DLList::addFirst(T data){
Node* node = new Node();
node->setData(data);
if(head == nullptr){
head = node;
head -> next = nullptr;
head -> prev = nullptr;

tail = node; //update tail if first element
//tail = x; //I mistyped x instead of node
}
else{
node -> next = head;
node -> prev = nullptr;
head -> prev = node;
head = node; //update head
}
}

Lastly, I found issues with your print function. You didn't change the value of curr, which was causing your infinite loop.

void DLList::printElements(){
Node* curr = head;
while(curr != nullptr){
cout << curr->data;
curr = curr->next; //traverse to the next node / nullptr
}

}

As you only requested help for the addFirst(), I have not tested the other methods.


Related Solutions

In trying to apply my knowledge in the real world, I am trying to create a...
In trying to apply my knowledge in the real world, I am trying to create a realistic retirement schedule. However, I am running into difficulties using both a financial calculator as well as our equations from class in doing this. I am trying to do the following: plan a retirement schedule between the ages of 25 and 70, in which I would deposit 20% of my income each year. The income starts at 80,000 with an annual growth rate of...
Professor, In trying to apply my knowledge in the real world, I am trying to create...
Professor, In trying to apply my knowledge in the real world, I am trying to create a realistic retirement schedule. However, I am running into difficulties using both a financial calculator as well as our equations from class in doing this. I am trying to do the following: plan a retirement schedule between the ages of 22 and 68, in which I would deposit 25% of my income each year. The income starts at 80,000 with an annual growth rate...
/* I been trying to solve this problem . this is my own coding /here is...
/* I been trying to solve this problem . this is my own coding /here is the HW INSTRUCTION Let the user to try of maximum 8 times to find the random number For every try display if the user find or not the number and the number of tries At the end of each game display to the user the number of tries to find the number Allow the user to play a maximum of 4 times, by asking...
I am working on a project where I have to create my own organization (stem cell...
I am working on a project where I have to create my own organization (stem cell research) I need to go further in depth about the program or organization in terms of describing the program implementation and the project evaluation...
I am trying to create a classified balance sheet and I am unsure what is involved...
I am trying to create a classified balance sheet and I am unsure what is involved when reporting the current assets, liabilities and owners equity?
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse();...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse(); //reverses the order of elements in the linked list void insert(int value); private: struct Node{ int data; Node* next; Node* prev; }; Node* head; Node* tail; //Add your helper function here that recursively reverses the order of elements in the linked list }; Write the declaration of a helper function in the class provided above that recursively reverses the order of elements in the...
1. I am trying to determine the level of measurement of my data type? I am...
1. I am trying to determine the level of measurement of my data type? I am looking for advice on Nominal, Ordinal, Interval, and Ratio 2. Does the data set have any categorical variables? I am trying to Describe the data set below in very general terms? This data consist of 8 variables: Which are GRE Scores, TOEFL Scores, University Rating, Statement of Purpose, Letter of Recommendation Strength, Undergraduate GPA, . Research Experience, and Chance of Admit. Name Type Description...
I am trying to start saving for retirement. I am investing all my cash into the...
I am trying to start saving for retirement. I am investing all my cash into the S&P 500, which will assume consistently 9.8% interest, compounded annually. I initially put a lump sum of $100 into my account, and I will deposit $10 every second week. a) After 10 years, how much money will I have invested? b) After 10 years, if I sold all of my stocks, how much money will I have in my account? c) After 25 years,...
I have the following code for my java class assignment but i am having an issue...
I have the following code for my java class assignment but i am having an issue with this error i keep getting. On the following lines: return new Circle(color, radius); return new Rectangle(color, length, width); I am getting the following error for each line: "non-static variable this cannot be referenced from a static context" Here is the code I have: /* * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT