forked from ccgcv/Cplus-plus-for-hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked List implementation in C++
183 lines (168 loc) · 3.84 KB
/
Linked List implementation in C++
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
/*
LINKED LIST IMPLEMENTATION IN C++
Author: Shourya gupta
*/
#include <iostream>
using namespace std;
struct node{
int data;
node* link;
};
node* head=NULL;
void InsertAtEnd(int d){
node* n = new node();
n->data=d;
n->link=NULL;
if(head==NULL){
head=n;
cout<<endl<<"Element Added!";
}
else{
node* temp=head;
while((temp->link)!=NULL){
temp=temp->link;
}
temp->link=n;
cout<<endl<<"Element Added!";
}
}
void InsertAtBeg(int d){
node* n = new node;
n->data=d;
n->link=head;
head=n;
cout<<endl<<"Element Added!";
}
void DeleteBeg(){
if(head==NULL){
cout<<endl<<"List is empty!";
}
else{
node* ptr=head;
head=ptr->link;
free(ptr);
cout<<endl<<"Element Deleted!";
}
}
void DeleteEnd(){
if(head==NULL){
cout<<endl<<"List is empty!";
}
else{
node* temp=head;
node* prev;
while(temp->link!=NULL){
prev=temp;
temp=temp->link;
}
prev->link=NULL;
free(temp);
cout<<endl<<"Element Deleted!";
}
}
void MidDisplay(){
if(head==NULL){
cout<<endl<<"List is empty!";
}
else{
/* Time Consuming method as 2 times iteration is happening.
int count=0,mid=0;
node* temp=head;
while(temp->link != NULL){
temp=temp->link;
count++;
}
temp=head;
mid=count/2;
for(int i=0; i<mid; i++){
temp=temp->link;
}
cout<<temp->data<<endl;
*/
node* fast=head; //Moves 2 nodes at a time
node* slow=head; //Moves a single node at a time
while(fast!=NULL && fast->link!=NULL){
slow=slow->link;
fast=fast->link;
fast=fast->link; //or instead of writng 2 times, write fast->link->link;
}
cout<<slow->data<<endl;
}
}
void reverse(){
if(head==NULL){
cout<<endl<<"List is empty!";
}
else{
node*prev = NULL;
node* cur = head;
node* next;
while(cur!=NULL){
next=cur->link;
cur->link=prev;
prev=cur;
cur=next;
}
head=prev;
}
}
void display(){
if(head==NULL){
cout<<endl<<"The list is empty.";
}
else{
node* temp=head;
while(temp != NULL){
cout<<temp->data<<"->";
temp=temp->link;
}
cout<<"NULL";
}
}
int main(){
int choice,d;
char ans='y';
while(ans=='y'){
cout<<"1. Display Linked List"<<endl;
cout<<"2. Add an element to the beginning"<<endl;
cout<<"3. Add an element to the end"<<endl;
cout<<"4. Delete the first element"<<endl;
cout<<"5. Delete the last element"<<endl;
cout<<"6. Display the middle element"<<endl;
cout<<"7. Reverse the list"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice){
case 1:
display();
break;
case 2:
cout<<"Enter the value to be added: ";
cin>>d;
InsertAtBeg(d);
break;
case 3:
cout<<"Enter the value to be added: ";
cin>>d;
InsertAtEnd(d);
break;
case 4:
DeleteBeg();
break;
case 5:
DeleteEnd();
break;
case 6:
MidDisplay();
break;
case 7:
reverse();
break;
default:
cout<<"Invalid Choice!";
break;
}
cout<<endl<<"Do you want to choose again(y/n): ";
cin>>ans;
}
}