-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllLinkedList.cpp
More file actions
200 lines (177 loc) · 3.5 KB
/
AllLinkedList.cpp
File metadata and controls
200 lines (177 loc) · 3.5 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
#include <iostream>
using namespace std;
struct Node{
int data;
Node *next = NULL;
}*front=NULL, *back=NULL;
int size = 0;
Node* pushFront(Node *head,int data){
Node* newNode = new Node;
newNode->data = data;
newNode->next = head;
if(back==NULL){
back = head;
}
head = newNode;
size++;
front=head;
return head;
}
Node* pushEnd(Node* head, int data){
Node* newNode = new Node;
newNode->data=data;
Node* curr = head;
while(curr->next){
curr = curr->next;
}
curr->next = newNode;
newNode->next = NULL;
size++;
back=newNode;
return head;
}
Node* insertMid(Node* head,int data){
Node* newNode = new Node;
newNode->data = data;
int curCount = 0,mid = size/2;
Node* curr = head;
while(curr && curCount!=mid){
curCount++;
curr = curr->next;
}
newNode->next = curr->next;
curr->next = newNode;
size++;
return head;
}
int getMiddle(Node* head){
int curCount = 0;
int mid = size/2;
Node* curr = head;
while(curCount!=mid){
curCount++;
curr = curr->next;
}
return curr->data;
}
int getFront(){
if(front==NULL){
return -1;
}
return front->data;
}
int getBack(){
if(back==NULL){
return -1;
}
return back->data;
}
// Deletion
Node* removeFront(Node* head){
if(!head){
return NULL;
}
Node* curr = head;
head = head->next;
delete curr;
front = head;
size--;
return head;
}
Node* removeBack(Node* head){
if(head==NULL){
return NULL;
}
Node* curr = head;
Node* prev=NULL;
while(curr->next){
prev = curr;
curr = curr->next;
}
delete curr;
prev->next = NULL;
back = prev;
size--;
return head;
}
Node* removeMid(Node* head){
int curCount = 0;
int mid = size/2;
if(mid==0){
delete head;
return NULL;
}
Node* curr = head;
Node* prev=NULL;
while(curCount!=mid){
curCount++;
prev = curr;
curr = curr->next;
}
prev->next = curr->next;
delete curr;
size--;
return head;
}
void printList(Node* head){
while(head){
cout<<head->data;
if(head->next){
cout<<"->";
}
head = head->next;
}
cout<<endl;
}
int main() {
// your code goes here
Node *head = NULL;
head = pushFront(head,10);
head = pushFront(head,20);
head = pushFront(head,30);
head = pushFront(head,40);
printList(head);
cout<<" And Size is "<<size<<endl;
cout<<"Middle Element :"<<getMiddle(head)<<endl;
cout<<"Front :"<<getFront()<<endl;
cout<<"Back : "<<getBack()<<endl;
// Adding in the end
head = pushEnd(head,50);
head = pushEnd(head,60);
head = pushEnd(head,70);
printList(head);
cout<<" And Size is "<<size<<endl;
cout<<"Middle Element :"<<getMiddle(head)<<endl;
cout<<"Front :"<<getFront()<<endl;
cout<<"Back : "<<getBack()<<endl;
// Insert In the middle
head = insertMid(head,41);
head = insertMid(head,42);
head = insertMid(head,43);
printList(head);
cout<<" And Size is "<<size<<endl;
cout<<"Middle Element :"<<getMiddle(head)<<endl;
cout<<"Front :"<<getFront()<<endl;
cout<<"Back : "<<getBack()<<endl;
// Deletion
cout<<"Deletion"<<endl;
head = removeFront(head);
printList(head);
cout<<" And Size is "<<size<<endl;
cout<<"Middle Element :"<<getMiddle(head)<<endl;
cout<<"Front :"<<getFront()<<endl;
cout<<"Back : "<<getBack()<<endl;
head = removeBack(head);
printList(head);
cout<<" And Size is "<<size<<endl;
cout<<"Middle Element :"<<getMiddle(head)<<endl;
cout<<"Front :"<<getFront()<<endl;
cout<<"Back : "<<getBack()<<endl;
head = removeMid(head);
printList(head);
cout<<" And Size is "<<size<<endl;
cout<<"Middle Element :"<<getMiddle(head)<<endl;
cout<<"Front :"<<getFront()<<endl;
cout<<"Back : "<<getBack()<<endl;
return 0;
}