-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.c
134 lines (124 loc) · 2.99 KB
/
5.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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node {
int data;
struct node *link;
};
typedef struct node *NODE;
NODE getnode();
NODE insert_front(NODE);
NODE delete_front(NODE);
NODE insert_rear(NODE);
NODE delete_rear(NODE);
void display(NODE);
void main() {
NODE head;
int option;
//clrscr();
head = getnode();
head->link = head;
while (1) {
printf("\nCircular linked list operations\n");
printf("1. Insert to rear\n2. Insert to front\n3. Delete from front\n4. Delete from rear\n5. Display\n6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &option);
switch (option) {
case 1:
head = insert_rear(head);
getch();
clrscr();
break;
case 2:
head = insert_front(head);
getch();
clrscr();
break;
case 3:
head = delete_front(head);
getch();
clrscr();
break;
case 4:
head = delete_rear(head);
getch();
clrscr();
break;
case 5:
display(head);
getch();
clrscr();
break;
case 6:
exit(0);
default:
printf("Invalid choice\n");
}
}
}
NODE getnode() {
NODE temp;
temp = (NODE)malloc(sizeof(struct node));
return temp;
}
NODE insert_front(NODE head) {
NODE temp, first;
temp = getnode();
printf("Enter the data: ");
scanf("%d", &temp->data);
printf("%d is successfully inserted\n", temp->data);
temp->link = head->link;
head->link = temp;
return head;
}
NODE delete_front(NODE head) {
NODE ptr;
if (head->link == head) {
printf("No nodes\n");
return head;
}
ptr = head->link;
printf("%d is successfully deleted\n", ptr->data);
head->link = ptr->link;
free(ptr);
return head;
}
NODE insert_rear(NODE head) {
NODE ptr, temp;
ptr = head->link;
while (ptr->link != head)
ptr = ptr->link;
temp = getnode();
printf("Enter the data: ");
scanf("%d", &temp->data);
printf("%d is successfully inserted\n", temp->data);
ptr->link = temp;
temp->link = head;
return head;
}
NODE delete_rear(NODE head) {
NODE ptr, prev;
if (head->link == head) {
printf("No nodes\n");
return head;
}
ptr = head->link;
while (ptr->link != head) {
prev = ptr;
ptr = ptr->link;
}
printf("%d is successfully deleted\n", ptr->data);
prev->link = head;
free(ptr);
return head;
}
void display(NODE head) {
NODE temp;
if (head->link == head)
printf("No nodes\n");
else {
for (temp = head->link; temp->link != head; temp = temp->link)
printf("%d=>", temp->data);
printf("%d", temp->data);
}
}