Repository navigation
Expand file tree
/
Copy pathA7_SLLqueue.c
More file actions
110 lines (71 loc) · 1.48 KB
/
Copy pathA7_SLLqueue.c
File metadata and controls
110 lines (71 loc) · 1.48 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* Queues using Singly Linked List. */
#include<stdlib.h>
#include<stdio.h>
typedef struct queue {
int data;
struct queue * ptr;
}queue;
queue *front = NULL, *rear = NULL;
queue * createnode() {
queue * newnode = (queue *)malloc(sizeof(queue));
printf("Enter the data element : ");
scanf("%d" , &newnode->data);
newnode->ptr = NULL;
return newnode;
}
void insert() {
if(front == NULL) {
front = createnode();
rear = front;
}
else {
queue * newnode = createnode();
rear->ptr = newnode;
rear = newnode;
}
printf("%d successfully inserted.\n" , rear->data);
}
void delete() {
if (front == NULL)
printf("Queue is empty.\n");
else {
printf("%d successfully deleted.\n" , front->data);
queue * temp = front;
front = front->ptr;
free(temp);
}
}
void display() {
if (front == NULL)
printf("Queue is empty.\n");
else {
queue * strt = front;
printf("Elements of queue are : ");
while(strt != NULL) {
printf("%d ", strt->data);
strt = strt->ptr;
}
printf("\n");
}
}
void main() {
int choice , flag = 1 ;
printf("Queue using Singly Linked List.\n");
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;
}
}
}