-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleLinkedList.cpp
More file actions
193 lines (169 loc) · 3.87 KB
/
SimpleLinkedList.cpp
File metadata and controls
193 lines (169 loc) · 3.87 KB
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
/* Program: Lab 3 - Linked List
Author: Aaron Lewis
Class: CSCI 220 MW
Date: 09/18/2017
Description: This program allows the user to add names
to a linked list of names to either the front or
the back of the list and also remove a name based
on its position in the list.
I certify that the code below is my own work.
Exception(s): N/A
*/
#include <iostream>
#include <string>
#include "string"
using namespace std;
// Linked-list node
struct Node
{
string name;
Node *next;
Node *prev;
};
// Simple Linked-list
struct linked_list
{
Node head;
Node tail;
int size;
};
// Function prototypes
void insert_front(linked_list *l, string n);
void insert_rear(linked_list *l, string n);
bool remove_name(linked_list *l, int e);
const void print_list(linked_list *l, int size);
void print_menu();
int main()
{
cout << "Author: Aaron Lewis\n";
// Create a new linked list for names
linked_list names;
// Assign head and tail to point to each other
names.head.next = &names.tail;
names.head.prev = nullptr;
names.tail.next = nullptr;
names.tail.prev = &names.head;
// Assign 0 to names linked list size
names.size = 0;
// Initialize sentinal variable
bool end = false;
// Initialize input variables
string choice = "";
string new_name;
int name_loc;
// Loop until user chooses to end
while (!end)
{
// Display menu options
print_menu();
// Get user input
cin >> choice;
// If insert new name at front
if (choice == "1")
{
cout << endl << "Enter the new name: ";
cin >> new_name;
insert_front(&names, new_name);
cout << endl;
}
// If insert new name at rear
else if (choice == "2")
{
cout << endl << "Enter the new name: ";
cin >> new_name;
insert_rear(&names, new_name);
cout << endl;
}
// If remove a name
else if (choice == "3")
{
cout << endl << "Enter the number of the name to remove: ";
cin >> name_loc;
while (cin.fail())
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Enter a valid integer between 1 and " << names.size
<< ": ";
cin >> name_loc;
}
remove_name(&names, name_loc);
cout << endl;
}
// If print list
else if (choice == "4")
{
cout << endl;
print_list(&names, names.size);
cout << endl;
}
// If invalid menu choice
else
cout << "Please enter 1, 2, 3 or 4!!" << endl;
}
return 0;
}
// Creates a new node with string n and adjusts pointers to insert at front
// then increments the size
void insert_front(linked_list *l, string n)
{
Node *temp = new Node;
temp->name = n;
temp->prev = &l->head;
temp->next = l->head.next;
l->head.next->prev = temp;
l->head.next = temp;
l->size++;
}
// Creates a new node with string n and adjusts pointers to insert at rear
// then increments the size
void insert_rear(linked_list *l, string n)
{
Node *temp = new Node;
temp->name = n;
temp->prev = l->tail.prev;
temp->next = &l->tail;
temp->prev->next = temp;
l->tail.prev = temp;
l->size++;
}
// Goes to index e in the linked list and removes that node if possible
// then decrements the size. Also, adjusts pointers to skip over the to-be-deleted node.
bool remove_name(linked_list *l, int e)
{
if (e < l->size + 1)
{
Node *cur = &l->head;
for (int x = 0; x < e; x++)
{
cur = cur->next;
}
cur->prev->next = cur->next;
cur->next->prev = cur->prev;
l->size--;
delete cur;
return true;
}
else
return false;
}
// Iterates through the list and prints out the name in each node
const void print_list(linked_list *l, int size)
{
Node *cur = &l->head;
for (int x = 0; x < l->size; x++)
{
cur = cur->next;
cout << x + 1 << ": " << cur->name << endl;
}
}
// Prints the menu
void print_menu()
{
cout << "Menu Options" << endl << endl;
cout << "1 - Insert New Name @ Front" << endl;
cout << "2 - Insert New Name @ Rear" << endl;
cout << "3 - Remove a Name" << endl;
cout << "4 - Print List of Names" << endl;
cout << "5 - End" << endl << endl;
}