C++ HELP!! On what code to write for addToDLLHead and deleteFromDLLHead

I am trying to finish a addDLLto head and delete DLL from head project ive got most of it i just need to know the code because im not sure what i did but everything ive tried doesnt work or didnt work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  
#include <iostream>

using namespace std;

template <class T>
class DLLNode {
public:
       DLLNode() 
       {
            next = prev = 0;
       }
       DLLNode(const T& el, DLLNode *n = 0, DLLNode *p = 0) 
       {
            info = el; next = n; prev = p;
       }
       T info;
       DLLNode *next, *prev;
};

template <class T>
class DLList {
public:
       DLList() {
           head=tail=0;
       }
       ~DLList();
       int isEmpty() {
           return head==0;
       }
       void addToDLLHead(const T&);
       void addToDLLTail(const T&);
       T deleteFromDLLHead();
       T deleteFromDLLTail();
       void deleteDLLNode(const T&);
       bool isInList(const T&) const;
       void showList();
private:
       DLLNode<T> *head, *tail;
};

template <class T>
DLList<T>::~DLList() {
    for (DLLNode<T> *p; !isEmpty(); ) {
        p=head->next;
        delete head;
        head=p;
    }
}

template <class T>
void DLList<T>::addToDLLTail (const T& el) {
     if (tail != 0) {
         tail = new DLLNode<T>(el, 0, tail);
         tail->prev->next = tail;
     }
     else head = tail = new DLLNode<T>(el);
}

template <class T>
T DLList<T>::deleteFromDLLTail() {
    T el = tail->info;
    if (head == tail) {  // if only one node in the list;
         delete head;
         head = tail = 0;
    }
    else {               // if more than one node in the list;
         tail = tail->prev;
         delete tail->next;
         tail->next = 0;
    }
    return el;
}

template <class T>
void DLList<T>::deleteDLLNode(const T& el) {
    if (head != 0)
        if (head == tail && el == head->info) {
             delete head;
             head = tail = 0;
        }                   
        else if (el == head->info) {
             head = head->next;
             delete head->prev;
             head->prev = 0;
        }
        else {
             DLLNode<T> *tmp;
             for (tmp=head->next;
                  tmp != 0 && tmp->info != el;
                  tmp = tmp->next);
             if (tmp != 0) {
                  tmp->prev->next = tmp->next;
                  if (tmp == tail) tail = tmp->prev;
                  delete tmp;
             }
        }
}

template <class T>
void DLList<T>::addToDLLHead(const T& el) {

// Fill out addToDLLHead code here


}

template <class T>
T DLList<T>::deleteFromDLLHead() {
    T el = head->info;
// Fill out deleteFromDLLHead code here


    return el;
}

                  
template <class T>
bool DLList<T>::isInList(const T& el) const {
     DLLNode<T> *tmp;
     for (tmp = head; tmp != 0 && tmp->info != el; tmp = tmp->next);
     return tmp != 0;
}

template <class T>
void DLList<T>::showList() {
     if (head == 0) cout << endl << "No node in the DLList";
     else
        for (DLLNode<T> *tmp = head; tmp != 0; tmp = tmp->next)
            cout << tmp->info << "  ";
     cout << endl;
}   

int main()
{
    DLList<int> oList;    // create an empty list object
    oList.showList();
    
    oList.addToDLLTail(5);
    oList.addToDLLTail(8);
    oList.addToDLLTail(3);
    oList.showList();    // node order should be 5, 8, 3

    oList.addToDLLHead(6); 
    oList.addToDLLHead(13);
    oList.addToDLLHead(9);
    oList.showList();   // node order should be 9, 13, 6, 5, 8, 3

    oList.deleteFromDLLHead(); 
    oList.showList();

	oList.deleteFromDLLTail();
    oList.showList();

    oList.deleteDLLNode(5); 
    oList.showList();   // node order should be 13, 6, 8


    system("PAUSE");
    return 0;
}
1
2
3
4
5
6
7
8
template <class T>
void DLList<T>::addToDLLTail (const T& el) {
     if (tail != 0) {                          
         tail = new DLLNode<T>(el, 0, tail);  // *1
         tail->prev->next = tail;             // *2
     }
     else head = tail = new DLLNode<T>(el);
}


//*1 - After this line, the node is correctly inserted at the end of the list, however the previous tail node's next pointer is not pointing to the new tail.
//*2 - After this line, the node is no longer connected in any way to the list.


[edit: This is completely wrong. Please ignore this post! I need to wake up.]



Last edited on
I copy pasted the code from addToDLLTail into addToDLLHead. Then, I just swapped all the heads for tails and vice versa. Then I swapped two arguments in the DLLNode constructor call. Then I changed ->prev->next to ->next->prev. I bet there is a very similar approach to typing code for deleteFromDLLHead.
1
2
3
4
5
6
7
8
template <class T>
void DLList<T>::addToDLLHead(const T& el) {
     if (head != 0) {
         head = new DLLNode<T>(el, head, 0);
         head->next->prev = head;
     }
     else head = tail = new DLLNode<T>(el);
}


Doubly Linked Lists are awesomely symmetrical!
Last edited on
Topic archived. No new replies allowed.