NEED HELP FOR DOUBLY LINK LIST HOW CAN I SEE THE LIST?

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
#include <iostream>
#include <string.h>

using namespace std;

typedef int Elem; //list element type

class DNode //doubly linked list node
{
private:
	Elem elem; //node elem value
	DNode* prev; //previous node in list
	DNode* next; // next node in list

	friend class DLinkList; //allow DLinkList access
};


class DLinkList
{
public:
	DLinkList(); //constructor for adding sentinels(header and trailer)
	~DLinkList(); //destructor for removing sentinels
	bool isEmpty() const; //check if the list is empty
	const Elem& front() const; // get front element
	const Elem& back() const; //get back element
	void addFront(const Elem& e); //add to front of list
	void addBack(const Elem& e);  // add to back of list
	void removeFront(); //remove from front
	void removeBack(); //remove from back
	void showList();

private:
	DNode* header; //list of sentinels
	DNode* trailer;

protected: //local utilities
	void add(DNode* v, const Elem& e); //insert new node before v
	void remove(DNode * v); //remove node v
};


DLinkList::DLinkList() 
{
	header = new DNode; //create sentinels
	trailer = new DNode;
	header->next = trailer; //point to each other
	trailer->prev = header;
}

DLinkList::~DLinkList()
{
	while (!isEmpty()) //REMOVE ALL SENTINELS
		removeFront();

	delete header; //REMOVE THE SENTINELS
	delete trailer;

}


bool DLinkList::isEmpty() const //check if the list is empty
{
	return (header->next == trailer);

}

const Elem& DLinkList::front() const // get the front elem
{
	return (header->next->elem);
}

const Elem& DLinkList::back() const // get the last elem
{
	return (trailer->prev->elem);
}

void DLinkList::add(DNode* v, const Elem& e) //insert new node before v
{
	DNode* u = new DNode;
	u->elem = e; // put the value in e to u
	
	u->next = v; // point u(next) to v(which is the one you move)
	u->prev = v->prev; //the previous of v will be the previous of u  

	u->prev->next = v->prev = u; // this just points the previous of v be next to u and points u as before v 
	
}

void DLinkList::addFront(const Elem& e)
{
	add(header->next, e); //add to front list
}

void DLinkList::addBack(const Elem& e)
{
	add(trailer, e); //add to back list
}

void DLinkList::remove(DNode* v)
{
	DNode* u = v->prev; //link the v prev to u pointer
	DNode* w = v->next; //link the v next to w pointer
	u->next = w; //connect u and w
	w->prev = u; //connect u and w
	delete v;// remove or unlink v
}

void DLinkList::removeFront()
{
	remove(header->next); // remove the front
}

void DLinkList::removeBack()
{
	remove(trailer->prev); //remove the back;
}

void DLinkList::showList()
{
	if (isEmpty())
		cout << "The list is Empty" << endl; //if true this will print

	else
	{
		for (DNode* p = header->next; p != trailer; p->next) //this prints out the list
			cout << p->elem << '\n';
	}
}



int main()
{
	int num;
	
	DLinkList();

	
	DLinkList a;
	
	cout << "Please enter a number: " << endl;
	cin >> num;
	
	a.addBack(num);
	a.showList();

	
	system("pause");
	return 0;
}
Last edited on
Please use code tags. Edit your post, highlight the code and click the "<>" button to the right of the edit window.
v->prev->next = v->prev = u; // this just points the previous of v be next to u and points u as before v
Is that v>prev the original one, or the one after the assignment on the left side of the statement? The answer is that you don't know. The language lets the compiler decide. You can get around this by using u->prev instead:
u->prev->next = v->prev = u; // this just points the previous of v be next to u and points u as before v

Review your DLinkList constructor. You don't set any of the prev pointers and you set one of the next pointers incorrectly.

Why does showList() take a DNode pointer argument? The users of your list class shouldn't have to know about the DNode class at all. More importantly, they have no way to access any of the Dnodes in the class, so there's no way for them to pass a DNode as the argument.

I think showList should be more like:

1
2
3
4
5
6
7
8
9
void DLinkList::showList() // no arguments
{
if (isEmpty()) {
    cout << "The list is Empty\n";
} else {
    for (DNode *p = header->next; p != trailer; p = p->next) {
        cout << p->elem << '\n';
    }
}
hi thanks for helping. i fix the constructor, so for the u->prev this one is already in the position of v that is why we use it instead of v? for the showlist i tried to debug it but the cin>>num does not stop spamming or it does not stop to show the number endlessly
Topic archived. No new replies allowed.