-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.cpp
197 lines (190 loc) · 3.32 KB
/
LinkedList.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
#include<iostream>
using namespace std;
struct node
{
int value;
node *next;
};
class List
{
private:
node *head;
node *current;
int size;
int key;
public:
List()
{ size=0;
head=NULL;
key=0;
}
void insertion()
{
node *temp;
node *temp1;
if(head==NULL)
{
head=new node;
cout<<"Enter The Value Of Node "<<"=";
cin>>head->value;
head->next=NULL;
size++;return;
}
else if(size==1)
{
current=new node;
cout<<endl<<"Enter The Value Of Node#"<<"=";
cin>>current->value;
current->next=head;
head=current;
head->next->next=NULL;
size++;return;
}
else
{
cout<<endl<<"Enter After Which Node You Have To Add a New Node = ";
cin>>key;
temp=head;
while(temp!=NULL)
{
if(temp->value==key)
{
temp1=new node;
cout<<endl<<"Enter The Value Of Node#"<<size+1<<"=";
cin>>temp1->value;
temp1->next=temp->next;
temp->next=temp1;size++;
return;
}
else
{
temp=temp->next;
}
}
}
cout<<"Sorry You Have Entered Wrong Value";
}
void print()
{ node *temp;temp=head;int counter=1;
while(temp!=NULL)
{
cout<<endl<<"Node #"<<counter<<" = "<<temp->value;
temp=temp->next;
counter++;
}
}
void deletion()
{
node *temp;temp=head;int a=0;node *temp1;
cout<<endl<<"Enter After Which Node You Have To Delete a New Node = ";
cin>>key;
while(temp!=NULL)
{
if(size==1 && head->value==key)
{
node *del;
del=head;
head=NULL;
delete del;
return;
}
else if(size>1 && head->value==key)
{
node *del;
temp=temp->next;
del=head;
head=temp;
delete del;
return;
}
if(temp->next->value==key)
{
if(temp->next->next!=NULL)
{node * del;del=temp->next;
temp->next=temp->next->next;
cout<<"temp1->next==temp1";size--;
delete del;
return;
}
else if(temp->next->next==NULL)
{
node *del;del=temp->next;
temp->next=NULL;
cout<<"temp1->next==temp->next";size--;
delete del;
return;
}
}
temp=temp->next;
}
}
void emptiness()
{
if(head==NULL)
cout<<"The List is empty"<<endl;
else
cout<<"The List is not empty"<<endl;
}
void searching()
{
cout<<endl<<"Enter The Value to Find In Nodes"<<" = ";
cin>>key;
node *temp;temp=head;
while(temp!=NULL)
{
if(temp->value==key)
{
cout<<"Value Found";
return;
}
temp=temp->next;
}
if(temp==NULL)
{
cout<<"Value Not Found";
}
}
};
int main()
{
List linked_list;char opt;char ch;
do
{system("CLS");
cout<<"WELCOME TO LINKED LIST MENU BAR"<<endl;
cout<<"Select Any of the Option you Need ::"<<endl;
cout<<"1 for Insertion "<<endl;
cout<<"2 for Deletion "<<endl;
cout<<"3 for Printing "<<endl;
cout<<"4 for Checking ther list is empty or not "<<endl;
cout<<"5 for Searching "<<endl;
cout<<"6 for exit "<<endl;
cin>>opt;
switch(opt)
{
case '1':
{linked_list.insertion();break;
}
case '2':
{linked_list.deletion();break;
}
case '3':
{linked_list.print();break;
}
case '4':
{linked_list.emptiness();break;
}
case '5':
{linked_list.searching();break;
}
case '6':
{break;break;
}
default:
{
cout<<"Error";break;
}
}
cout<<endl<<"Enter 1 for going back to menu else press any key to exit";
cin>>ch;
}while(ch=='1');
}