Here is a DLinkedList Implementation, an element of int type is added to it using addFront(), but its not retrieved using front().. No Error is displayed. Don’t know why ?? Here is complete implementation. Code is run on xCode4.5.
int main(int argc, const char * argv[])
{
DLinkedList listOne;
if(listOne.empty()){
cout<<"Empty";
}
const int num=23;
listOne.addFront(num);
//Here 0 is returned from front(), while it should be 23;
cout<<listOne.front()<<endl;
return 0;
}
//Node Defination
class DNode { // doubly linked list node
private:
int elem; // node element value
DNode* prev; // previous node in list
DNode* next; // next node in list
friend class DLinkedList; // allow DLinkedList access
};
class DLinkedList { // doubly linked list
public:
DLinkedList(); // constructor
~DLinkedList(); // destructor
bool empty() const; // is list empty?
const int front() const; // get front element
const int back() const; // get back element
void addFront(const int e); // add to front of list
void addBack(const int e); // add to back of list
void removeFront(); // remove from front
void removeBack(); // remove from back
private: // local type definitions
DNode* header; // list sentinels
DNode* trailer;
protected: // local utilities
void add(DNode* v, const int e); // insert new node before v
void remove(DNode* v); // remove node v
};
void listReverse(DLinkedList& L) { // reverse a list
DLinkedList T; // temporary list
while (!L.empty()) { // reverse L into T
int s = L.front(); L.removeFront();
T.addFront(s);
}
while (!T.empty()) { // copy T back to L
int s = T.front(); T.removeFront();
L.addBack(s);
}
}
void DLinkedList::remove(DNode* v) { // remove node v
DNode* u = v->prev; // predecessor
DNode* w = v->next; // successor
u->next = w; // unlink v from list
w->prev = u;
delete v;
}
void DLinkedList::removeFront() // remove from font
{ remove(header->next); }
void DLinkedList::removeBack() // remove from back
{ remove(trailer->prev); }
// insert new node before v
void DLinkedList::add(DNode* v, const int e) {
DNode* u = new DNode; u->elem = e; // create a new node for e
std::cout<<u->elem<<std::endl;
u->next = v; // link u in between v
u->prev = v->prev; // ...and v->prev
v->prev->next = v->prev = u;
}
void DLinkedList::addFront(const int e) // add to front of list
{
add(header->next, e);
}
void DLinkedList::addBack(const int e) // add to back of list
{ add(trailer, e); }
DLinkedList::~DLinkedList() { // destructor
while (!empty()) removeFront(); // remove all but sentinels
delete header; // remove the sentinels
delete trailer;
}
DLinkedList::DLinkedList() { // constructor
header = new DNode; // create sentinels
trailer = new DNode;
header->next = trailer; // have them point to each other
trailer->prev = header;
std::cout<<"DLinkedListConstructor"<<std::endl;
}
bool DLinkedList::empty() const // is list empty?
{ return (header->next == trailer); }
const int DLinkedList::front() const // get front element
{ return header->next->elem; }
const int DLinkedList::back() const // get back element
{ return trailer->prev->elem; }
The problem is with your
addfunction:Let’s follow it through. You begin with the following situation (I’ve called the node that
v->prevpoints top):You then set
u->nexttov:And then
u->prevtov->prev:The next line is the problem. First it assigns
utov->prev:Then it assigns the same pointer to
v->prev->next. Butv->previs nowu, so you’re just settingu->nextto point atvand it already does. So there’s no change.Now when you attempt to get the front element, you are just getting element
v, which is your sentinel node.You need to do these two assignments separately: