Link Lists

I have a project to do that uses Link lists and I'm somewhat confused. I think I'm up to the point where I need to call on things in my main so that I can add/delete/count ect. to the list but I am unsure of how to do so and my book is being of no help.

For an example in my main the line:

cout << "There are currently " << << " Students registered" << endl;

I need the count list function to output how many students are in the list

Node.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 #include <iomanip>
using namespace std;

class Node{

public:

	int nodeValue;
	Node* Next;

	Node();
	Node(int newValue,Node* nextNode = NULL);
	
	void printlist(Node* front);
	void printback(Node* front);
	Node* searchlist(Node* front, int value);
	void deleteNode(Node* &front, int value);
	void insertNode(Node* &front, int value);
	int countlist(Node* front);
	
};


Student.h
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
  #include <iomanip>
using namespace std;



class Students{


public:
	string lastName;
	string firstName;
	string birthDate;
	string year;
	char gender;
	float GPA;
	int studentID;
	
	Students();
	Students(string& lastName, string& firstName, string& birthDate, string& year, char& gender, float* GPA, int* studentID);
	void setlastName(string ln);
	void setfirstName(string fn);
	void setbirthDate(string bd);
	void setyear(string y);
	void setgender(char gn);
	void setGPA(float grade);
	void setid(int id);
	string getlastName();
	string getfirstName();
	string getbirthDate();
	string getyear();
	char getgender();
	float getGPA();
	int getid();
	
};


Node.cpp
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
125
#include <iomanip>
#include <string>
#include <iostream>
#include "Node.h"
#include "Student.h"
using namespace std;

Node::Node(){
	nodeValue = 0;
	Next = NULL;
	      
}

Node::Node(int newValue, Node* nextNode = NULL){
	nodeValue = newValue;
	Next = nextNode;

}


void printlist(Node* front){
	Node* temp = front;

	while (temp != NULL){
		cout << temp->nodeValue;
		temp = temp->Next;
	}
}

void printback(Node* front){
	Node* temp;
	temp = front;


	if (front == NULL){
		return;
	}
	else if (front->Next == NULL){
		cout << front->nodeValue;
		return;
	}
	else{
		printback(front->Next);
		cout << temp->nodeValue;
	}
}

Node* searchlist(Node* front, int value){
	Node* temp = front;

	while (temp != NULL){
		if (value == temp->nodeValue)
			return temp;
		temp = temp->Next;
	}return NULL;
}

void deleteNode(Node* &front, int value){

	Node* curr, *prev;

	curr = front;
	prev = NULL;
	bool found = false;

	while (curr != NULL & !found){

		if (curr->nodeValue == found){
			if (prev == NULL){
				front = front->Next;
			}
			else{
				prev->Next = curr->Next;
			}
			delete curr;
			found = true;
		}
		else{
			prev = curr;
			curr = curr->Next;
		}
	}

}

void insertNode(Node* &front, int value){

	Node* newNode = new Node(value);

	if (front == NULL){
		front = newNode;
		return;
	}

	Node* curr = front;
	Node* prev = NULL;

	while (curr != NULL && curr->nodeValue < value){
		prev = curr;
		curr = curr->Next;
	}
	if (prev == NULL){
		newNode->Next = front;
		front = newNode;
	}
	else if (curr == NULL){
		prev->Next = newNode;
	}
	else{
		prev->Next = newNode;
		newNode->Next = curr;
	}
}


int countlist(Node* front){
	int total = 0;
	Node* curr = front;

	while (curr != NULL){
		total++;
		curr = curr->Next;
	}
	return total;
}


Student.cpp
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
#include <iomanip>
#include <string>
#include <iostream>
#include "Node.h"
#include "Student.h"
using namespace std;


Students::Students(){

	

}

Students::Students(string& ln, string& fn, string& bd, string& y, char& g, float& gr, int& id){
	lastName = ln;
	firstName = fn;
	birthDate = bd;
	year = y;
	gender = g;
	GPA = gr;
	studentID = id;

}

void Students::setlastName(string ln){
	lastName = ln;
}

void Students::setfirstName(string fn){
	firstName = fn;
}

void Students::setbirthDate(string bd){
	birthDate = bd;
}

void Students::setyear(string y){
	year = y;
}
 
void Students::setgender(char gn){
	gender = gn;
}

void Students::setGPA(float grade){
	GPA = grade;
}

void Students::setid(int id){
	studentID = id;
}




string Students::getlastName(){
	return lastName;
}

string Students::getfirstName(){
	return firstName;
}

string Students::getbirthDate(){
	return birthDate;
}

string Students::getyear(){
	return year;
}

char Students::getgender(){
	return gender;
}

float Students::getGPA(){
	return GPA;
}

int Students::getid(){
	return studentID;
}


Main.cpp
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
 #include <iomanip>
#include <string>
#include <iostream>
#include <fstream>
#include "Node.h"
#include "Student.h"
using namespace std;



int mian(){

	int value = 0;
	int temp = 0;
	
	
	label1:
	cout << "*****************************************************" << endl;
	cout << "Welcome to the Salisbury University Student Database." << endl;
	cout << "*****************************************************" << endl;
	cout << endl;
	cout << "There are currently " <<   << " Students registered" << endl;
	cout << endl;
	cout << "            MAIN MENU:               " << endl;
	cout<<	"*select one of the following options*" << endl;
	cout << endl;
	cout << "[1]- Create a new student account." << endl;
	cout << "[2]- Update a current students information." << endl;
	cout << "[3]- Search the Database." << endl;
	cout << "[4]- Delete a student fron the Database." << endl;
	cout << "[5]- Quit the program." << endl;
	cin >> value;

	switch (value){

	case'1':
		cout << endl;
		cout << "You selected: Create a new student account." << endl;
		cout << endl;
		cout << "Enter:" << endl;
		cout << "[1] -Continue." << endl;
		cout << "[2] -Return to Main Menu." << endl;
		cin >> value;

		if (value == 1){
			break;
		}
		else{
			goto label1;
		}
	
		cout << "How many students would you like to enter" << endl;
		cin >> value;
		temp = value;
		
		for (int i = 0; i < value; i++){
			cout << "You are entering Student #" << value << " of #" << temp << endl;
			cout << endl;
			cout << "Enter the students Last Name:" << endl;
		
			cout << "Enter the students First Name:" << endl;

			cout << "Select the students grade level" << endl;
			cout << "[1] -Freshman" << endl;
			cout << "[2] -Sophmore" << endl;
			cout << "[3] -Junior" << endl;
			cout << "[4] -Senior" << endl;
			cin >> value;
			if (value == 1){

			}
			else if (value == 2){

			}
			else if (value == 3){

			}
			else{

			}

		}

	case'2':

	case'3':

	case'4':

	case'5':

	}




	






	}
	
You need to either:


1. Create a new class called "university" which contains a linked list of nodes. Each of these nodes contains a "Student".

or

2. Add a linked list to your "Students" class. Each time you add a new student, add a node containing a student into the linked list.


Then you can traverse the linked list to get the size. Or, better yet, add code to your insert function that updates a size counter data member each time a new student is added and reduce the size counter each time a student leaves.

You should set data members to private.
Last edited on
Topic archived. No new replies allowed.