Linked List Help

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
 
B

benben

imranzafar said:
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

Hmmm, you need to better structure your code perhaps. For example, you are
bundling list manipulation with user level IO, allocating memory with
malloc() without free()ing any, etc. Basically, the first/last node in a
linked list must be treated carefully, usually with if-then-else branchings.

How to solve the problem? Get yourself a debugger and step through the code.
This took me seconds to find the error, so I think it wouldn't take you long
either.

Ben
 
K

Kai-Uwe Bux

imranzafar said:
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?

It went wrong in not using high-level features of C++. You are a beginner
c++ programmer. So maybe, you want to avoid getting into the dark corners
of pointer fiddling where it is not needed.

If it is up to you, I would suggest learning about the standard library
first, then classes, exceptions, templates, and *finally* pointers and
arrays.

#include <stdio.h>
#include <alloc.h>
#include <ctype.h>
#include <conio.h>
#include <iostream.h>

Here are the headers that I would use:

#include <string>
#include <iostream>
#include <algorithm>
#include <list>
#include said:
struct student
{
int st_id;
char name[50];
student *ptr;
};

a) char name [50] is not good.
b) Why do you insist on rolling your own list code?
So:

struct student {
int st_id;
std::string name;
};

And since any nice type should come with I/O, we add:

std::istream & operator>> ( std::istream & i_str, student & s ) {
if ( ! ( ( i_str >> s.st_id ) && ( i_str >> s.name ) ) ) {
// throw something
}
return( i_str );
}

std::eek:stream & operator<< ( std::eek:stream & o_str, student const & s ) {
o_str << s.st_id << ' ' << s.name << ' ';
return( o_str );
}

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

Let us take the issues apart:

struct bad_input {}; // flags invalid input
struct end_input {}; // the user wants to stop

student prompt_for_student ( void ) {
student result;
std::string input;
std::cout << "Please enter student id: ";
if ( ! std::getline( std::cin, input ) ) {
throw ( bad_input() );
}
if ( input == "" ) {
throw( end_input() );
}
std::stringstream s_str ( input );
if ( ! ( s_str >> result.st_id ) ) {
throw ( bad_input() );
}
std::cout << "Please enter student name: ";
if ( ! std::getline( std::cin, result.name ) ) {
throw( bad_input() );
}
return( result );
}


student_list read_students ( void ) {
student_list result;
try {
while( true ) {
result.push_back( prompt_for_student() );
}
}
catch( end_input ) {}
catch( bad_input ) {
std::cout << "Can't you even enter something that simple? Go home!\n";
throw;
}
return( result );
}
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 print_student ( student const & s ) {
std::cout << s << '\n';
}
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

You do not need your own code here. A list is deleted by calling the
clear()-method:


int main ( void ) {
student_list l = read_students();
std::for_each( l.begin(), l.end(), print_student );
std::cout << "\nDeleting list.\n";
l.clear();
std::for_each( l.begin(), l.end(), print_student );
}



Best

Kai-Uwe Bux
 
I

imranzafar

Its really nice of you two guys. I really apprciate your help and
Kai-Uwe Bux special thanks to you for taking out your time and
rewriting the whole code again. I really really appreicate you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top