Problem with traversing doubly link list
Feb 26, 2014 at 5:48pm UTC
so here's the thing, i have no problem traversing at both forward and backward but not simultaneously. i can traverse forward, and traverse again forward with no problem. i can also traverse backward and traverse backward again with no problem (take note this is by not exiting the program). without exiting the program, with the same datas inputed, if i traversed forward, i cannot traverse backward (it only gives me infinite loop of the first data) and vice versa. i hope you can help me
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
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
Node *prev;
};
int main()
{
int choice,linksize;
Node *head,*temp,*ptr,*tail;
head=NULL;
temp=head;
tail=head;
do
{
cout<<"Options" <<endl;
cout<<"1.)Create" <<endl;
cout<<"2.)Display" <<endl;
cout<<"3.)Reverse" <<endl;
cout<<"4.)Exit" <<endl;
cout<<"option: " ;
cin>>choice;
if (choice==1)
{
cout<<"\tHow many would you like to create?: " ;
cin>>linksize;
for (int x=0;x<linksize;x++)
{
if (x==0)
{
ptr=new Node;
ptr->next=NULL;
ptr->prev=NULL;
cout<<"\tEnter Data: " ;
cin>>ptr->data;
head=ptr;
temp=ptr;
}
else if (x>0&&x!=linksize-1)
{
ptr=new Node;
ptr->next=NULL;
ptr->prev=temp;
cout<<"\tEnter Data: " ;
cin>>ptr->data;
temp->next=ptr;
temp=temp->next;
}
else if (x==linksize-1)
{
cout<<"\tThis is the last NODE!" <<endl;
ptr=new Node;
ptr->next=NULL;
ptr->prev=temp;
cout<<"\tEnter Data: " ;
cin>>ptr->data;
temp->next=ptr;
temp=temp->next;
tail=temp;
cout<<"\t" <<tail->prev<<endl;
}
}
}
else if (choice==2)
{
cout<<"Previous\tData(Current)\tNext" <<endl;
temp=head;
while (temp!=NULL)
{
cout<<temp->prev<<"\t" <<temp->data<<"(" <<temp<<")" <<"\t" <<temp->next<<endl;
temp->prev=temp;
temp=temp->next;
}
}
else if (choice==3)
{
cout<<"Previous\tData(Current)\tNext" <<endl;
temp=tail;
while (temp!=NULL)
{
cout<<temp->prev<<"\t" <<temp->data<<"(" <<temp<<")" <<"\t" <<temp->next<<endl;
temp->next=temp;
temp=temp->prev;
}
}
}while (choice!=4);
return 0;
}
Feb 26, 2014 at 6:46pm UTC
remove lines 77 and 89
Feb 27, 2014 at 3:48pm UTC
Wow thanks, that actually works!
Topic archived. No new replies allowed.