do while not working properly..
May 23, 2014 at 2:05pm UTC
Hi all, below is my piece pf code of linked list for deletion. My problem is whenever I am choosing a position to delete it asks me "do u wish to delete another node" and even after entering "n" , instead of going to main function it is going back to the "do" loop and asks ""enter the node you wish to delete"
Pls let me know what's wrong ?
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
void link_list :: del_pos()
{
if (head==NULL)
{
cout<<"List is empty can't delete" <<endl;
}
else
{
char ch;
do
{
node *s ;
int pos ;
int count =1;
list=head;
cout<<"enter the node you wish to delete" <<endl;
cin >> pos;
if (pos==1 && head->next==NULL)
{
delete head;
}
else
{
if (pos==1)
{
s=head;
head=list->next;
}
else
{
//s=list->next;
while (list!=NULL)
{
list=list->next;
s=list->next->next;
count++;
}
if (count==pos)
{
list->next = s->next;
}
}
}
delete s;
/* list=head;
while(list->next->!=NULL)
{
list=list->next;
}
list->next=NULL;*/
cout <<"do u wish to delete another node?" <<endl;
cin >> ch;
} while (ch='y' ||ch=='Y' );
}
}
int main()
{
link_list ll;
char choice;
do {
cout << "select one" << endl;
cout << "press 1 for insert ." << endl;
cout << "press 2 for insert at beginning ." << endl;
cout << "press 3 for insert at the middle ." << endl;
cout << "press 4 normal_delete ." << endl;
cout << "print 5 print the linked list :" << endl;
cout << "print 6 exit :" << endl;
int no;
cin >> no;
switch (no)
{
case 1:
ll.insert();
break ;
case 2 :
ll.insert_at_beg();
break ;
case 4:
ll.del_pos();
break ;
case 5:
ll.print_list();
break ;
case 6:
return 0;
default :
cout <<"oops wrong choice" << endl;
}
fflush(stdin);
cout << "Do u wanna make another choice?" << endl;
cin >> choice;
cout<< choice << endl;
}while (choice=='Y' ||choice=='y' );
cout<<"Thanks!" << endl;
system("pause" );
return 0;
}
May 23, 2014 at 2:09pm UTC
line 58:
while (ch='y' ||ch=='Y' );
You are doing an assignment (=) instead of a comparison (==)
May 23, 2014 at 2:16pm UTC
@disch
thanks a lot :) ... It was so silly of me to miss that :)
Topic archived. No new replies allowed.