Linked list problems

I had linked lists for my assignment and I literally wasn't able to turn in anything because I knew what I had was wrong and didn't want to come here and "cheat" off you guys. Now that the assignment is done and over with, I need help...I'll have my final in 2 weeks and I need to get more familiar with linked lists.

Here's the gist of the whole program:
1. The user will enter a name and a number. That input will be a part of a list.
2. Make sure that the user's input is properly sorted (descending order...meaning, the head value will have the highest number).
3. If the user has inputs with the same number, the most recent input (with the same number) will be sent a node deeper than the second most recent input (with the same number).

BTW, my code isn't exactly like my flunked assignment but I altered it a little.
Here's my code...

header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef TESTER_H
#define TESTER_H

struct List
{
	int number;
	char name[20];
	List* link;
};

class Lister
{
	public:
		Lister();
		~Lister();
		void Add();
		void View();
	private:
		List* head;
};
#endif 


implementation file
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
#include "tester.h"
#include <iostream>
using namespace std;

Lister::Lister()
{
	head = NULL;
}

Lister::~Lister()
{
	List* Current = head;
	while(Current != NULL)
	{
		List* toDel = Current;
		Current = Current->link;
		delete toDel;
	}
}

void Lister::Add()
{
	int number;
	char name[20];

	cout << "\nEnter name: ";
	cin.getline(name, 20, '\n');

	cout << "Enter number: ";
	cin >> number;

	List* Curr = head;
	List* toAdd = new List;
	toAdd->link = NULL;
	strcpy_s(toAdd->name, name);
	toAdd->number = number;

	while(Curr->link != NULL)
		Curr = Curr->link;

	if(head == NULL) //my attempt at adding the first node
	{
		head = toAdd;
	}
	else if(number <= Curr->number) //my attempt at adding a node
	{                                                 //after the last one
		Curr->link = toAdd;
	}
	else if(number > head->number) //my attempt at adding a node
	{                                               //with a number larger than head
		List* temp = toAdd;
		temp->link = head;
		head = temp;
	}
	else //this whole else statement is an "insert node"...well it's supposed to be anyway
	{
		Curr = head;
		while(Curr->link != NULL)
		{
			if(number < Curr->number && number > Curr->link->number)
			{
				List* temp = Curr->link;
				toAdd->link = temp;
				Curr->link = toAdd;
			}
		}
	}
}

void Lister::View()
{
	List* out = head;
	int index = 0;
	if(head == NULL)
		cout << "The list is empty\n";
	else
		while(out ->link != NULL)
		{
			index++;
			cout << index << ". " << out->name << " $" << out->number << '\n';
		}
}


main/driver/test file
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
#include "tester.h"
#include <iostream>
using namespace std;

void menu(int&);
void option(int, Lister&);

int main()
{
	int choice;
	Lister myList;

	do
	{
		menu(choice);

		option(choice, myList);
	}while(choice == 1 || choice == 2);
	
	cout << '\n';
	system("pause");
	return 0;

}

void menu(int& c)
{
	cout << "Choose an operation:\n"
		 << "\t1. Add\n"
		 << "\t2. View\n"
		 << "\t3. Exit\n"
		 << "Enter choice: ";
	cin >> c;
	cin.ignore();
}

void option(int c, Lister& myList)
{
	if(c == 1)
		myList.Add();
	else if(c == 2)
		myList.View();
	else
		return;
}


My code compiles and builds properly but it doesn't run properly. I don't know the problem. I know my code is most likely messed up here and there...perhaps everywhere but I just need to understand this better. Please fix this code.
Last edited on
I got it to work! OMG, I feel soooo coool!!!
Most people here are pro's but I'll post my solution as a reference to newbies (like me) and potential pro's that might suffer for a little mental block!

First, my header file is perfect...
Second, my driver/test/main file is perfect...
Now here comes the issue...

In my add function, I moved lines 41-44 up just above lines 38 and 39. I think that allowed CurrPtr have some sort of actual value...more importanly CurrPtr's link now has NULL.

Then I forgot to place a line that will help terminate my while loop in my view function. I added code]out = out->link[/code]. Then, I replaced the terminating condition from while(out->link != NULL) to while(out != NULL).

However, I am still open for suggestions, corrections, and improvements. Thanks in advance.
Topic archived. No new replies allowed.