#include <iostream>
#include <string.h>
usingnamespace std;
typedefint 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
friendclass 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;
}
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