Hi i am a beginner in programming C++ language and am having a bug in the doubly linked list program. The problem is while printing the list in fwd direction the prgm prints the contents just fine. however while trying to print the list in reverse direction nothing gets printed. Any comments on why this is happening would greatly b appreciated. Thanks
#include <iostream.h>
#include <process.h>
#include <conio.h>
class node
{
public:
int data;
node *prev,*next;
};
class doubly
{
private:
node *head;
public:
doubly()
{
head=NULL;
}
void create();
void display();
};
void doubly::create()
{
int val;
node *temp;
temp=new node;
cout<<"\nEnter value:";
cin>>val;
if(head==NULL)
{
temp->data=val;
temp->prev=NULL;
temp->next=NULL;
head=temp;
return;
}
else
temp->data=val;
temp->prev=NULL;
temp->next=head;
temp->next->prev=temp;
head=temp;
//display();
}//create
void doubly::display()
{
node *temp;
temp=head;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
cout<<"\n";
cout<<"\nnow printing in reverse order";
while(temp->prev!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->prev;
}
}//display
void main()
{
int ch;
doubly s;
clrscr();
do
{
cout<<"\n1)Add";
cout<<"\n2)Display";
cout<<"\n3)Exit";
cout<<"\nEnter Choice:";
cin>>ch;
switch(ch)
{
case 1:s.create();
break;
case 2:s.display();
break;
case 3:exit(0);
break;
}
}while(ch!=3);
}//main