-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedListAddMiddle.cpp
219 lines (177 loc) · 5.08 KB
/
LinkedListAddMiddle.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
//
// main.cpp
// LinkedList - addMiddle
//
// Created by David P. Lopez on 4/10/15.
// Copyright (c) 2015 David P. Lopez. All rights reserved.
//
#include <iostream>
using namespace std;
const int SIZE = 35;
struct Entry
{
char name[SIZE];
int age;
Entry *next;
};
Entry* getNewEntry()
{
char name[SIZE];
cout << "Enter a new name or press ENTER to quit.";
cin.getline(name,SIZE);
if(strlen(name) == 0)
{
cout << "You pressed ENTER" << endl;
return NULL;
}
Entry *newOne = new Entry;
strcpy(newOne->name, name);
cout << "Age: ";
cin >> newOne->age;
cin.ignore(1024,'\n');
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->name << " " << e->age << endl;
}
bool addFirst(Entry *newEntry, Entry* &head, Entry* &tail)
{
/* this function takes in a pointer to a node of type Entry
and the function will point newEntry->next to the address of
the node that that the pointer called head is currently being
pointed to. Basically, The pointer of type Entry being passed
to the function will first be added as the new head of the linked list.
It will be pointed to the head instead of its default NULL value.
The pointer designated to head from the old node will then be reassigned and it
will now point to the node passed as newEntry. So now we know
that the head node's pointer points to the head, and the old head
node and its next pointer will now point to the new node which is the
new head of the linked list.
*/
if (newEntry == NULL)//a node was not passed into the function
return false;//therefore head points to the last node entered as the head of the list
else
{
if(head == NULL)
{
head = newEntry;
tail = newEntry;
}
else
{
newEntry->next = head;
head = newEntry;
}
}
return true;
}
bool addLast(Entry *newEntry, Entry* &head, Entry* &tail)
{
if(newEntry == NULL)
return false;
else
{
Entry *current;
current = head;
if(head == NULL)
{
head = newEntry;
tail = newEntry;
}
else
{
tail->next = newEntry;
tail = newEntry;
}
}
return true; //this return tells the buildList function to keep creating nodes.
}
bool addAnywhere(Entry* &previous, Entry* newEntry, Entry* &head, Entry* &tail)
{
if(newEntry == NULL)
return false;
else
{
if(previous == NULL)
{
addFirst(newEntry, head, tail);
}
else if(previous == tail)
{
addLast(newEntry, head, tail);
}
else
{
Entry *next = previous->next;
previous->next = newEntry;
newEntry->next = next;
}
return true;
}
}
void traverseList(Entry* &head, Entry* &tail, Entry *newEntry)
{
Entry *current;
Entry *previous;
bool found = false;
current = head;
previous = NULL;
while((current != NULL) && !found)
{
if(newEntry->age >= current->age)
found = true;
else
{
previous = current;
current = current->next;
}
}
if(!addAnywhere(previous, newEntry, head, tail))
{
cout << "Failed to addAnywhere!" << endl;
found = false;
}
}
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
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);
}
int main() {
// new pointer of type Entry that points to the data in a
// new node whose pointer points to null
Entry *listTail = NULL;
Entry *listHead = buildList(listTail);
displayList(listHead);
//get an entry and add it
Entry *newOne = getNewEntry();
if(newOne)
{
traverseList(listHead, listTail, newOne);
}
displayList(listHead);
return EXIT_SUCCESS;
}