-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbestlkdlst.cpp
206 lines (167 loc) · 3.09 KB
/
bestlkdlst.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
198
199
200
201
202
203
204
205
206
#include<iostream>
#include<conio.h>
#include<malloc.h>
using namespace std;
struct node
{
int data;
struct node*add;
}
*start=NULL,*prev,*temp,*ptr;
void insertbeg();
void insertend();
void insert_spec();
void deletebeg();
void deleteend();
void deletespec();
void display();
int main()
{
int ch;
cout<<" Linked List by YASH LADE \n";
do{
cout<<"\n main menu\n";
cout<<"press 1 for insertion at beginning \n";
cout<<"press 2 for insertion at end\n";
cout<<"press 3 for insertion at specific position\n";
cout<<"press 4 for deletion at beginning\n";
cout<<"press 5 for deletion at end\n";
cout<<"press 6 for deletion at specific position\n";
cout<<"press 7 for display linked list\n";
cout<<"press 8 for exit \n";
cout<<"Enter your choice :";
cin>>ch;
switch(ch)
{
case 1:insertbeg();break;
case 2:insertend();break;
case 3:insert_spec();break;
case 4:deletebeg();break;
case 5:deleteend();break;
case 6:deletespec();break;
case 7:display();break;
case 8:break;
default:cout<<"/wrong choice";
}
}
while(ch!=8);
}
void insertbeg()
{
ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
cout<<"Linked list is empty";
else
{
int no;
cout<<"Enter the number :";
cin>>no;
ptr->data=no;
ptr->add=start;
start=ptr;
}
}
void insertend()
{
ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
cout<<"Linked list is empty";
else
{
int no;
cout<<"Enter the number :";
cin>>no;
ptr->data=no;
ptr->add=NULL;
temp=start;
while(temp->add!=NULL)
{ temp=temp->add;
}
temp->add=ptr;
}
}
void insert_spec()
{
ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
cout<<"Linked list is empty";
else
{ node* data;
int no,i,pos;
cout<<"Enter the position :";
cin>>pos;
cout<<"Enter the number :";
cin>>no;
ptr->data=no;
temp=start;
for(i=1;i<pos;i++)
{
prev=temp;
temp=temp->add;
}
ptr->add=temp;
prev->add=ptr;
}
}
void deletebeg()
{
if(start==NULL)
cout<<"Linked list is empty :";
else
{
cout<<"Deleted elemet is :"<<start->data;
temp=start;
start=start->add;
free(temp);
}
}
void deleteend()
{
if(start==NULL)
cout<<"Linked list is empty :";
else
{
temp=start;
while(temp->add!=NULL)
{
prev=temp;
temp=temp->add;
}
prev->add=NULL;
cout<<"Deleted elemet is :"<<temp->data;
free(temp);
}
}
void deletespec()
{
if(start==NULL)
cout<<"Linked list is empty :";
else
{ int pos,i;
cout<<"Enter the position";
cin>>pos;
temp=start;
for(i=1;i<pos;i++)
{
prev=temp;
temp=temp->add;
}
prev->add=temp->add;
cout<<"Deleted element is :"<<temp->data;
free(temp);
}
}
void display()
{
if(start==NULL)
cout<<"Linked list is empty ";
else
{
temp=start;
while(temp!=NULL)
{cout<<temp->data<<" ";
temp=temp->add;
}
cout<<"\n";
}
}