Major Aly
Major Aly

Reputation: 2580

Displaying a Linked List

I'm encountering a problem with displaying data from the linked list. I have tried to both include the display loop inside my for loop and out just to check whether it was a problem with pointers and data but I've been getting the same result.

It displays the first data but then starts showing gibberish.

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

void main(void) {
    clrscr();
    struct Student {
        string Name;
        double GPA;
        Student *next;
    };

    Student *head;
    head = NULL;

    int ch, i;
    string name;
    double gpa;

    cout << "How Many Records Do You Want To Enter?";
    cin >> ch;
    cout << endl;

    for (i = 0; i < ch; i++) {
        cout << (i + 1) << ". Name Of Student:";
        cin >> name;
        cout << "GPA Of Student:";
        cin >> gpa;
        cout << endl;

        Student *newstudent;
        Student *studentptr;

        newstudent = new Student;
        newstudent->Name = name;
        newstudent->GPA = gpa;
        newstudent->next = NULL;

        if (!head)
            head = newstudent;
        else {
            studentptr = head;

            while (studentptr->next) {
                studentptr = studentptr->next;
            }
            studentptr->next = new Student;
        }
    }

    clrscr();
    Student *display;
    display = head;

    while (display) {
        cout << "Name:" << display->Name << endl;
        cout << "GPA:" << display->GPA << endl;

        display = display->next;
    }
    getch();
}

Any suggestions and pointers towards the right direction?

Apparently I was following someone's tutorial but this error occurs.

Upvotes: 2

Views: 5904

Answers (3)

andre
andre

Reputation: 7249

I have a few suggestions that may help:

struct Student {
    string Name;
    double GPA;
    Student *next;
    Student(const string& name, double GPA) : Name(name), GPA(GPA), next(NULL) {}
    void print() {
        cout << "Name:" << Name << endl;
        cout << "GPA:" << GPA << endl;
    }
};

now instead of:

    newstudent = new Student;
    newstudent->Name = name;
    newstudent->GPA = gpa;
    newstudent->next = NULL;

You simply write:

newstudent = new Student(name, gpa);

Make a struct for the list:

struct StudentList {
    Student* head;
    Student* current;

    StudentList() :head(NULL), current(NULL) {}

    ~StudentList() {/*Delete list here*/}

    void insert(string name, double gpa) {
        if(!head) {
            head = new Student(name, gpa);
            current = head;
        } else {
            current->next = new Student(name, gpa);
            current = current->next;
        }
    }

    void display() {
        Student *display = head;
        while (display) {
            display->print();
            display = display->next;
        }
    }
};

With all this your main should now be:

int main(void) {
    clrscr();

    StudentList list;

    int ch, i;
    string name;
    double gpa;

    cout << "How Many Records Do You Want To Enter?";
    cin >> ch;
    cout << endl;

    for (i = 0; i < ch; i++) {
        cout << (i + 1) << ". Name Of Student:";
        cin >> name;
        cout << "GPA Of Student:";
        cin >> gpa;
        cout << endl;

        list.insert(name, gpa);
    }

    clrscr();
    list.display();
    getch();
}

Upvotes: 1

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153802

Since the question is about suggestions in the right direction:

  1. You need to always check that you input was successful after you attempted to read whatever you want to read, e.g., if (std::cin >> value) { ... }.
  2. Do not use std::endl.
  3. You are creating an excess Student object in your loop.
  4. You are not hooking up the created Student object with the list. You probably meant to do this where you create a new object instead.

Upvotes: 2

Rollie
Rollie

Reputation: 4752

studentptr->next = new Student; should be studentptr->next = newstudent;

Upvotes: 2

Related Questions