-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeletion-LL.cpp
163 lines (148 loc) · 3.45 KB
/
deletion-LL.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
//
// Created by HP on 27-Jan-18.
//
//
// Created by HP on 25-Jan-18.
//
#include <iostream>
using namespace std;
struct node
{
int info ;
struct node *link ;
}*start , *loc ;
void createlist(int data) ;
void display() ;
void delete_first();
void delete_last();
struct node* search_loc(int );
void delete_loc( struct node * );
int main(void)
{
int i , data , n ,ch;
start = NULL ;
cout << "--> Enter the number of nodes\n" ;
cin >> n ;
for(i = 1 ; i <= n ; i++)
{
cout << "--> Enter data to node "<< i <<"\n" ;
cin >> data ;
createlist(data) ;
}
cout << "--> list is is given below\n" ;
display() ;
label:
if( start != NULL ) {
cout << "## enter 1 to delete header node\n";
cout << "## enter 2 to delete last node\n";
cout << "## enter 3 to delete a node at given location\n";
cout << "## enter any other key to exit\n";
}
else
cout << "## list is empty , so enter number greater then 4 to Quit the program\n" ;
cin >> ch ;
switch(ch){
case 1:
delete_first();
goto label;
case 2:
delete_last();
goto label;
case 3:
cout << "\nenter data you want to delete at given location\n" ;
cin >> data ;
loc = search_loc( data );
delete_loc( loc );
goto label;
default:
return 0;
}
}
void delete_last()
{
struct node *ptr;
if(start == NULL)
cout << "underflow condition\n";
else {
ptr = start;
while (ptr->link->link != NULL)
ptr = ptr->link;
ptr->link = NULL;
cout << "--> list after deletion of last node\n " ;
display();
}
}
struct node* search_loc( int data ){
struct node *ptr ;
if(start == NULL)
{ cout << "list is empty \n" ; return start; }
else {
ptr = start;
while ( ptr != NULL && ptr->info != data )
ptr = ptr->link;
if( ptr != NULL )
return ptr;
if( ptr == NULL )
return ptr ;
}
}
void delete_loc(struct node *loc ){
struct node *ptr ;
if( start == loc )
start = start -> link ;
else if( loc == NULL)
cout << "element not found\n";
else if ( start != loc && loc != NULL )
{
ptr = start;
while (ptr->link != loc)
ptr = ptr->link;
ptr->link = ptr->link->link;
}
cout <<"--> list after deletion\n";
display();
}
void delete_first(){
struct node *ptr;
if(start == NULL)
cout << "underflow condition\n" ;
ptr = start -> link;
start = ptr ;
cout <<"--> list after deletion at head\n";
display();
}
void createlist(int data)
{
struct node *temp , *q ;
temp = new(struct node) ;
temp -> info = data ;
temp -> link = NULL ;
if(start == NULL)
{
start = temp ;
}
else
{
q = start ;
while(q -> link != NULL)
{
q = q -> link ;
}
q -> link = temp ;
}
}
void display()
{
struct node *q ;
if(start == NULL)
{
cout << "List is empty!!!! \n" ;
}
q = start ;
while(q != NULL)
{
cout << q -> info << " ";
q = q -> link ;
}
cout << "\n" ;
}