-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.c
87 lines (69 loc) · 1.6 KB
/
6.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
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
struct Node* prev;
} Node;
Node* head = NULL;
void insert(int data) {
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = data;
temp->next = head;
temp->prev = NULL;
if (head != NULL) {
head->prev = temp;
}
head = temp;
}
void delete(int data) {
Node* temp = head;
while (temp != NULL && temp->data != data) {
temp = temp->next;
}
if (temp == NULL) return;
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}
if (temp == head) {
head = temp->next;
}
free(temp);
}
void printList() {
Node* node = head;
while (node != NULL) {
printf(" %d ", node->data);
node = node->next;
}
}
int main() {
int choice, data;
while(1) {
printf("\n1. Insert\n2. Delete\n3. Print\n4. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter the value to be inserted: ");
scanf("%d", &data);
insert(data);
break;
case 2:
printf("Enter the value to be deleted: ");
scanf("%d", &data);
delete(data);
break;
case 3:
printList();
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}