I
imranzafar
hi! i am beginner c++ programmer. Here is my code. I can delete any
entered record but if i want to delete first and last node of the list
it goes into indefinite loop, although the compilation is successfull.
Can anybody suggest any clue what and where it went wrong?
#include <stdio.h>
#include <alloc.h>
#include <ctype.h>
#include <conio.h>
#include <iostream.h>
struct student
{
int st_id;
char name[50];
student *ptr;
};
student *fptr, *cptr, *nptr;
void create(void);
void display (void);
void del(void);
void main(void)
{
clrscr();
create();
display();
del();
display();
} //end of main
void create (void)
{
char ch;
do{
nptr=(student*) malloc(sizeof(struct student));
cout<<"\n\tEnter Student ID = ";cin>>nptr->st_id;
cout<<"\n\tEnter Student Name = ";gets(nptr->name);
if (fptr==NULL)
{fptr = cptr = nptr;}
else
{cptr->ptr=nptr;
cptr = nptr;}
nptr = NULL;
cout<<"\n\tEnter another record (y/n)";
ch=tolower(getche());
}while (ch!='n');
cptr->ptr=NULL;
} //end of create
void display(void)
{
char ch;
cptr=fptr;
do{
cout<<"\n\tStudent ID = ";cptr->st_id;
cout<<"\n\tStudent Name = ";puts(cptr->name);
cout<<"\n\tView another record (y/n";
ch=tolower(getche());
if (ch=='y');
cptr=cptr->ptr;
}while (ch!='n' && cptr !=NULL);
} //end of display
void del(void)
{
student *pptr;
pptr=cptr=fptr;
int id;
cout<<"\n\tEnter record to delete = ";cin>>id;
do{
if (cptr->st_id == id)
{
cout<<"\n\tStudent ID = " <<cptr->st_id;
cout<<"\n\tStuident Name = "<<puts(cptr->name);
cout<<"\n\tThe above record will be deleted";
cout<<endl;
pptr->ptr=cptr->ptr;
}
pptr=cptr;
cptr=cptr->ptr;
}while(cptr->ptr!=NULL);
} //end of delete
entered record but if i want to delete first and last node of the list
it goes into indefinite loop, although the compilation is successfull.
Can anybody suggest any clue what and where it went wrong?
#include <stdio.h>
#include <alloc.h>
#include <ctype.h>
#include <conio.h>
#include <iostream.h>
struct student
{
int st_id;
char name[50];
student *ptr;
};
student *fptr, *cptr, *nptr;
void create(void);
void display (void);
void del(void);
void main(void)
{
clrscr();
create();
display();
del();
display();
} //end of main
void create (void)
{
char ch;
do{
nptr=(student*) malloc(sizeof(struct student));
cout<<"\n\tEnter Student ID = ";cin>>nptr->st_id;
cout<<"\n\tEnter Student Name = ";gets(nptr->name);
if (fptr==NULL)
{fptr = cptr = nptr;}
else
{cptr->ptr=nptr;
cptr = nptr;}
nptr = NULL;
cout<<"\n\tEnter another record (y/n)";
ch=tolower(getche());
}while (ch!='n');
cptr->ptr=NULL;
} //end of create
void display(void)
{
char ch;
cptr=fptr;
do{
cout<<"\n\tStudent ID = ";cptr->st_id;
cout<<"\n\tStudent Name = ";puts(cptr->name);
cout<<"\n\tView another record (y/n";
ch=tolower(getche());
if (ch=='y');
cptr=cptr->ptr;
}while (ch!='n' && cptr !=NULL);
} //end of display
void del(void)
{
student *pptr;
pptr=cptr=fptr;
int id;
cout<<"\n\tEnter record to delete = ";cin>>id;
do{
if (cptr->st_id == id)
{
cout<<"\n\tStudent ID = " <<cptr->st_id;
cout<<"\n\tStuident Name = "<<puts(cptr->name);
cout<<"\n\tThe above record will be deleted";
cout<<endl;
pptr->ptr=cptr->ptr;
}
pptr=cptr;
cptr=cptr->ptr;
}while(cptr->ptr!=NULL);
} //end of delete