| | |
Doubly Linked List Problem
Thread Solved |
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
#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
Just looking at the code for display, I'm surprised it doesn't crash.
When you quit the first loop, temp will be NULL, as per the loop condition. Then when you try to start the second loop, you'll be accessing invalid memory trying to dereference NULL (and should crash). You'll can change the first loop to go until
Side notes:
- <iostream.h> is deprecated. Use <iostream> and the std:: namespace in the future.
- <conio.h> is compiler specific and most people here would not be able to run your code.
- Why do you need <process.h>? Unless I missed something...
- Please use [code] and [/code] tags when you post code on the forums, as it preserves formatting (especially indentation)
C++ Syntax (Toggle Plain Text)
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; } }
temp->next == NULL or you can keep a tail pointer in addition to the head one (where tail would record the last node in the list).Side notes:
- <iostream.h> is deprecated. Use <iostream> and the std:: namespace in the future.
- <conio.h> is compiler specific and most people here would not be able to run your code.
- Why do you need <process.h>? Unless I missed something...
- Please use [code] and [/code] tags when you post code on the forums, as it preserves formatting (especially indentation)
![]() |
Similar Threads
- Linked List problem (C)
- doubly linked list--- help needed1 (C++)
- To create contact manager using doubly linked list(c++) (C++)
- "doubly linked list" question (C)
- How Can I Sort a Doubly Linked List Queue? (C)
- Need Help to Print Doubly Linked List(DLL) (Java)
- doubly linked list implementation (Java)
Other Threads in the C++ Forum
- Previous Thread: Insertion Sort and Number Occurence
- Next Thread: Code Logic: Why does this happen?
Views: 4046 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment basic beginner binary c++ c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files form forms fstream function functions game givemetehcodez graph graphics gui homework iamthwee ifstream input int lazy link linker list loop loops map math matrix member memory newbie number numbers object objects opengl output parameter pointer pointers problem program programming project python random read reading recursion recursive reference search sockets sort spoonfeeding string strings struct student studio template templates text time tree url variable vc++ vector video visual win32 window windows winsock wxwidgets






