470,596 Members | 1,654 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

Student List Using insert_at_end method

hi everyone..im trying to create a student list program using linked list that will display all my info of students..but it seems theres a little prob. after i enter my first student the program will exit.. can somebody help me please. thank you

#include <stdlib.h>
#include <iostream.h>
#include <string.h>
#include <conio.h>

class student {

char name[30];
char gender[6];
int age;
student *next;
friend stud_list;

student() {
cout << "Name: "; cin >> name; cout << endl;
cout << "Gender: "; cin >> gender; cout << endl;
cout << "Age: "; cin >> age; cout << endl;
~student() {
cout << "Deleted!" << endl;
void display();

class stud_list {

int count;
student *first, *last;


stud_list() {
first=NULL; last=NULL;

~stud_list() {
cout << "List Deleted\n";


void insert_at_end(student *a);
void insert_as_first(student *a);


void stud_list::insert_as_first(student *a) {


void student::display() {

cout << "Name: " << name << endl;
cout << "Gender: " << gender << endl;
cout << "Age: " << age << endl;


int main()
student a;
return 0;
Dec 11 '07 #1
3 2454
9,208 Expert Mod 8TB
student() {
cout << "Name: "; cin >> name; cout << endl;
cout << "Gender: "; cin >> gender; cout << endl;
cout << "Age: "; cin >> age; cout << endl;
This constructor is not correct. What I see here is your main(). The constructor is to initialize the object data members. You should have code like:
Expand|Select|Wrap|Line Numbers
  1. name[0] = '\0';
  2. gender[0] ] = '\0';
  3. age = 0;
  4. student::student() {
  5. next=NULL;
  6. };
Then in main() you have:
Expand|Select|Wrap|Line Numbers
  1. student a;
  2. char buffer[80];
  3. cout << "Name: "; cin >> buffer; cout << endl;
  4. a.SetName(buffer);          //you need to write this method

Also, there's no reason for stud_list to be a friend of stud. In general, friend classes are a no-no as they violate the laws of encapsulation. If stud_list needs to fiddle with the data of student, there should be a student method that can be called.

Also, this is C++ and not C. You should be using the string class and not the C string library. Your data members should be string objects and you should cin>> into a string buffer.

Also, your class methods are coded as inline when there is no reason to do so. The member function definitions should be outside the class declaration. There is a place for inline functions, and this problem is not the place.

Also, your linked list is twisted up with the student. Separate youe list from your data:
Expand|Select|Wrap|Line Numbers
  1. //The Data:
  2. class student {
  4. char name[30];
  5. char gender[6];
  6. int age;
  7. etc....
  8. };
  9. class stud_Node
  10. {
  11.     student* s;                //points to a student object
  12.     stud_Node* next;      //points to next node
  13. etc...
  14. };
  15. class stud_list {
  16. int count;
  17. stud_Node *first, *last;
  18. };
The reason for separating these is you may need to use the student in another program and the way things are set up, you will need to drag all the linked list code along with the student. A student is a student and should stand on its own.

Also, I assume this linked list is a class exercise otherwise you should be using the STL list<> template and not re-inventing the wheel.
Dec 11 '07 #2
So you mean to say i must use struct before the class student?
Dec 12 '07 #3
9,208 Expert Mod 8TB
So you mean to say i must use struct before the class student?
I don't know what you mean. In C++ a class and a struct are the same thing. The only difference is that unless you say otherwise, struct members are public by default whereas class members are private. Other than that, you can interchange the struct and class keywords.

May be you could expand a little on your question.
Dec 12 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by mchoya | last post: by
1 post views Thread by Student | last post: by
2 posts views Thread by sallyk07 | last post: by
11 posts views Thread by xxbabysue123xx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.