Need to print list normally

This code was provided by my instructor and the loadList function is printed in reverse. Just wanted to know how to print the linked list normally, not in reverse. Also, if you can help me with my studentList function. I am trying to put a number next to the name of the student. For example:
1. Smith, Jefferey
2. Lee, Jackson
3. Gonzales, Mariana
Etc etc
Thanks in advance.

students.dat file contents:
Smith Jefferey 891789 j.smith@spartans.nsu.edu 3.75
Lee Jackson 3678902 j.lee@spartans.nsu.edu 3.66
Gonzales Mariana 168790 m.gonzales18@spartans.nsu.edu 4.0
Jones Mike 8973125 m.jones143@spartans.nsu.edu 3.1
Williams Anita 2985465 a.williams@spartans.nsu.edu 3.64
Ronsinson Taylor 3278976 t.robinson@spartans.nsu.edu 3.55
Clark Allen 1094567 a.clark@spartans.nsu.edu 3.48
Turner Tavon 318796 t.turner@spartans.nsu.edu 3.2
Jenkins Nelson 289563 n.jenkins@spartans.nsu.edu 3.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//This Example demonstrates some of the features of linked lists of structs
//It performs basic searching, summing, largest values, counting, filtering and input-output

#include <iostream>
using namespace std;
#include <fstream>
#include<string>

struct student; //allows definition of student ptr even though we do not 
			 //define student here
typedef student * studentptr;
struct student
{
	string firstName;
	string lastName;
	unsigned int studentID;
	string email;
	float gpa;
	studentptr next;
};
student *head, *tail;

class studentsProc // Definition of class named studentsProc
{
	public:
		void loadList(studentptr & students); // Function that loads data from a data file into a student list
		void studentList(student *head);
	
	private:
		student * student; // Pointer to the list of students
		void printList() const; //Print the contents of the list
};

//This function loads values into the linked lists with head pointer
// from a data file called "linked.dat". The list is built in reverse
void loadList(studentptr & students)
{
	ifstream infile;
	infile.open("students.dat");
	students = NULL; //head pointer points no where
	student temp;
	studentptr temp_ptr;
	while (!infile.eof())
	{
		temp_ptr = new student;
		infile >> temp_ptr->firstName >> temp_ptr->lastName >> temp_ptr->studentID >> temp_ptr->email >> temp_ptr->gpa;//place data in the node
		temp_ptr->next = students;
		students = temp_ptr;
	}
	infile.close();
	
}

// Function definition that displays list of students' names the user can choose from
void studentList(student *head)
{
	student *n = head;
	while(n)
	{
	cout << n->firstName << " " << n->lastName << " " << n->studentID << " " << n->email << " " << n->gpa << endl;
	n = n->next;	
	}		
}

int main()
{
	studentptr students;
	loadList(students);
	studentList(students);
	return 0;
}
Actually are you aware that tha function loadList(...) is not related to the class studentsProc?

You add the new entry to the head of the list. That the reason why it is backwards.
Consider this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void loadList(studentptr & students)
{
	ifstream infile;
	infile.open("students.dat");
	students = NULL; //head pointer points no where
	student temp;
	studentptr temp_ptr = NULL;
	while (!infile.eof())
	{
if(temp_ptr)
{
  temp_ptr->next = new student;
  temp_ptr = temp_ptr->next;
}
else
{
  temp_ptr = new student;
  students = temp_ptr;
}
		infile >> temp_ptr->firstName >> temp_ptr->lastName >> temp_ptr->studentID >> temp_ptr->email >> temp_ptr->gpa;//place data in the node
	}
	infile.close();
	
}
Topic archived. No new replies allowed.