This repository was archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist_c
More file actions
290 lines (249 loc) · 7.84 KB
/
linkedlist_c
File metadata and controls
290 lines (249 loc) · 7.84 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;
class LLNode //Class LLNode represents a single node
{
private:
int data; //data is the value of the element which is to be stored in each node of the linked list
public:
int getData() //As data is private, for using it in another class or main defining functions to get and set value
{
return data;
}
void setData(int new_data)
{
data = new_data;
}
};
class Linkedlist :LLNode //Class Linkedlist represents the collection of nodes and
//includes all the behavior which can be carried over nodes and linked-list
{
private:
int size; //This is the maximum size of the linked-list
int cur_size; //this is the current size of the linked list based on number of nodes inserted
int cur; //current value of the last node index
public:
LLNode Llist[100000]; //created an array of objects of nodes
Linkedlist(int new_size) //constructor which initializes value of size variable and allocated memory
{
size = new_size;
cur_size = 0;
cur = -1;
cout << "Memory allocated, Constructor called!!" << endl;
}
~Linkedlist() //destructor which deallocates memory
{
cout << "Memory is free, destructor called!!" << endl;
};
int getCurrent() //to get and set value of the current size of the linked-list as it is a private variable
{
return cur_size;
}
void setCurrent(int new_current)
{
cur_size = new_current;
}
void insertNode(LLNode newnode) //adding or inserting a node to the existing linked-list at the end
{
if (cur_size < size)
{
Llist[cur + 1] = newnode;
cur_size += 1;
cur += 1;
cout << "Note: Element added at " << cur << endl;
}
else //if the current size reaches its maximum value i.e size then we should not allow to insert
//an element further
{
cout << "Error: Out of bound!!" << endl;
}
}
void shiftRight(int position) //this function handles shifting nodes to right by one
{
for (int i = cur_size; i >= position; i--)
{
Llist[i + 1] = Llist[i];
}
}
void insertNode(LLNode newnode, int position) //this function inserts node at a desired position
//but only within the current size
{
if (cur_size < size && position < cur_size && position >= 0) //insert is successful only if current size is less than
//maximum size and the position where it has to be inserted
{ //has to be less than current size
shiftRight(position); //calling the right shift function
Llist[position] = newnode;
cur_size += 1;
cur += 1;
cout << "Note: Element added at" + position << endl;
}
else {
cout << "ERror: Please provide valid index, "
"non-negative and less than the current linked-list size of " << cur_size << endl;
}
}
void printArray() //this will print the current size of the linked list and all the nodes with element
{
cout << "Current size of the linked-list is: " << cur_size << endl;
for (int j = 0; j<cur_size; j++)
{
cout << "Node " << (j) << ": " << Llist[j].getData() << endl;
}
}
Linkedlist& operator = (const Linkedlist &a) //this is operator overloading, which assigns one linked list to another, thus
{ //copying all the nodes of source linked list to destination one
for (int i = 0; i < a.cur_size; i++)
{
this->insertNode(a.Llist[i]);
}
cout << "New linked list created successfully" << endl;
return *this;
}
void nextNode(LLNode newnode) //this provides the element next to the desired element
{
int position = nodeSearch(newnode.getData());
if (position == -1) { cout << "No such element" << endl; }
else if (position == cur_size - 1){ cout << "No next element to display" << endl; }
else { cout << "The next element is " << Llist[position + 1].getData() << endl; }
}
void shiftLeft(int position) //this function shifts the nodes to left by one
{
for (int i = position; i <= cur_size; i++)
{
Llist[i] = Llist[i + 1];
}
}
void deleteNode(LLNode newnode) //this will delete the element and hence node, updates current size of the linked-size
{
int position = nodeSearch(newnode.getData());
if (position == -1) { cout << "No such element" << endl; }
else
{
shiftLeft(position);
cur_size -= 1;
cur -= 1;
cout << "Note: Deleted node " << position << " successfully" << endl;
}
}
int nodeSearch(int data) //this will search the node where the desired element is present and returns the index or -1 if not foud
{ //also it give the time consumed to search the element
if (cur_size == 0)
{
return -1;
}
else
{
float start = clock();
for (int j = 0; j < size; j++)
{
if (Llist[j].getData() == data)
{
cout << endl << "Node number of the Element is: " << j << endl;
start = (clock() - start) / CLOCKS_PER_SEC;
cout << "Time taken to search the element: " << start << " secs" << endl;
return j;
}
//else if (j >= size) { cout << "Element Not Found" << endl; return -1; }
}
cout << "Element Not Found" << endl;
start = (clock() - start) / CLOCKS_PER_SEC;
cout << "Time taken to search the element: " << start << " secs" << endl;
return -1;
}
}
};
int main()
{
cout << "Hello, Welcome to the Linked-list program!" << endl <<
"Please Provide the maximum size of the linked-list: ";
int size;
cin >> size; //maximum size of linked list taken as input from user
check: if (size <= 0 || size>100000) //max size check that it should be valid
{
cout << "Please specify valid size limit here: ";
cin >> size;
goto check;
}
Linkedlist list1(size); //declared object of linkedlist class
int user_element;
int index;
LLNode a1; //object of type LLNode is declared
for (int kk = 0; kk<size; kk++)
{
a1.setData(1000 + kk);
list1.insertNode(a1);
}
ask: cout <<endl<<endl<< "Please select one of the options to continue" << endl <<
"1: Add a node" << endl <<
"2: Add a node at a particular index" << endl <<
"3: Delete a node" << endl <<
"4: Print nodes" << endl <<
"5: Next node" << endl <<
"6: Copy the linked list into another" << endl <<
"7: Search value" << endl <<
"8: Exit program" << endl;
int answer;
cin >> answer; //this will allow user to select the next operation to be performed
switch (answer) //based on the option selected, we call corresponding function
{
case 1: cout << "Please provide the element to insert: "; //asking user to insert element in the first node
cin >> user_element;
a1.setData(user_element);
list1.insertNode(a1);
goto ask;
case 2:
{
cout << "Please provide the element to insert and the index number respectively: " << endl;
cin >> user_element;
cin >> index;
LLNode a2;
a2.setData(user_element);
list1.insertNode(a2, index);
goto ask;
}
break;
case 3:
{
cout << "Please provide the element to be deleted: ";
cin >> user_element;
a1.setData(user_element);
list1.deleteNode(a1);
goto ask;
}
break;
case 4: {list1.printArray(); goto ask; }
break;
case 5:
{
cout << "Enter the element of whose next node is desired: ";
cin >> user_element;
a1.setData(user_element);
list1.nextNode(a1);
goto ask;
}
break;
case 6:
{
Linkedlist list2(list1.getCurrent());
list2 = list1;
list2.printArray();
goto ask;
}
break;
case 7:
{
int input;
cout << "Enter the value to search in the linked-list: ";
cin >> input;
list1.nodeSearch(input);
goto ask;
}
break;
case 8: cout << "Good Bye!" << endl;
break;
default: {cout << "Please provide a valid option" << endl; goto ask; }
break;
}
return 0;
}