-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinked_list.cpp
168 lines (156 loc) · 3.13 KB
/
linked_list.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
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
class LinkedList
{
public:
int count = 0;
Node *head;
LinkedList()
{
head = NULL;
}
void AddItem(int val)
{
Node *new_node = new Node;
new_node->data = val;
new_node->next = NULL;
if (head == NULL)
{
head = new_node;
}
// else, make the new_node the head and its next, the previous
// head
else
{
new_node->data = head;
head = new_node;
}
}
// loop over the list. return true if element found
bool IsInList(int val)
{
Node *temp = head;
while (temp != NULL)
{
if (temp->data == val)
return true;
temp = temp->data;
}
return false;
}
int GetItem(int val)
{
// If the head is to be deleted
if (head->data == val)
{
delete head;
head = head->next;
return val;
}
// If there is only one element in the list
if (head->next == NULL)
{
// If the head is to be deleted. Assign null to the head
if (head->data == val)
{
delete head;
head = NULL;
return val;
}
// else print, value not found
int *ptr = NULL;
return *ptr;
}
// Else loop over the list and search for the node to delete
Node *temp = head;
while (temp->next != NULL)
{
// When node is found, delete the node and modify the pointers
if (temp->next->data == val)
{
Node *temp_ptr = temp->next->next;
delete temp->next;
temp->next = temp_ptr;
return val;
}
temp = temp->next;
}
int *ptr = NULL;
return *ptr;
}
bool IsEmpty()
{
if (Size() == 0)
{
return true;
}
return false;
}
int Size()
{
int count = 0;
Node *cur = head;
while (cur != NULL)
{
count += 1;
cur = cur->next;
}
return count;
}
void display()
{
Node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
void test()
{
cout << "1. Add item \n2. Get Student \n3. Is In List \n4. Check if empty \n5. Size \n6. See next \n7. See at \n8. Reset" << endl;
cout << "Enter your choice: ";
int choice;
cin >> choice;
if (choice == 1)
{
}
else if (choice == 2)
{
}
else if (choice == 3)
{
}
else if (choice == 4)
{
}
else if (choice == 5)
{
}
else if (choice == 6)
{
}
else if (choice == 7)
{
}
else if (choice == 8)
{
}
else
{
cout << "Enter a valid choice";
}
}
int main()
{
test();
return 0;
}