473,395 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Linked List Help

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

Aug 18 '05 #1
3 1611

"imranzafar" <im************@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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
Aug 18 '05 #2
imranzafar wrote:
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 <sstream>

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::ostream & operator<< ( std::ostream & 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
Aug 18 '05 #3
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.

Aug 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
1
by: Little | last post by:
Hello everyone. I am trying to do the following program and am unable to get the beginning portion to work correctly. The scanner works when I print the statements without the double linked list...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
2
by: phiefer3 | last post by:
Ok, first of all I'm not sure if this is the correct forum for this question or not. But hopefully someone can help me or at least point me in the direction of the forum this belongs. First of...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.