Linked List Program

Here's my objective:

Linked List
In this program, you will implement a number of functions:
1. Insertion of a node to a linked list, by given criteria
2. Deletion of a node on a linked list, by given criteria
3. Search a node on a linked list, by given criteria
Node structure:
Info Field: Name (only one string, for simplicity)
Score (an integer)
Link: a pointer
1. Insertion criteria:
a. The linked list insertion is based on alphabetical order of the given names.
b. If a node on the list has the same name as the one that is to be inserted, then the
existing one is deleted and the new one is inserted.
2. Deletion criterion
a. For a given name from input.txt, delete that name associated node from the linked list.
3. Search criteria:
a. For a given name, search on the linked list if there is a node that has this name.
b. If found, print out the node info: Name and Score. One single line for each
searched node.
c. If not found, print out “not found”.
d. The search process cannot change any info or node on the list.

The program reads from input.txt for actions (insertion, deletion, or search):
%SEARCH, %INSERT, %DELETE, %PRINT, %END
The “PRINT” command directs the program to print out information in each and every node
on the linked list, from the first node to the last. The “END” command ends the program
running, but not closing the window.

The file is:
%INSERT
MARK 29
DAVID 21
JOHN 44
JOHN 51
LARRY 39
MARK 21
DAVID 18
JOHN 28
MARK 35
DONALD 41
PHIL 26
%PRINT
%DELETE
MARK
DAVID
%PRINT
%SEARCH
JONE
DAVID
LARRY
%INSERT
LARRY 13
GARY 15
GARY 42
%PRINT
%INSERT
TERRY 23
%DELETE
GARFIELD
%SEARCH
PHIL
%PRINT
%END

I can't get my main program to work properly. Any ideas? Here's my code 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
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
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;

class LinkedList
{
private:	
	struct Node
	{
		string name;
		int number;	
		struct Node *next;
	};


	Node *head;

public:
	LinkedList()
		{head = NULL; }
	void addLast(string, int);
	void Insert(string, int);
	void Delete(string, int);
	void Display() const;
};

void LinkedList::addLast(string data1, int data2)
{
	Node *newNode;
	Node *nodePtr;
	
	newNode = new Node;
	newNode->name = data1;
	newNode->number = data2;
	newNode->next = NULL;

	if (!head)
		head = newNode;
	else
	{
		nodePtr = head;
		while (nodePtr->next)
			nodePtr = nodePtr->next;
		nodePtr->next = newNode;
	}
}

void LinkedList::Insert(string data1, int data2)
{
	int comp;
	Node *newNode;
	Node *nodePtr;
	Node *prevNode = NULL;

	newNode = new Node;
	newNode->name = data1;
	newNode->number = data2;
	
	if(!head)
	{
		head = newNode;
		newNode->next = NULL;
	} 
	else
	{
		nodePtr = head;
		prevNode = NULL;
	
		while((nodePtr != NULL) && (nodePtr->name.compare(data1) < 0))
		{
			prevNode = nodePtr;
			nodePtr = nodePtr->next;
		}

		if(prevNode == NULL)
		{
			head = newNode;
			newNode->next = nodePtr;
		}
		else 
		{
			prevNode->next = newNode;
			newNode->next = nodePtr;
		}
	}
}

void LinkedList::Delete(string data1, int data2)
{
	int comp;
	Node *nodePtr;
	Node *prevNode;
	
	if(!head)
		return;

	
	if(nodePtr->name.compare(data1) == 0)
	{
		nodePtr = head-> next;
		delete head;
		head = nodePtr;
	}
	else
	{
		nodePtr = head;
		while((nodePtr != NULL) && (nodePtr->name.compare(data1) != 0))
		{
			prevNode = nodePtr;
			nodePtr = nodePtr-> next;
		}

		if(nodePtr)
		{
			prevNode->next = nodePtr->next;
			delete nodePtr;
		}
	}
}		

void LinkedList::Display() const
{
	Node *nodePtr;
	nodePtr = head;

	while(nodePtr)
	{
		cout << nodePtr->name << nodePtr->number << endl;
		nodePtr = nodePtr->next;
	}
}

