My list is structured like this:
struct Node {
Node *next;
Node *prev;
T datum;
};
Node *first; // points to first Node in list, or 0 if list is empty
Node *last; // points to last Node in list, or 0 if list is empty
I've attempted to do the following:
void pop_front()
{
//copied from lecture slides
assert(!empty());
Node *victim = first;
first = first->next;
if(first != 0)
{
first->prev = 0;
}
delete victim;
victim=0;
}
The problem is, it is giving me a memory leak when I do the delete victim line. I don't know what's wrong.
edit: this is how I am adding a node:
//MODIFIES: this
//EFFECTS: inserts i into the front of the list
void pushit_tofront(const T &datum)
{
//if list is empty, update both the first and last element
//copied from lecture slides
Node *p = new Node;
p->datum = datum;
p->next = first;
if(empty())
{
first = last = p;
}
else
{
first = p;
}
}
try to use this code for node
class Node {
public:
int get() { return object; }; // returns the value of the element
void set(int object) { this->object = object; }; // set the value of the element
Node* getNext() { return nextNode; }; // get the address of the next node
void setNext(Node* nextNode) // set the address of the next node
{ this->nextNode = nextNode; };
Node* getPrev() { return prevNode; }; // get the address of the prev node
void setPrev(Node* prevNode) // set the address of the prev node
{ this->prevNode = prevNode; };
private:
int object; // it stores the actual value of the element
Node* nextNode; // this points to the next node
Node* prevNode; // this points to the previous node
};
Doubly link list look like this it is uncompleted. but you have to only add the remove function in it.
class DoublyList
{
public:
List();
void add (int addObject);
int get();
bool next();
void start();
void remove();
private:
int size;
Node * headNode;
Node * currentNode;
Node * lastCurrentNode;
};
Doubly link list remove it will remove from any location. you have asked about first node the last two if condition will help you.
DoublyList::remove(){
Node *removeNode= currentNode;
if((currentNode->getNext()!=null)&&(currentNode->getprev())!=headNode)){ // it will remove the node in middle
lastCurrentNode->setNext(currentNode->getNext());
(currentNode->getNext())->setPrev(currentNode->getPrev);
currentNode= currentNode->getNext();
}
if((currentNode->getprev())!=headNode)&&(currentNode->getNext()==null)){ // it will remove the node if it is last node
lastCurrentNode->setNext(null);
currentNode= lastCurrentNode;
lastCurretnNode= currentNode->getPrev();
}
if((currentNode->getprev())==headNode)&&(currentNode->getNext()==null)){ //if it is at the start and next node is null
headNode->setNext(null);
lastCurrentNode= headNode;
currentNode= headNode;
}
if((currentNode->getprev())==headNode)&&(currentNode->getNext()!=null)){ //if it is at the start and next node is not null
headNode->setNext(currentNode->getNext());
(currentNode->getNext())->setPrev(headNode);
lastCurrentNode= headNode;
currentNode= currentNode->getNext();
}
delete removeNode;
size--;
}
I think you are trying to make stack using doubly link list. but it is not clear in you question. so i have written the remove method in all condition. i have not tested this code.you have to do it by self.if there is any mistake you can contact with me.
Concerning the issue of academic honesty, I am not going to post the solution, as this is a popular assignment.
However, I will give you some pointers :)
Anytime you are doing a mutation of a doubly-linked list you have (in your case) at most six pointers to worry about.
curr->next = ?
curr->prev = ?
next->prev = ?
next->next = ?
head = ?
tail = ?
If you are definitively assuring that all those pointers are being managed properly, you will probably resolve your current issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.