Double linked list : insertion stops after entering third value
Jun 26, 2014 at 6:12pm UTC
Hi All,
I am writing a double linked list , but my program is giving me a weird problem, for the insertion if I enter the first node it's working fine and also the second node , but if i enter 3rd node the screen just stops there neither vanishes nor prints the prompt for entering 4th node .below is my code .
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
#include<iostream>
using namespace std;
struct node
{ node * prv;
int num;
node * next;
};
class link_list
{
node * head;
node * list;
public :
link_list()
{
head =NULL;
list = NULL;
}
void insert();
//void del_at_pos();
//void reverse_list();
void print_list();
};
void link_list :: insert()
{
char ch1;
int no;
do {
cout <<"add the node u wish to add?" <<endl;
cin >> no;
if (head==NULL)
{
head = new node;
list= head;
head->num = no;
head->next=NULL;
head->prv=NULL;
}
else
{
node * newNode;
list=head;
while (list->next!=NULL)
{
list->next=list;
}
newNode = new node;
newNode->num=no;
newNode->prv=list;
list->next = newNode;
newNode->next=NULL;
}
cout <<"do u wish to add another node?" << endl;
cin >> ch1;
}while (ch1=='y' ||ch1=='Y' );
}
void link_list :: print_list()
{
int count=0;
if (head==NULL)
{
cout << "List is Empty!!!" << endl;
}
else
{
list=head;
while (list!=NULL)
{
count++;
cout << count << " " << list->num <<endl;
list =list->next;
}
}
}
int main()
{
char ch;
link_list ll;
do
{
cout<< "select anyone of the following :" << endl;
cout <<"press 1 for insertion :" << endl;
cout <<"press 2 for deletion :" <<endl;
cout <<"press 3 for reverse :" << endl;
cout <<"press 4 for print :" <<endl;
int i;
cin >> i;
switch (i)
{
case 1 :
ll.insert();
break ;
/* case 2 :
ll.del_at_pos();
break;
case 3 :
ll.reverse_list();
break;*/
case 4 :
ll.print_list();
break ;
default :
cout <<"oops wrong choice!!!" <<endl;
}
cout << "do you want to make another choice ?" << endl;
cin >> ch;
}while (ch=='y' ||ch=='Y' );
system("pause" );
return 0;
}
Jun 27, 2014 at 2:36pm UTC
someone please help!!!
Jun 27, 2014 at 2:52pm UTC
1 2 3 4
while (list->next != NULL)
{
list->next = list;
}
What do you suppose this does?
Jun 27, 2014 at 3:02pm UTC
oops my mistake :(.. found the problem...
Last edited on Jun 27, 2014 at 3:03pm UTC
Topic archived. No new replies allowed.