forked from sukritishah15/DS-Algo-Point
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_nth_node.c
191 lines (174 loc) · 4.26 KB
/
remove_nth_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
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node* head = NULL;
//returns a node with the entered user data
struct node* createNode(int value)
{
struct node *n = (struct node*)malloc(sizeof(struct node));
n->data = value;
n->next = NULL;
return n;
}
//appends a node with the given data, at the end of the linked list
void addNode(int val)
{
struct node *ptr = createNode(val);
if(head==NULL)
{
head = ptr;
head->next = NULL;
}
//continue until we reach the end of the list
else
{
struct node *k = head;
while(k->next!=NULL)
{
k = k->next;
}
k->next = ptr;
}
}
//delete node at nth position
void deleteNodeAt(int pos)
{
struct node* ptr1 = head;
if(head==NULL)
{
printf("The List is empty \n");
return;
}
if(pos==1)
{
head = ptr1->next;
free(ptr1);
}
else
{
//returns if the size of list is one and position entered is greater than given size
if(ptr1->next==NULL)
{
printf("The list size is smaller than the position entered! \n");
return;
}
int i;
//moves the pointer to the (n-1)th node of nth node to be removed
for(i=0; i<pos-2; i++)
{
ptr1 = ptr1->next;
//returns if the position entered is greater than number of nodes in the list
if(ptr1->next==NULL || ptr1==NULL)
{
printf("The list size is smaller than the position entered! \n");
return;
}
}
//points to the nth node to be removed
struct node* ptr2 = ptr1->next;
ptr1->next = ptr2->next;
free(ptr2);
}
}
void printList()
{
if(head==NULL)
printf("The list is empty\n");
else
{
printf("List: ");
struct node* p = head;
while(p!=NULL)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
}
int main()
{
int choice, num, p;
while(1)
{
printf(" 1. Append node at the end of list\n");
printf(" 2. Display the Linked list\n");
printf(" 3. Delete nth Node\n");
printf(" 4. Exit\n");
printf("Enter the operation you want to perform: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the data of node: ");
scanf("%d",&num);
printf("\n");
addNode(num);
break;
case 2:
printList();
break;
case 3:
p;
printf("\nEnter the node position to be deleted: ");
scanf("%d",&p);
deleteNodeAt(p);
break;
case 4:
exit(0);
default:
printf("Invalid choice!!\n");
}
}
return 0;
}
/*
Program to remove nth node of a linked list.
Sample I/O
1. Append node at the end of list
2. Display the Linked list
3. Delete nth Node
4. Exit
Enter the operation you want to perform: 1
Enter the data of node: 10
1. Append node at the end of list
2. Display the Linked list
3. Delete nth Node
4. Exit
Enter the operation you want to perform: 1
Enter the data of node: 20
1. Append node at the end of list
2. Display the Linked list
3. Delete nth Node
4. Exit
Enter the operation you want to perform: 1
Enter the data of node: 30
1. Append node at the end of list
2. Display the Linked list
3. Delete nth Node
4. Exit
Enter the operation you want to perform: 2
List: 10 20 30
1. Append node at the end of list
2. Display the Linked list
3. Delete nth Node
4. Exit
Enter the operation you want to perform: 3
Enter the node position to be deleted: 2
1. Append node at the end of list
2. Display the Linked list
3. Delete nth Node
4. Exit
Enter the operation you want to perform: 2
List: 10 30
1. Append node at the end of list
2. Display the Linked list
3. Delete nth Node
4. Exit
Enter the operation you want to perform: 4
Time Complexity : O(n)
*/