954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Single Linked list with pointer - problem when inserting from back

I'm new to programming and C++.
I've managed to succesfully insert multiple nodes from front. Problem is that when printing it starts with the latest node and goes "backwards". I would like it to start with the first node and print forward. Therefore I would like to insert new nodes from the back. But I've tried for many hours and read all kinds of tutorials and feel really stupid because I haven't managed to figure it out.

Please guide me to where the problem is.

#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

struct student {
    string firstName;
    string lastName;
    int no;
    int age;
    struct student *next;
}*head=NULL;

void add_student(int a);
void print_list();

int main(int argc, char *argv[])
{
    int studentNo=0;
    char choice;
    do {
          add_student(studentNo);
          cout << endl << "Add another student to the list: (Y/N) ";
          cin >> choice;
          choice=toupper(choice);
          studentNo++;
    } while(choice=='Y');

    print_list();

    system("PAUSE");
    return EXIT_SUCCESS;
}

void add_student(int a)
{      
       struct student *temp=new struct student;
       cout <<"\nStudent first name: ";
       cin >> temp->firstName;
       cout <<"Student last name: ";
       cin >> temp->lastName;
       cout <<"Student age: ";
       cin>>temp->age;
       temp->no=a;
       
       if (head==NULL){
                       temp->next=head;
                       head=temp;
       } else {
              struct student *temp1=new struct student;
              temp1=head;
              while (temp1->next!=NULL) {
                    temp1=temp1->next;
              }
              temp->next=NULL;
              temp1->next=temp;
              head=temp;              
       }       
}

void print_list()
{
     struct student *temp=new struct student;
     temp=head;
     while (temp !=NULL)
     {
           
           cout << "\n\nStudent name: " << temp->firstName << " " << temp->lastName;
           cout << "\nStudent no:   " << temp->no;
           cout << "\nStudent age:  " << temp->age;
           cout << endl << "------------------------------" << endl;
           temp=temp->next;
     }
     cout << endl;
}
methosmen
Newbie Poster
2 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

The head of the list only needs to be set if the list is presently empty. Your add_student() function always resets the head to the last node in the list during the else clause. Just cut that line out and all will be well:

if (head==NULL) {
    temp->next=head;
    head=temp;
} else {
    struct student *temp1=new struct student;
    temp1=head;
    while (temp1->next!=NULL) {
        temp1=temp1->next;
    }
    temp->next=NULL;
    temp1->next=temp;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
temp1=head;                     // temp1 = Head of list
while (temp1->next!=NULL) {     // Iterate till the last node (i.e., next != NULL)
   temp1=temp1->next;
   }
temp->next=NULL;                
temp1->next=temp;               // Add node to end.
head=temp;                      // Head is the last added node! WHAT?


1. You are adding nodes to the end, not the front.
2. When you add your node to the end, you change the value of head, hence all nodes before it are lost.

To add node to front, you do:

temp->next = head;
head = temp;
nbaztec
Posting Pro in Training
475 posts since May 2010
Reputation Points: 57
Solved Threads: 60
 

The head of the list only needs to be set if the list is presently empty. Your add_student() function always resets the head to the last node in the list during the else clause. Just cut that line out and all will be well:

if (head==NULL) {
    temp->next=head;
    head=temp;
} else {
    struct student *temp1=new struct student;
    temp1=head;
    while (temp1->next!=NULL) {
        temp1=temp1->next;
    }
    temp->next=NULL;
    temp1->next=temp;
}

Thank you so much for this simple, yet effective answer. Got me on the right track and I managed to deliver the assignment on time.
Cheers, have a nice day!

methosmen
Newbie Poster
2 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: