-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue_using_single_linked_list.c
More file actions
90 lines (90 loc) · 2.02 KB
/
Copy pathQueue_using_single_linked_list.c
File metadata and controls
90 lines (90 loc) · 2.02 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
#include<stdlib.h>
#include<stdio.h>
struct node{
int data;
struct node *next;
}*front=NULL,*rear=NULL;
void enqueue();
void dequeue();
void print();
void is_empty();
void peek();
void main(){
int op,ele;
while(1){
printf("\n* * * * * * * * *\n");
printf("* 1. Enqueue *\n* 2. Dequeue *\n* 3. Print *\n* 4. Peek *\n* 5. Is empty *\n* 6. Exit *\n");
printf("* * * * * * * * *\n\nEnter your option: ");
scanf("%d",&op);
switch (op)
{
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: print();
break;
case 4: peek();
break;
case 5: is_empty();
break;
case 6: exit(0);
break;
default: printf("\nEnter valid option.\n");
break;
}
}
}
void enqueue(){
struct node *p=(struct node*)malloc(sizeof(struct node));
if(!p){
printf("\nQueue is overflow!\n");
}
else{
printf("Enter data value: ");
scanf("%d",&p->data);
p->next=NULL;
printf("\n%d is enqueued.\n",p->data);
if(front==NULL)
front=rear=p;
else{
rear->next=p;
rear=p;
}
}
}
void dequeue(){
if(front==NULL)
printf("\nQueue is empty!\n");
else{
struct node *temp=front;
printf("\n%d is dequeued.\n",front->data);
front=front->next;
free(temp);
}
}
void print(){
if(front==NULL)
printf("\nQueue is empty!\n");
else{
struct node *temp=front;
printf("\nQueue elements are: ");
while(temp!=NULL){
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
}
void is_empty(){
if(front==NULL)
printf("\nQueue is empty.\n");
else
printf("\nQueue is not empty.\n");
}
void peek(){
if(front==NULL)
printf("\nQueue is empty.\n");
else
printf("\nPeek is %d\n",rear->data);
}