i am making a menu driven program on linked lists.
the program's display function is printing garbage values.
i.e if n=3...i add three values and when the values are displayed the last entry is displayed and the rest two enteries are garbage values
for eg.
MENU (Linked List)
1. CREATE
2. INSERT
3. DELETE
4. DISPLAY
5. EXITEnter Your Choice: 1
Enter The Number Of Members You Want To Add: 3
Enter Name: sbi
Enter Roll: 44Enter Name: chi
Enter Roll: 65Enter Name: csc
Enter Roll: 11
MENU (Linked List)
1. CREATE
2. INSERT
3. DELETE
4. DISPLAY
5. EXITEnter Your Choice: 4
Current List:
NAME: csc
ROLL: 11NAME: ☻∙├H.;♠╜▲├QR♠3╔.ï0►▲↓
ROLL: -29906NAME: ┤H═!;┌r♂┤H═!r♣ASP
ROLL: 20563
and when choice 3 is used the program doesnt come to an end...
i will be gratefull if somebody helps me out...
struct student
{
char name[15];
int roll;
student*next;
};
void display(student*first)
{
student*ptr;
ptr=first;
cout<<"\nCurrent List:\n";
while(ptr!=NULL)
{
cout<<"NAME: "<<ptr->name<<"\nROLL: "<<ptr->roll<<"\n\n";
ptr=ptr->next;
}
}
void delet(student*first, int val)
{
student*ptr,*list;
ptr=first;
while(ptr!=NULL)
{
if(ptr->roll==val)
{
list=ptr;
ptr=ptr->next;
delete list;
}
}
}
void main()
{
student*first, *last, *x;
first=last=NULL;
int c=0, val, choice, n, i;
char ch='y';
while (c==0)
{
clrscr();
**menu**
cout<<"\n\nEnter Your Choice: ";
cin>>choice;
switch (choice)
{
case 1:cout<<"\nEnter The Number Of Members You Want To Add: ";//add
cin>>n;
first=new student;
cout<<"\nEnter Name: ";
gets(first->name);
cout<<"Enter Roll: ";
cin>>first->roll;
first->next=NULL;
last=first;
for(i=1; i<n; i++)
{
x=new student;
cout<<"\nEnter Name: ";
gets(first->name);
cout<<"Enter Roll: ";
cin>>first->roll;
x->next=NULL;
last->next=x;
last=x;
}
break;
case 3:do//delete
{
if(first!=NULL)
{
cout<<"\nEnter The Roll: ";
cin>>val;
delet(first, val);
display(first);
}
else
{
cout<<"\nLIST EMPTY!!!";
getch();
break;
}
cout<<"\n\nDo you want to continue (y/n)?\n";
cin>>ch;
}while(ch=='y'||ch=='Y');
break;
case 4:if (first!=NULL)//display
{
display(first);
}
else
{
cout<<"\nLIST EMPTY!!!";
}
getch();
break;
**exit and default**
}
}
}