943,862 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 5073
  • C++ RSS
Apr 1st, 2007
0

Doubly Linked List Problem

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kartik911 is offline Offline
8 posts
since Jul 2005
Apr 1st, 2007
0

Re: Doubly Linked List Problem

Just looking at the code for display, I'm surprised it doesn't crash.
C++ Syntax (Toggle Plain Text)
  1. void doubly::display()
  2. {
  3. node *temp;
  4. temp=head;
  5.  
  6. while(temp!=NULL)
  7. {
  8. cout<<temp->data<<"\t";
  9. temp=temp->next;
  10. }
  11. cout<<"\n";
  12. cout<<"\nnow printing in reverse order";
  13.  
  14. while(temp->prev!=NULL)
  15. {
  16. cout<<temp->data<<"\t";
  17. temp=temp->prev;
  18. }
  19. }
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 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)
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Apr 2nd, 2007
0

Re: Doubly Linked List Problem

Thanxs for your reply. I made a stupid error! Anyway i am using the Turbo C 3.0 Compiler which is over a decade old. The exit(0) function requires the process.h file
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kartik911 is offline Offline
8 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Insertion Sort and Number Occurence
Next Thread in C++ Forum Timeline: Code Logic: Why does this happen?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC