-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathlinklist deletion.c
More file actions
200 lines (156 loc) · 3.37 KB
/
linklist deletion.c
File metadata and controls
200 lines (156 loc) · 3.37 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
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
// #include<stdio.h>
// int main()
// {
// int i;
// int a[5];
// printf("Enter elements of array");
// for(i=0;i<6;i++)
// {
// scanf("%d",&a[i]);
// if( a[0]<a[i])
// a[0]=a[i];
// }
// printf("largest element = %d",a[i]);
// return 0;
// }
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
}*head;
void createList(int n);
void deleteNodeAtBeginning(int position);
void deleteNodeAtEnd(int position);
void deleteNodeAtMiddle( int position);
void displayList();
int main()
{
int n,position;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("Enter the position where you want to delete ");
scanf("%d",&position);
if(position<1 || position>n)
printf("Invalid Input");
else if(position==1)
{
deleteNodeAtBeginning(position);
printf("\nData in the list \n");
displayList();
}
else if( position==n)
{
deleteNodeAtEnd(position);
printf("\nData in the list \n");
displayList();
}
else
{
deleteNodeAtMiddle(position);
printf("\nData in the list \n");
displayList();
return 0;
}
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void deleteNodeAtBeginning(int position)
{
struct node *temp;
temp=head;
head=temp->next;
free(temp);
printf("DATA DELETED SUCCESSFULLY\n");
}
void deleteNodeAtEnd(int position)
{
struct node *temp,*prevnode;
temp = head;
while(temp->next != NULL)
{
prevnode=temp;
temp=temp->next;
}
if(temp==head)
{
head=0;
free(temp);
}
else
{
prevnode->next=0;
free(temp);
}
printf("DATA DELETED SUCCESSFULLY\n");
}
void deleteNodeAtMiddle(int position)
{
int i=1;
struct node *nextnode, *temp;
temp=head;
while(i<position-1)
{
temp=temp->next;
i++;
}
nextnode=temp->next;
temp->next=nextnode->next;
free(nextnode);
printf("DATA DELETED SUCCESSFULLY\n");
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}