-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_a_node.cpp
More file actions
57 lines (57 loc) · 1.07 KB
/
delete_a_node.cpp
File metadata and controls
57 lines (57 loc) · 1.07 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
#include <stdio.h>
struct node{
int data;
struct node* next;
};
int main()
{
struct node *p,*q,*t,*r;
int x;
p = new node;
printf("Enter the first value: ");
scanf("%d",&p->data);
printf("Enter the next value: ");
scanf("%d",&x);
q = p;
while(x)
{
t = new node;
t->data = x;
q->next = t;
q = q->next;
printf("Enter the next value: ");
scanf("%d",&x);
}
q->next = NULL;
q = p;
printf("First List is : ");
while(q)
{
printf("%d ",q->data);
q=q->next;
}
printf("\n");
int pos;
printf("Which Node You Want To Delete : ");
scanf("%d",&pos);
if(pos == 1){
p = p->next;
}
else{
q = p;r = p;
for(int i = 1;i<=pos-1;i++){
r = q;
q = q->next;
}
r->next = q->next;
}
q = p;
printf("New List is : ");
while(q)
{
printf("%d ",q->data);
q=q->next;
}
printf("\n");
return 0;
}