linked list

so i have a program well you are creating a history of classes taken using linked lists and pointers were you prompt the user if they want to add a class including the name term units and grade once all entered i was told that my only problem is that i am not adding the new object to the front of the linked list? when i run the program after the person enters the subject it outputs the just entered data to the beginning of the list but was told that i was doing something wrong please help
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
	using std::cin;
	using std::cout;
	using std::endl;
	using std::ios;

	#include <cstring>
	#include <cstdlib>

	#include <iomanip>
	using std::left;

	class course
	{
	public:
	char className[11]; // Class array
	char term[7]; // Term array
	int units; // Units
	char grade; // Grade
	course* next; // Next node
	};

	// Prototypes
	void printCourse(course*);
	int myStCmp(const char*, const char*);
	int myStrCmp(const char*, const char*, size_t count);
	int courseCmp(const course*, const course*);

	int main()
	{
		cout << endl;
		// set up for new entry
		char classEntry;
	
		// set empty list
		course* head = 0;
		course* cEntry = 0;
	
		while(1)
		{
			// print blank course history 
			printCourse(head);
			cout << endl;
			// ask user if they want to add a course
			cout << "Enter another class to your history? [Y/N]: "; 
			cin >> classEntry;
			cin.ignore(1000, 10);
			cout << endl;
			
			// if user doesnt want to add print course history 
			if (classEntry == 'N' || classEntry == 'n')
			{
				printCourse(head);
				break;
			}

			//
			if (classEntry == 'Y' || classEntry == 'y')
			{
				cEntry = new course;
				cout << "COURSE: Enter your course name [10 Characters Max]: ";
				cin.getline(cEntry->className, 11);
				cout << endl;
				
				cout << "TERM: Enter the term [Ex: fa2011, 6 Characters Max]: ";
				cin.getline(cEntry->term, 7);
				cout << endl;
				
				cout << "UNITS: Enter # of units [1-5]: ";
				cin >> cEntry->units;
				cin.ignore(1000, 10);
				cout << endl;
				
				cout << "GRADE: Enter your grade [letter grade (A->F, or W if incomplete]: ";
				cin >> cEntry->grade;
				cin.ignore(1000, 10);
			
				// create list if the head is empty
				if (head == 0) 
				{
					head = cEntry;
					head->next = 0;
				}

				// compare and arrange in order
				else
				{
					course* x = head;
					
					if (courseCmp(x, cEntry) == 1)
					{
						while (courseCmp(x, cEntry) == 1)
						{
							if (x->next != 0 && courseCmp(x->next, cEntry) != -1)
								x = x->next;

						else
							break;
						} 

					cEntry->next = x->next;
					x->next = cEntry;
					} // End if

					else
					{
						cEntry->next = x;
						head = cEntry;
					}
				} 
			} 
		} 

		// Deallocate memory
		while (head)
		{
			course* n = head->next;
			delete head;
			head = n;
		} 
	
	} 
	// create blank class history to be printed and updated
	void printCourse(course* h)
	{
		if (h)
	
		cout << endl;
		cout << "                CURRENT CLASS HISTORY \n";
		cout << "                --------------------- \n";
		cout << endl;
		cout.setf(ios::left, ios::adjustfield);
		cout << setw(20) << "     COURSE" << setw(10) << " TERM" << setw(10);
		cout << " UNITS" << setw(10) << " GRADE" << endl;
		cout << "     ------          ----      -----     -----" << endl; 

		for(course* i = h; i; i = i->next)
		{
			cout.setf(ios::left, ios::adjustfield);
			cout << "     " << setw(10) << i->className;
			cout << "      " << setw(10) << i->term;
			cout.setf(ios::adjustfield);
			
			cout << setw(3) << i->units;
			cout.setf(ios::right, ios::adjustfield);
			cout << setw(10) << i->grade;
			cout << endl;
			cout << "     ------------|----------|---------|--------" << endl;
		} 


	}
	// course compare function
	int courseCmp(const course* a, const course* b)
	{
		// make sure string is correct length
		if ((strlen(a->term) != 6) || (strlen(b->term) != 6)) // in cstring
			return myStCmp(a->className, b->className);
	
		// if the same 
		if (myStCmp(a->term, b->term) == 0)
			return myStCmp(a->className, b->className);

		// determine year order
		int yearA = atoi(a->term + 2); 
		int yearB = atoi(b->term + 2); 
		
		if (yearA < yearB)
			return -1; 

		if (yearA > yearB)
			return 1; 

		// compare semester in accordance to year
		if (myStrCmp(a->term, "SP", 2) == 0)
			return -1; 

		if (myStrCmp(a->term, "SU", 2) == 0)
			return myStrCmp(b->term, "SP", 2) ? -1 : 1;
		
			return 1;
	} 


	int myStCmp(const char* dst, const char* src)
	{
		int f, l;
		do
		{
			if (((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z')) f -= 'A' - 'a';
			if (((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z')) l -= 'A' - 'a';
		}
	
		while (f && (f == l));
			return(f - l);
	}

	int myStrCmp(const char* dst, const char* src, size_t count)
	{
		int f, l;
		do
		{
			if (((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z')) f -= 'A' - 'a';
			if (((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z')) l -= 'A' - 'a';
		}
		
		while (--count && f && (f == l));
			return (f - l);
	}
Topic archived. No new replies allowed.