Repository navigation
Expand file tree
/
Copy pathA9b_CLLqueue.c
More file actions
86 lines (75 loc) · 1.6 KB
/
Copy pathA9b_CLLqueue.c
File metadata and controls
86 lines (75 loc) · 1.6 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
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
/* Implement queue using Circular Linked List. */
#include<stdio.h>
#include<stdlib.h>
typedef struct cllqueue {
int data;
struct cllqueue * ptr ;
}cllqueue;
cllqueue * front = NULL , *rear = NULL;
cllqueue * createnode() {
cllqueue * newnode = (cllqueue *)malloc(sizeof(cllqueue));
printf("Enter the element to be inserted : ");
scanf("%d" , &newnode->data);
newnode->ptr = front;
return newnode;
}
void insert() {
if (front == NULL) {
front = createnode();
front->ptr = front;
rear = front;
}
else {
cllqueue * temp = createnode();
rear->ptr = temp;
rear = temp;
}
printf("Element %d successfully inserted.\n" , rear->data);
}
void delete() {
if(front == NULL)
printf("Queue is empty.\n");
else if(front->ptr == front) {
printf("Element %d successfully deleted.\n" , front->data);
front = NULL;
rear = NULL;
}
else {
printf("Element %d successfully deleted.\n" , front->data);
front = front->ptr;
rear->ptr = front;
}
}
void display() {
if(front == NULL)
printf("Queue is empty.\n");
else {
cllqueue * strt = front;
printf("Elements of queue are : ");
do {
printf("%d ",strt->data);
strt = strt->ptr;
}while(strt != front);
printf("\n");
}
}
void main() {
int choice , flag = 1 ;
printf("Enter the operation to be performed.\n");
while(flag) {
printf("1.Insert \t 2.Delete \t 3.Display \t 4.Exit : ");
scanf("%d" , &choice);
switch(choice) {
case 1 : insert();
break;
case 2 : delete();
break;
case 3 : display();
break;
case 4 : flag = 0 ;
break;
default : printf("Invalid input.\n");
break;
}
}
}