-
Notifications
You must be signed in to change notification settings - Fork 0
/
LAB_05_CODE.C
131 lines (124 loc) · 2.89 KB
/
LAB_05_CODE.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
//DELETION IN A LINKED LIST
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head;
void insertion() {
struct node *ptr, *temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL) {
printf("Queue Full\n");
}
else {
printf("Enter value to be inserted: ");
scanf("%d", &item);
ptr->data = item;
if(head == NULL) {
ptr->next = NULL;
head = ptr;
printf("Node inserted\n");
}
else {
temp = head;
while(temp->next != NULL) {
temp = temp->next;
}
temp->next = ptr;
ptr->next = NULL;
printf("Node inserted\n");
}
}
}
void deleteAtStart() {
struct node *ptr;
if(head == NULL) {
printf("Stack empty\n");
}
else {
ptr = head;
head = ptr ->next;
free(ptr);
printf("Node deleted\n");
}
}
void deleteAtEnd() {
struct node *ptr, *ptr1;
if(head == NULL) {
printf("Stack empty\n");
}
else if(head -> next == NULL) {
head = NULL;
free(head);
printf("Only node of the list is deleted\n");
}
else {
ptr = head;
while(ptr -> next != NULL) {
ptr1 = ptr;
ptr = ptr-> next;
}
ptr1 -> next = NULL;
free(ptr);
printf("Node deleted");
}
}
void deleteRandom() {
struct node *ptr, *temp;
int loc, i;
printf("Enter the location to be added at: ");
scanf("%d", &loc);
ptr = head;
for(i = 0; i < loc; i++) {
temp = ptr;
ptr = ptr->next;
if(ptr == NULL) {
printf("Cannot delete at this location\n");
return;
}
}
temp->next = ptr->next;
free(ptr);
printf("Deleted Node: %d\n", loc+1);
}
void display() {
struct node *ptr;
ptr = head;
if(head == NULL) {
printf("List empty");
}
else {
printf("List: \n");
while(ptr != NULL) {
printf("%d \n", ptr->data);
ptr = ptr->next;
}
}
}
void main() {
int choice;
while(1) {
printf("Deletion Operations: \n");
printf("\n 1.Insertion\n 2.Delete at beginning\n 3.Delete at random location\n 4.Delete at end\n 5.Display\n 6.Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice){
case 1: insertion();
break;
case 2: deleteAtStart();
break;
case 3: deleteRandom();
break;
case 4: deleteAtEnd();
break;
case 5: display();
break;
case 6: exit(1);
break;
default: printf("Invalid Choice");
}
}
}