-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkListQuiz.cpp
216 lines (175 loc) · 6.04 KB
/
LinkListQuiz.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
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
210
211
212
213
214
215
216
// LinkListQuiz.cpp
// LinkedList - Linked Lists Quiz
//
// Created by David P. Lopez on 4/16/15.
// Copyright (c) 2015 David P. Lopez. All rights reserved.
//
#include <iostream>
using namespace std;
const int SIZE = 5;
struct Entry
{
char number[SIZE];
Entry *next;
};
Entry* getNewEntry() //creates a new node when called
{
char number[SIZE];
cout << "Enter a new number or press ENTER to display list and view more Options: ";
cin.getline(number, SIZE);
if (strlen(number) == 0)
{
cout << "You pressed ENTER" << endl;
return NULL;
}
Entry *newOne = new Entry;
strcpy(newOne->number, number);
newOne->next = NULL;
return newOne;
}
// a pointer is passed to this function and this function will print the
// attributes of each node that the pointer being passed to the function
// is pointing to when it is called.
void displayEntry(Entry *e)
{
cout << e->number << endl;
}
bool addLast(Entry *newEntry, Entry* &head, Entry* &tail)
{
if (newEntry == NULL)
return false;
else
{
/*
This creates a pointer to the current node which will traverse each node
until it gets to the end of the list where it will add the new node called
newEntry.
the pointer current will be set equal to the pointer that was passed to the
function which is keeping track of the head of our list.
*/
Entry *current;
current = head;
if (head == NULL)
{
head = newEntry;
tail = newEntry;// 1st node created will be both head and tail
}
else
{
tail = newEntry;//this wires the tail pointer to track the newest Entry as soon as it is created
/*
this while loop checks to see if the next pointer in each node points to another node
the head is the start of the list and it points to another node. the last node points
to NULL and this will be where the newEntry node will be added by pointing the current
node's next pointer to the newEntry node
*/
//traverse the list to get to the end
while (current->next != NULL)
{
current = current->next;
}
//Set the current's *next to the new Entry
current->next = newEntry;//the current node's next pointer is now set to point at the newEntry node passed to function
}
}
return true; //this return tells the buildList function to keep creating nodes.
}
Entry* buildList(Entry* &tail)
{
Entry *listHead = NULL; //start of the list
while (true)
{
Entry *newOne = getNewEntry();//creates a new node every time a user enters data for the new node
//add to end of the list when calling this function
if (!addLast(newOne, listHead, tail))
break;
}
return listHead;
}
void displayList(Entry *list)
{
//a pointer to a list of type Entry is passed into this function and it iterates
// over the list using a new pointer called current which begins its search at
// list. current will continue to iterate over the list until it reaches a NULL
// or a node of nothing. on each iteration current will be passed to the displayEntry
// function and be printed to the screen and it will then point to the next node
// using the nodes pointer to repeat the process until the loop reaches NULL
for (Entry *current = list; current != NULL; current = current->next)
{
displayEntry(current);
}
}
bool removeLast(Entry* &head, Entry* &tail)
{
if (tail == NULL)
return false;
else
{
Entry *toBeDeleted; //creates pointer that will be wired to the last node for deletion
toBeDeleted = tail; //wires toBeDeleted to the tail that will be deleted
if (head == tail)
{
head = NULL;
tail = NULL;
}
else
{
Entry *previous; //pointer to find tail without losing head
previous = head; //pointer start at head
while (previous->next != tail) //as long as the pointer from the current node does not point to data in the tail
previous = previous->next; //then the loop will continue to look for tail
tail = previous; //finds tail
tail->next = NULL;
}
delete toBeDeleted; //deletes tail
}
return true;
}
Entry* search(int num, Entry* &head)
{
Entry *current;
current = head;
while(current != NULL)
{
if(atoi(current->number) == num)
{
return(current); //selected node is found
}
current = current->next; //move to next node to check for num
}
return head;//if not found return head of linked list
}
int main() {
Entry *listTail = NULL;
int deleteLast, searchNum;
// new pointer of type Entry that points to the data in a
// new node whose pointer points to null
Entry *listHead = buildList(listTail);
displayList(listHead);
cout << "Please enter a number to search for inside your List: ";
cin >> searchNum;
while(atoi(search(searchNum, listHead)->number) != searchNum)
{
cout << "The number you entered is not in your List. Please try again: ";
cin >> searchNum;
cout << endl;
}
Entry *found;
found = search(searchNum, listHead);
cout << "The number you searched for is: " << found->number << endl;
cout << "Please press '1' to delete the last node from the list: ";
while(true)
{
cin >> deleteLast;
cout << endl;
if(deleteLast == 1)
{
removeLast(listHead, listTail);
displayList(listHead);
cout << "Please press '1' to delete or '2' to escape: " << endl;
}
else
break;
}
return EXIT_SUCCESS;
}