-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueusingCircularlySLL.c
82 lines (74 loc) · 1.96 KB
/
QueueusingCircularlySLL.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
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
typedef struct node *NODE;
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf("out of memory\n");
exit(0);
}
return x;
}
void freenode(NODE x)
{
free(x);
}
NODE insert_rear(int item,NODE last)
{
NODE temp;
temp=getnode();
temp->info=item;
temp->link=NULL;
if(last==NULL)
{
temp->link=temp;
return temp;
}
else{
temp->link=last->link;
last->link=temp;
return temp;
}
}
NODE delete_front(NODE last){
NODE temp;
if(last==NULL){
printf("Queue is empty,Can't delete an item\n");
return last;}
if(last->link==last){ printf("%d deleted from queue\n",last->info); freenode(last); return NULL;}
temp=last->link;
last->link=temp->link;
printf("%d deleted from queue\n",temp->info);
freenode(temp);
return last;}
void display(NODE last){
NODE temp=last;
if (last==NULL){ printf("Queue is empty\n"); return;}
printf("contents of Queue:\n");
temp=last->link;
while(temp!=last){ printf("%d ",temp->info); temp=temp->link;}
printf("%d",temp->info);
printf("\n");}
int main(){
int item,choice;
NODE last=NULL;
while(1){
printf("Enter 1 to insert element\nEnter 2 to delete element\nEnter 3 for display\nEnter any other key to exit\n");
scanf("%d",&choice);
switch(choice){
case 1:printf("Enter item to be inserted\n"); scanf("%d",&item); last=insert_rear(item,last); break;
case 2:last=delete_front(last); break;
case 3:display(last); break;
default:exit(0);}
}
}