int main()
{
	ifstream inFile;
	const int SIZE = 51;
	string nm, info[SIZE], input, num;
	char ch;
	int i = 0;

	inFile.open("/home/sgilbert/Documents/scores.txt");
	if(!inFile)
	{
		cout << "Error! File cannot open." << endl;
		return 0;
	}
	else
		cout << "File has been opened." << endl;

	inFile >> input;
	while(!inFile.eof())
	{
		if (input == "%INSERT")
		{
			while(input != "%DELETE")
			{
				inFile >> nm;
				inFile >> num;
				cout << nm << num << endl;
			}
		}
		else if (input == "%DELETE")
		{
			cout << "delete" << endl;
		}
		else if (input == "%SEARCH")
		{
			cout << "search" << endl;
		}
		else if (input == "%PRINT")
			cout << "print" << endl;
		else if (input == "%END")
			return 0;

	cout << "File has been closed." << endl;
	return 0;


When I run the program, I get "%PRINT0" in an infinite loop. By the way, I've only started with the insert function, because I'm working on the program piece by piece. The rest of the instructions are just being read out to make sure that the program is reading them in correctly. Also, when I read in the file, no matter if I use getline or just >>, my program skips over the first %INSERT. It doesn't recognize it and instead skips to the else statement for that particular line, but it does recognize the other %INSERTs in the file.
Last edited on
//cppluvr88
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;

class LinkedList
{
private:
struct Node
{
string name;
int number;
struct Node *next;
};


Node *head;

public:
LinkedList()
{head = NULL; }
void addLast(string, int);
void Insert(string, int);
void Delete(string, int);
void Display() const; //ABHI: What is the need to put const??
};

void LinkedList::addLast(string data1, int data2)
{
Node *newNode;
Node *nodePtr;

newNode = new Node;
newNode->name = data1;
newNode->number = data2;
newNode->next = NULL;

if (!head)
head = newNode;
else
{
nodePtr = head;
while (nodePtr->next)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
}
void LinkedList::Insert(string data1, int data2)
{
int comp;
Node *newNode;
Node *nodePtr;
Node *prevNode = NULL;

newNode = new Node;
newNode->name = data1;
newNode->number = data2;

if(!head)
{
head = newNode;
newNode->next = NULL;
}
else
{
nodePtr = head;
prevNode = NULL;

while((nodePtr != NULL) && (nodePtr->name.compare(data1) < 0))
//ABHI: Should not it be nodePtr->next??
{
prevNode = nodePtr;
nodePtr = nodePtr->next;
}

if(prevNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else
{
prevNode->next = newNode;
newNode->next = nodePtr;
}
}
}
void LinkedList::Delete(string data1, int data2)
//ABHI:What is the purpose of data2??
{
int comp;
Node *nodePtr;
Node *prevNode;
//ABHI:Assigned both the pointers to NULL.

if(!head)
return;

//ABHI:nodePtr has not been assigned to any location.
//ABHI:How it can be directly used??
if(nodePtr->name.compare(data1) == 0)
{
nodePtr = head-> next;
delete head;
head = nodePtr;
}
else
{
nodePtr = head;
//ABHI:Same comment here. It should be nodePtr->next.
while((nodePtr != NULL) && (nodePtr->name.compare(data1) != 0))
{
prevNode = nodePtr;
nodePtr = nodePtr-> next;
}

if(nodePtr)
{
prevNode->next = nodePtr->next;
delete nodePtr;
}
}
}

void LinkedList::Display() const
{
Node *nodePtr;
nodePtr = head;

while(nodePtr)
{
cout << nodePtr->name << nodePtr->number << endl;
nodePtr = nodePtr->next;
}
}
int main()
{
ifstream inFile;
const int SIZE = 51;
string nm, info[SIZE],num;// input, num;
string input;
char ch;
int i = 0;

inFile.open("/home/abhishek/view/sample_abhi/cpluplus/cppluvr8.txt");
if(!inFile)
{
cout << "Error! File cannot open." << endl;
return 0;
}
else
cout << "File has been opened." << endl;

getline(inFile,input);
while(!inFile.eof())
{
if (input == "%INSERT")
{
while(
(input != "%DELETE") && (input != "%SEARCH") && (input != "%PRINT") && (input != "%END")
)
//ABHI: input is not getting changed. It is constant. Hence the trouble.
//ABHI: only nm and num are getting changed. So there should be a switch
//ABHI: case statement.
{
getline(inFile,input);
cout << input << endl;
}
continue;
}
else if (input == "%DELETE")
{
cout << "delete" << endl;
}
else if (input == "%SEARCH")
{
cout << "search" << endl;
}
else if (input == "%PRINT")
cout << "print" << endl;
else if (input == "%END")
return 0;
//cout << "File has been closed." << endl;
// return 0;
getline(inFile,input);
}
check the above program...logic has been modified in the main..and some basic comments ..
Topic archived. No new replies allowed.