Need help with linked list program

Hello, I am looking for help on my program. I want to have one iteration in my program for "Courses for Jacob: " at the top of my output and I was hoping to get feedback on what changes I could make to get this outcome. Below is my code.

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct node
{
private:
string fullName;
int courseNumber;
string courseName;
string courseRoom;
node *next;
public:
string getFullName(){
return fullName;
}
void setFullName(string Full){
fullName = Full;
}
string getCourseName(){
return courseName;
}
void setCourseName(string name){
courseName = name;
}
int getCourseNumber(){
return courseNumber;
}
void setCourseNumber(int number){
courseNumber = number;
}
string getCourseRoom(){
return courseRoom;
}
void setCourseRoom(string room){
courseRoom = room;
}
void setNext(struct node * nextNode){
next = nextNode;
}
struct node * getNext(){
return next;
}

};


class linked_list
{
private:
node *head,*tail;
public:
linked_list()
{
head = NULL;
tail = NULL;
}


void add_node(struct node newNode)
{
struct node *tmp = new struct node;
tmp->setFullName(newNode.getFullName());
tmp->setCourseName(newNode.getCourseName());
tmp->setCourseNumber(newNode.getCourseNumber());
tmp->setCourseRoom(newNode.getCourseRoom());
tmp->setNext(NULL);


if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->setNext(tmp);
tail = tail->getNext();
}
}
struct node *getHead(){
return head;
}
struct node *getTail(){
return tail;
}
};


int main()
{
struct node record;
record.setFullName("Jacob Hallum");
record.setCourseName("C++ Programming");
record.setCourseNumber(31613);
record.setCourseRoom("SHIM 237");
linked_list a;
a.add_node(record);

struct node record2;
record2.setFullName("Jacob Hallum");
record2.setCourseName("Calculus 2");
record2.setCourseNumber(31525);
record2.setCourseRoom("SCMA 271");
a.add_node(record2);

struct node record3;
record3.setFullName("Jacob Hallum");
record3.setCourseName("Princ Physics: Mechanics");
record3.setCourseNumber(31296);
record3.setCourseRoom("SCMA 217");
a.add_node(record3);

struct node record4;
record4.setFullName("Jacob Hallum");
record4.setCourseName("Intro to Psychology");
record4.setCourseNumber(30297);
record4.setCourseRoom("Online");
a.add_node(record4);

struct node * head = a.getHead();
while(head !=NULL){
cout<<"Course for: "<<head->getFullName()<<"\n";
cout<<"Course Number: "<<head->getCourseNumber()<<"\n";
cout<<"Course Name: "<<head->getCourseName()<<"\n";
cout<<"Course Room: "<<head->getCourseRoom()<<"\n\n";
head=head->getNext();
}
string name = head->getCourseName();
cout<<name<<endl;
return 0;
}

The output I have for my program is:

Course for: Jacob Hallum
Course Number: 31613
Course Name: C++ Programming
Course Room: SHIM 237

Course for: Jacob Hallum
Course Number: 31525
Course Name: Calculus 2
Course Room: SCMA 271

Course for: Jacob Hallum
Course Number: 31296
Course Name: Princ Physics: Mechanics
Course Room: SCMA 217

Course for: Jacob Hallum
Course Number: 30297
Course Name: Intro to Psychology
Course Room: Online
You can reuse record. You don't need record2, record3, and record4. By reusing it, you also save having to set the name each time.

Use a for loop to walk through a linked list.

Only print the name if it's different from the previous value.
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
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct node
{
  private:
    string fullName;
    int courseNumber;
    string courseName;
    string courseRoom;
    node *next;
  public:
      string getFullName()
    {
	return fullName;
    }
    void setFullName(string Full)
    {
	fullName = Full;
    }
    string getCourseName()
    {
	return courseName;
    }
    void setCourseName(string name)
    {
	courseName = name;
    }
    int getCourseNumber()
    {
	return courseNumber;
    }
    void setCourseNumber(int number)
    {
	courseNumber = number;
    }
    string getCourseRoom()
    {
	return courseRoom;
    }
    void setCourseRoom(string room)
    {
	courseRoom = room;
    }
    void setNext(struct node *nextNode)
    {
	next = nextNode;
    }
    struct node *getNext()
    {
	return next;
    }

};

class linked_list
{
  private:
    node * head, *tail;
  public:
    linked_list()
    {
	head = NULL;
	tail = NULL;
    }

    void add_node(struct node newNode)
    {
	struct node *tmp = new struct node;
	tmp->setFullName(newNode.getFullName());
	tmp->setCourseName(newNode.getCourseName());
	tmp->setCourseNumber(newNode.getCourseNumber());
	tmp->setCourseRoom(newNode.getCourseRoom());
	tmp->setNext(NULL);

	if (head == NULL) {
	    head = tmp;
	    tail = tmp;
	} else {
	    tail->setNext(tmp);
	    tail = tail->getNext();
	}
    }
    struct node *getHead()
    {
	return head;
    }
    struct node *getTail()
    {
	return tail;
    }
};

int
main()
{
    struct node record;
    record.setFullName("Jacob Hallum");
    record.setCourseName("C++ Programming");
    record.setCourseNumber(31613);
    record.setCourseRoom("SHIM 237");
    linked_list a;
    a.add_node(record);

    // Add more courses for Jacob
    record.setCourseName("Calculus 2");
    record.setCourseNumber(31525);
    record.setCourseRoom("SCMA 271");
    a.add_node(record);

    record.setCourseName("Princ Physics: Mechanics");
    record.setCourseNumber(31296);
    record.setCourseRoom("SCMA 217");
    a.add_node(record);

    record.setCourseName("Intro to Psychology");
    record.setCourseNumber(30297);
    record.setCourseRoom("Online");
    a.add_node(record);

    string prevName;
    if (a.getHead()) {
	for (struct node *head = a.getHead(); head; head = head->getNext()) {
	    if (head->getFullName() != prevName) {
		cout << "\nCourse for: " << head->getFullName() << "\n";
		prevName = head->getFullName();
	    }
	    cout << "Course Number: " << head->getCourseNumber() << "\n";
	    cout << "Course Name: " << head->getCourseName() << "\n";
	    cout << "Course Room: " << head->getCourseRoom() << "\n\n";
	}
    }
    return 0;
}
Topic archived. No new replies allowed.