forked from satyamvats5/DataStructure_Assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.c
128 lines (127 loc) · 1.96 KB
/
12.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
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
}node;
node *list = NULL;
void ins_beg() {
int x;
scanf("%d",&x);
node *p;
p = (node *)malloc(sizeof(node));
p->data = x;
p->next = list;
list = p;
}
void ins_end() {
int x;
scanf("%d",&x);
node *p,*q;
p = list;
while(p->next != NULL)
p = p->next;
q = (node *)malloc(sizeof(node));
q->data = x;
q->next = NULL;
p->next = q;
}
void any_pos_ins(int n) {
int c = 0;
node *p = list;
while(c == n-1) {
c++;
p = p->next;
}
int x;
scanf("%d",&x);
node *q = (node *)malloc(sizeof(node));
q->data = x;
q->next = p->next;
p->next = q;
}
void del_beg() {
node *p;
p = list;
int x = p->data;
printf("%d is deleted\n",x);
list = p->next;
free(p);
}
void del_end() {
node *p = list;
node *q = p->next;
while(q->next != NULL) {
p = q;
q = q->next;
}
int x = q->data;
printf("%d is deleted\n",x);
p->next = NULL;
free(q);
}
void any_pos_del(int n) {
int c = 0;
node *p = list;
while(c == n-1) {
c++;
p = p->next;
}
node *q = p->next;
int x = q->data;
printf("%d is deleted\n",x);
p->next = q->next;
free(q);
}
void display() {
int c = 0;
node *p = list;
while(p != NULL) {
c++;
int y = p->data;
printf("%d ",y);
p = p->next;
}
printf("\n");
}
int main() {
int n;
int choice;
do {
printf("1)Insert at begining\n2)Insert at end\n3)Insert at any position\n4)Delete at beginging\n5)Delete at end\n6)Delete at any position\n7)Display\n9)Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
ins_beg();
break;
case 2:
ins_end();
break;
case 3:
printf("Enter the position\n");
scanf("%d",&n);
any_pos_ins(n);
printf("\n");
break;
case 4:
del_beg();
break;
case 5:
del_end();
break;
case 6:
printf("Enter the position\n");
scanf("%d",&n);
any_pos_del(n);
printf("\n");
break;
case 7:
display();
break;
case 8:
exit(0);
}
}while(choice!=8);
return 0;
}