-
Notifications
You must be signed in to change notification settings - Fork 0
/
C++ OOP LinkedList Implementation
184 lines (171 loc) · 2.43 KB
/
C++ OOP LinkedList Implementation
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
#include <iostream>
using namespace std;
class Linklist{
public:
class Node{
int data;
Node*next;
public:
Node(){}
Node(Node*next,int data){
this->data=data;
this->next=next;
}
friend class Linklist;
};
private:
Node*head,*tail;
public:
Linklist(){
head=tail=nullptr;
}
void insertonhead(int val){
if(head==nullptr)
{
Node*temp=new Node;
temp->data=val;
temp->next=NULL;
head=temp;
tail=temp;
}
else {
Node*temp=new Node;
temp->data=val;
temp->next=head;
head=temp;
}
}
void Deletefromhead(){
head=head->next;
}
void Deletefromtail(){
Node*prev=head,*curr=head;
while(curr->next!=nullptr){
prev=curr;
curr=curr->next;
}
prev->next=nullptr;
tail=prev;
}
void insertatail(int value)
{
Node* node = new Node;
node->data=value;
if (head == nullptr)
{
node->next=nullptr;
head=node;
tail=node;
}
else {
node->next=nullptr;
tail->next=node;
tail=node;
}
}
int getsize(){
int count=0;
Node*temp=head;
while(temp!=nullptr){
count++;
temp=temp->next;
}
return count;
}
void getMiddle()
{
Node *current=head;
int middle = getsize();
//cout<<middle;
middle=middle/2;
int x=0;
while(x!=middle)
{
current=current->next;
x++;
}
cout<< current->data<<endl;
}
bool isempty(){
if(head==nullptr){
return true;
}
else
return false;
}
void disp(){
Node*temp=head;
if(head!=nullptr){
while(temp!=nullptr)
{
cout<<"->"<<temp->data;
temp=temp->next;
}
}
}
int getAverage(){
}
void insertAfter(int val,int key){
Node *current = head;
while(current->data!=key){
current=current->next;
}
Node *n = new Node;
n->data=val;
n->next=current->next;
current=n;
}
int getMax(){
Node *current = head;
int max=0;
int s=getsize();
while(current!=nullptr){
if(max<current->data){
max=current->data;
}
current=current->next;
}
return max;
}
int getMin(){
Node *current = head;
int min=0;
int s=getsize();
while(current!=nullptr){
if(min>current->data){
min=current->data;
}
current=current->next;
}
return min;
}
};
int main(){
Linklist l1;
l1.insertonhead(8);
l1.insertonhead(16);
l1.insertonhead(48);
l1.insertonhead(87);
l1.insertonhead(54);
l1.insertonhead(21);
l1.disp();
cout<<endl;
cout<<l1.getsize()<<endl;
cout<<endl;
l1.insertatail(41);
l1.disp();
l1.Deletefromhead();
cout<<endl;
l1.disp();
cout<<endl;
l1.Deletefromtail();
l1.disp();
cout<<endl;
l1.getMiddle();
cout<<endl;
cout<<l1.getMax();
cout<<endl;
cout<<l1.getMin();
system("pause");
return 0;
}