-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path40-single_linked_list.cpp
More file actions
167 lines (159 loc) · 3.03 KB
/
Copy path40-single_linked_list.cpp
File metadata and controls
167 lines (159 loc) · 3.03 KB
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
//single link list
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
using namespace std;
struct node
{
int info;
node *link;
};
class list
{
public:
node *first;
int item;
list()
{
first=NULL;
}
void insert_front()
{
node *temp=new node;
temp->info=item;
temp->link=first;
if(first==NULL)
{
first=temp;
return;
}
else
{
first=temp;
}
}
void delete_front()
{
if(first==NULL)
{
cout<<"empty list\n";
return;
}
first=first->link;
}
void insert_rear()
{
node *temp=new node;
node *cur=new node;
temp->info=item;
temp->link=NULL;
if(first==NULL)
{
first=temp;
delete temp;
return;
}
cur=first;
while(cur->link!=NULL)
cur=cur->link;
cur->link=temp;
}
void delete_rear()
{
node *prev=new node;
node *cur=new node;
if(first==NULL)
{
cout<<"empty list\n";
return;
}
cur=first;
prev=NULL;
while(cur->link!=NULL)
{
prev=cur;
cur=cur->link;
}
prev->link=NULL;
delete cur;
}
void search_delete()
{
node *prev=new node;
node *cur=new node;
prev=NULL;
cur=first;
while(cur!=NULL)
{
if(cur->info==item)
{
cout<<"found and deleted"<<endl;
if(cur==first)
{
first=first->link;
return;
}
prev->link=cur->link;
return;
}
prev=cur;
cur=cur->link;
}
cout<<"not found";
}
void display()
{
node *temp=new node;
temp=first;
if(temp==NULL)
cout<<"Empty list";
while(temp!=NULL)
{
cout<<temp->info<<endl;
temp=temp->link;
}
}
};
int main()
{
int choice;
list obj;
for(;;)
{
cout<<"1:insert front \n2:insert rear \n3:delete front \n4:delete rear \n5:display \n6:search and delete \n7.:exit\n";
cout<<"enter choice\n";
cin>>choice;
switch(choice)
{
case 1:
cout<<"enter item\n";
cin>>obj.item;
obj.insert_front();
break;
case 2:
cout<<"enter item\n";
cin>>obj.item;
obj.insert_rear();
break;
case 3:
obj.delete_front();
break;
case 4:
obj.delete_rear();
break;
case 5:
obj.display();
break;
case 6:
cout<<"enter item\n";
cin>>obj.item;
obj.search_delete();
break;
default:
exit(0);
}
}
getch();
return 0;
}