-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsgLLDEL.cpp
More file actions
147 lines (137 loc) · 2.22 KB
/
sgLLDEL.cpp
File metadata and controls
147 lines (137 loc) · 2.22 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
//deletion
#include<stdlib.h>
#include<iostream>
#define null 0
using namespace std;
struct node
{
int data;
node *next;
};
node *first,*temp,*ttemp,*p,*q,*r,*ptemp,*pttemp,
*ntemp,*nttemp;
void init()
{
first=temp=ttemp=null;
}
void addnode(int val)
{
temp=first;
while(temp->next!=null)
{
temp=temp->next;
}
ttemp=new node;
ttemp->data=val;
ttemp->next=null;
temp->next=ttemp;
}
void createfirst(int val)
{
first=new node;
first->data=val;
first->next=null;
}
void disp()
{
temp=first;
while(temp!=null)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
void deletion()
{
temp=first;
first=first->next;
temp->next = null;
delete temp;
}
void del_after(int x)
{
temp=first;
while(temp->data!=x)
{
temp=temp->next;
}
ttemp=temp->next;
p=ttemp->next;
temp->next=p;
ttemp->next=null;
delete ttemp;
}
/*void del_before(int y)
{
temp=first;
while(temp->next->data!=y)
{
ttemp=temp;
temp=temp->next;
}
p=temp->next;
ttemp->next=p;
temp->next=null;
delete temp;
}*/
void del_before(int y)
{
// Edge case: list is empty or has only one node
if (first == null || first->next == null) return;
// Edge case: y is at head — no node before it
if (first->data == y) return;
temp = first;
ttemp = null;
// Traverse to find node before the one with data y
while (temp->next != null && temp->next->data != y)
{
ttemp = temp;
temp = temp->next;
}
// If y not found, or y is at head, return
if (temp->next == null) return;
// Now temp is the node before y
if (ttemp == null)
{
// We're deleting the head
first = temp->next;
}
else
{
ttemp->next = temp->next;
}
delete temp;
}
void del_last()
{
temp=first;
while(temp->next!=null)
{
ttemp=temp;
temp=temp->next;
}
ttemp->next=null;
delete temp;
}
int main()
{
init();
createfirst(10);
addnode(20);
addnode(30);
addnode(40);
addnode(50);
disp();
cout<<"\n";
deletion();
disp();
cout<<"\n";
del_after(30);
disp();
cout<<"\n";
del_before(30);
disp();
cout<<"\n";
del_last();
disp();
}