This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
04-Deleting node after a given node.c
140 lines (108 loc) · 2.77 KB
/
04-Deleting node after a given node.c
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
// Simply deleting node after a given node from a linked list
#include<stdio.h>
#include<stdlib.h>
struct node *start;
struct node
{
int roll;
struct node *next;
};
struct node * create_linked_list(struct node *);
void display(struct node *);
void delete_node_after(struct node *);
void main()
{
int roll;
printf("Enter negative number to stop\n");
printf("Enter roll number\n");
scanf("%d", &roll);
if(roll < 0)
{
printf("You have canceled\n");
}
else
{
start = (struct node *) malloc(sizeof(struct node));
if(start == NULL)
{
printf("Unable to make linked list due to no space in heap\n");//When there is no free space in heap is left for memory allocation
}
else
{
start -> roll = roll;
create_linked_list(start);
display(start);
delete_node_after(start);
printf("After modifying the list we get:\n");
display(start);
}
}
}
struct node * create_linked_list(struct node *ptr)
{
struct node *new_node;
static int count = 1, roll;
printf("Enter negative number to stop\n");
printf("Enter roll number\n");
scanf("%d", &roll);
if (roll < 0)//if user not want to create list, so here we don not waste any kind of memory
{
goto End;
}
else
{
new_node = (struct node *) malloc(sizeof(struct node));
if(new_node == NULL)
{
printf("No more space in heap\n");//When there is no free space in heap is left for memory allocation
goto End;
}
else
{
ptr -> next = new_node;
ptr = ptr -> next;
ptr -> roll = roll;
count++;
return create_linked_list(ptr);
}
}
End:
ptr -> next = NULL;
printf("Hurray! You have made %d insertion successfully in the linked list\n", count);
}
void display(struct node *ptr)
{
printf("The list is the following...\n");
do
{
printf("%d ", ptr -> roll);
ptr = ptr -> next;
}while (ptr -> next != NULL);
printf("%d ", ptr -> roll);
}
void delete_node_after(struct node * ptr)
{
struct node * preptr;
int value;
printf("\nAfter which value you want to delete the succesive node\n");
scanf("%d", &value);
while (ptr -> next != NULL)
{
if(ptr -> roll == value)
{
break;
}
ptr = ptr -> next;
}
preptr = ptr;
ptr = ptr -> next;
if(ptr == NULL)
{
printf("No element deleted\n");
}
else
{
ptr = ptr -> next;
preptr -> next = ptr;
}
}