Singly linked list

Feb 21, 2012 at 4:06am
Hi guys im trying to write a C++ program to read 510 students’ name and ID and also use a singly linked list to add each node to the bottom of the list. then to also reverse the list after it is created

If any one could help with this it would be great. This is what i have so far

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

#include<iostream>
#include<fstream>
#include<string>


using namespace std;


struct Student


{
   string name; 

   int id;

   Student *next;  //pointer to the next student

};
 
struct node
{
	node *next;
	int value;
};
	node *reverseList( node *head)  //function to reverse link list
		{
			node *next;

			node *newlist=head;
			node *newStudent=NULL;

			while(newlist !=NULL)
			{
				next = newlist->next;
				newlist->next = newStudent;
				newStudent = newlist;
				newlist = next;
			}

			return (newStudent);
	}
	


		


			

int main()
{

	Student *head;
	head = NULL; 
	
	string Student_name;
	int Student_id;

for (int i = 0; i < 3; i++)
	{

		cout << "what is the students name: ";
		cin >> Student_name;

		cout << "what is "<< Student_name << "'s id number: ";
		cin >> Student_id;


		Student *newStudent;
		Student *Student_ptr;




		newStudent = new Student;
		newStudent->name = Student_name;
		newStudent->id = Student_id;
		newStudent->next = NULL;



	if(!head)

	head = newStudent;

	else

	{

		Student_ptr = head;

	while(Student_ptr -> next)

		Student_ptr = Student_ptr -> next;
		Student_ptr->next = newStudent;

	}

		cout << endl;

	}
		Student *Display_ptr;            //Display the list add node to bottom 
		Display_ptr = head;

		while (Display_ptr)

		{

			cout << Display_ptr->name << endl;
			cout << Display_ptr->id<< endl;
			Display_ptr = Display_ptr->next;
		
			cout << endl; 
			 
		}
		
	

	 system("PAUSE");
		
}


PLEASE LET ME KNOW WHAT I AM DOING WRONG! THE PROGRAM RUNS BUT I DONT BELIEVE IT IS DOING WHAT THE QUESTION ASKS!

thanks and cant wait to here from yoou guys and girls soon!!!
Last edited on Feb 21, 2012 at 8:07am
Feb 21, 2012 at 4:16am
Feb 21, 2012 at 4:30am
closed account (zwA4jE8b)
A linked list is a type that consists of nodes.
Each node has data fields and a like to the next node. This link is a pointer. A pointer to a node!
The operations for this are fairly simple. Keep a pointer to the root node, if you lose this, your list is gone.

If you need more help, post what you have so far, ask a more specific question. And most people will be glad to help.
Last edited on Feb 21, 2012 at 4:34am
Topic archived. No new replies allowed.