问题
I used this in my main function but it's not working
void LinkedList::TraPrinHead(const LinkedList& p)
{
Nodes* currentNodes = header->next;
while( currentNodes != tail ) {
cout << currentNodes->elem << " ----> ";
currentNodes = currentNodes->next; }
}
I expect to print the whole list from this... but I keep getting the infinite loop.
cout << currentNodes->elem << " ----> ";
currentNodes = currentNodes->next;
cout << currentNodes->elem << " ----> ";
currentNodes = currentNodes->next;
And even if I simplify it just to print out the first two elements on the list I do not get the infinite loop but keep getting the same thing for the different two nodes
For example, my first node was A1, second was A2, but with that function I expect to get A1 ----> A2 but what I get is A1 ----> A1 ---->
I think I have problems on my add function.
this is the function that I use
void LinkedList::InsertDoublyBefore(Nodes* d, const string& e) {
if (header->next == tail)
{
Nodes* n = new Nodes;
n->elem = e;
n->next = tail;
n->prev = tail->prev;
tail->prev->next = tail->prev = n;
header->next = n; // very important!!!!
}
else
{
if (d==tail)
{
Nodes* n = new Nodes;
n->elem = e;
n->next = tail;
n->prev = tail->prev;
tail->prev = n;
}
else
{
Nodes* n = new Nodes;
n->elem = e;
n->next = d;
n->prev = d->prev;
d->prev->next = d->prev = n;
}
}
}
void LinkedList::InsertDoublyAfter(Nodes* d, const string& e)
{
InsertDoublyBefore(d->next, e);
}
void LinkedList::addtoFront(const string& e) { InsertDoublyBefore(header->next, e); }
void LinkedList::addtoBack(const string& e) { InsertDoublyBefore(tail, e); }
回答1:
Your cases are a little redundant. The cases that should be handled by this specific insert function include.
- d==head (need to change head to newNode)
- head==tail (need to change head and tail, this is also case when list is empty as head==tail==NULL)
you also may want to consider visiting your TA or your instructors office hours.
You have a lot of questions about very foundational concepts, and if you aren't gathering enough information from your lecture notes to comprehend the logic behind these ideas(from all of your posts) then you should be trying to reach out to your instructor, TA, or other options for assistance on campus. As these are very important ideas to fully understand for the future development of customized data structures and application development.
回答2:
This line
tail->prev->next = tail->prev = n;
doesn't look right. After tail->prev = n
you're going to modify n->next
with tail->prev->next = ...
and not really tail->prev->next
. Actually, you have undefined behavior here because you modify and then use the same variable (tail->prev
) in the expression and that's much worse.
Here
if (d==tail)
{
Nodes* n = new Nodes;
n->elem = e;
n->next = tail;
n->prev = tail->prev;
tail->prev = n;
}
you seem to be modifying only half of the links.
And here
{
Nodes* n = new Nodes;
n->elem = e;
n->next = d;
n->prev = d->prev;
d->prev->next = d->prev = n;
}
you have problems similar to the aforementioned ones.
Use your debugger. But before that, work out everything on paper.
来源:https://stackoverflow.com/questions/15994609/c-linked-list-printing-i-get-the-infinite-loop