forked from fineanmol/Hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueUsingLinkedList.cpp
105 lines (94 loc) · 1.63 KB
/
QueueUsingLinkedList.cpp
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
//Program in C for implementation of Queue Using Linked List.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};//to make a newnode
struct node *newnode, *temp;
struct node *front=0; //front points first node
struct node *rear=0;//rear should point last node
void enqueue(int x)
{
//struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=x;
newnode->next=0;
//check whether the queue is empty or not
if(front==0 && rear==0 ) //if empty
{
front=rear=newnode;
}
else //add new node at begining
{
rear->next=newnode;
rear=newnode;
}
}
void dequeue()
{
temp=front;
if(front==0 && rear==0 )
printf("\nQueue is Empty!!");
else
{
printf("\nElement Deleted : %d", temp->data);
front = front->next;
}
}
void display()
{
temp=0;
if(rear==0 && front==0) //if the queue is empty
{
printf("Queue is Empty!!");
}
else
{
temp=front;
while(temp!=0)
{
printf("%d\t",temp->data);
temp=temp->next;
}
}
}
int main()
{
int ch,d,i;
do
{ printf("\nMenu");
printf("\n1.Insert the elements\n2.Delete an element\n3.Display the list\n4.Exit\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("Enter no of element to be entered:");
scanf("%d",&d);
int inp[d];
printf("Enter %d elements: \n",d);
for(i=0;i<d;i++)
{
scanf("%d",&inp[i]);
enqueue(inp[i]);
}
break;
}
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exit Program!");
break;
default:
printf("Invalid Choice");
break;
}
}while(ch!=4);
return 0;
}