-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLAB2_Q1.cpp
248 lines (193 loc) · 4.33 KB
/
LAB2_Q1.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
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
#include <iostream>
using namespace std;
class Node
{
public:
//data
int data;
//pointer to the next node
Node * next;
// constructor that makes the pointer to NULL
Node()
{
next = NULL;
}
};
class linkedList
{
;
public:
// head => node type pointer
Node * head;
Node * tail;
//constructor
linkedList()
{
head=NULL;
tail=NULL;
}
// circles inside each other
//rules to link circle
//insert
void insert(int value)
{
Node * temp = new Node;
//Insert value in node
temp->data = value;
//1st node only
if(head==NULL){
head = temp;
}
//any other node
else{
tail->next= temp;
}
tail = temp;
}
void insertAt(int pos, int value){
//reach the lace before pos
Node * current = head;
int i=1;
int c= Count();
if(pos <= c and pos != 1){
while (i<pos-1){
i++;
current = current->next;
}
//create a node
Node *temp = new Node;
temp -> data = value;
//move pointers like magic
temp->next = current->next;
current->next = temp;
}
else if (pos == 1){
Node * temp = new Node;
//Insert value in node
temp->data = value;
temp->next = head;
head = temp;
}
else if (pos==c+1){
insert(value);
}
else{
cout<<"Linked List does not have that many elements." <<endl;
}
//update the code for 1st position
}
//deletion
void delet(){
if(Count()>1){
// before tail has to point to NULL
Node * current = head;
while(current->next->next!=NULL){
current = current->next;
}
Node * temp =current->next;
current->next = NULL;
//update tail
tail = current;
//delete temp
delete temp;
}
else if (Count()==1){
Node * temp = head;
head = NULL;
tail = NULL;
delete temp;
}
}
//deletion at position
void deletAt(int pos)
{
Node * current= head;
Node * previous;
if (pos==1){
head=current->next;
delete current;
}
else if(pos>1 and pos<Count()){
for(int i=1;i<pos-1;i++)
{
current=current->next;
}
previous=current;
current = current->next;
previous->next=current->next;
delete current;
}
else if(pos==Count()){
delet();
}
}
int Count()
{
int i = 0; // Initialize count
class Node* current = head; // Initialize current
while (current != NULL)
{
i++;
current = current->next;
}
return i;
}
//display
void display(){
Node * current = head;
while(current!= NULL){
cout<< current->data<<"->";
current = current->next;
}
cout<<endl;
}
void revDisplay(){
revDisplay2(head);
cout<<endl;
}
void revDisplay2(Node * current){
//move to next (till available)
if(current==NULL) return;
else{
//recursion
revDisplay2(current->next);
// print while returning
cout<<current->data<<"->";
}
}
void revLL(){
Node * temp= head;
revLL2(head);
tail=temp;
temp->next=NULL;
}
void revLL2(Node * current){
//empty list
if(current==NULL) return;
//1 node
else if (current->next==NULL){
head==current;
return;
}
//for rest of elements
else{
revLL2(current->next);
current->next->next=current;
}
}
};
int main()
{
linkedList l1;
l1.insert(4);
l1.insert(5);
l1.insert(6);
l1.insert(7);
l1.insertAt(1,3);
l1.display();
l1.delet();
l1.display();
cout<<"No. of elements is: ";
cout<<l1.Count()<<endl;
return 0;
}