-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathLinkedList
230 lines (224 loc) · 3.88 KB
/
LinkedList
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
#include <iostream>
using namespace std;
// create a node using pointer
// pointer
struct node
{
int data;
node *next;
};
// creating nodes
class linked_list
{
protected:
node *head,*tail;
static int objects ;
public:
linked_list()
{
head = NULL;
tail = NULL;
}
node* gethead()
{
return head;
}
// front of linked list
void front(int n)
{
node *tmp = new node;
tmp -> data = n;
tmp -> next = head;
head = tmp;
objects++;
}
// abcksa fadsf paksdjlf kn
// alskdf lhasldkf
void after(node *a, int value)
{
node* p = new node;
p->data = value;
p->next = a->next;
a->next = p;
objects++;
}
void last(int n)
{
node *tmp = new node;
tmp->data = n;
tmp->next = NULL;
if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
objects++;
}
void display_loop()
{
node *tmp;
tmp = head;
while (tmp != NULL)
{
cout << tmp->data << endl;
tmp = tmp->next;
}
cout << "NULL\n" << endl;
}
static void display_rec(node *head)
{
if(head == NULL)
{
cout << "NULL" << endl;
}
else
{
cout << head->data << endl;
display_rec(head->next);
}
}
void del(node *before_del)
{
node* temp;
temp = before_del->next;
before_del->next = temp->next;
delete temp;
objects--;
}
void del_first()
{
if (head)
{
node* temp = head;
head = head->next;
delete temp;
}
objects--;
}
void del_last()
{
node* prev;
node* curr;
if (head==NULL)
{
// empty list
}
else if (head->next==NULL)
{
curr = head;
head = NULL;
delete curr;
}
else
{
curr = head;
while(curr->next != NULL)
{
prev = curr;
curr = curr->next;
}
prev->next = NULL;
delete curr;
}
objects--;
}
virtual void PUSH(int )
{
cout<<"PUSH main";
}
virtual void POP()
{
cout << "POP main";
}
static void size( )
{
cout<<"The number of elements(size):" << objects << endl << endl ;
objects=0;
}
};
template <typename T>
class LinkStack:public linked_list
{
public:
void PUSH(T n)
{
node *tmp = new node;
tmp -> data = n;
tmp -> next = head;
head = tmp;
objects++;
}
void POP()
{
if (head)
{
node* temp = head;
head = head->next;
delete temp;
}
objects--;
}
};
template <typename X>
class LinkQueue:public linked_list
{
public:
void PUSH(X n)
{
node *tmp = new node;
tmp->data = n;
tmp->next = NULL;
if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
objects++;
}
void POP()
{
if (head)
{
node* temp = head;
head = head->next;
delete temp;
}
objects--;
}
};
int linked_list::objects=0;
int main()
{
linked_list *a;
LinkStack <int>s;
a=&s; //Polymorphism
a->last(1);
a->last(2);
a->front(3);
a->front(4);
a->PUSH(2);
a->POP();
a->POP();
a->PUSH(3);
linked_list::display_rec(a->gethead());
a->size();
LinkQueue <int>q;
a=&q; //This is another list for Queue using linked list.
a->last(1);
a->front(1);
a->PUSH(3);
a->POP();
linked_list::display_rec(a->gethead());
a->size();
return 0;
}