Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
17 views
4 pages
10 - Queue With Linked List
Uploaded by
sahilpandey367
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF, TXT or read online on Scribd
Download
Save
Save 10_Queue With Linked List For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
17 views
4 pages
10 - Queue With Linked List
Uploaded by
sahilpandey367
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF, TXT or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save 10_Queue With Linked List For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 4
Search
Fullscreen
1 /**
2 * Queue implementation using linked list in C.
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <limits.h>
8
9
10 #define CAPACITY 100 // Queue max capacity
11
12
13 /* Queue structure definition */
14 typedef struct node
15 {
16 int data;
17 struct node * next;
18 } Queue; // Queue is a typedef of struct node
19
20 /* Queue size */
21 unsigned int size = 0;
22
23
24 int enqueue(Queue ** rear, Queue ** front, int data);
25 int dequeue(Queue ** front);
26 int getRear(Queue * rear);
27 int getFront(Queue * front);
28 int isEmpty();
29 int isFull();
30
31
32 void main()
33 {
34 int ch, data;
35 Queue *rear, *front;
36
37 rear = NULL;
38 front = NULL;
39
40 /* Run indefinitely until user manually terminates */
41 while (1)
42 {
43 /* Queue menu */
44 printf("--------------------------------------------\n");
45 printf(" QUEUE LINKED LIST IMPLEMENTATION PROGRAM \n");
46 printf("--------------------------------------------\n");
47 printf("1. Enqueue\n");
48 printf("2. Dequeue\n");
49 printf("3. Size\n");
50 printf("4. Get Rear\n");
51 printf("5. Get Front\n");
52 printf("0. Exit\n");
53 printf("--------------------------------------------\n");
54 printf("Select an option: ");
55
56 scanf("%d", &ch);
57
58
59 /* Menu control switch */
60 switch (ch)
61 {
62 case 1:
63 printf("\nEnter data to enqueue: ");
64 scanf("%d", &data);
65
66 // Enqueue function returns 1 on success
67 // otherwise 0
68 if (enqueue(&rear, &front, data))
69 printf("Element added to queue.");
70 else
71 printf("Queue is full.");
72
73 break;
74
75 case 2:
76 data = dequeue(&front);
77
78 // on success dequeue returns element removed
79 // otherwise returns INT_MIN
80 if (data == INT_MIN)
81 printf("Queue is empty.");
82 else
83 printf("Data => %d", data);
84
85 break;
86
87 case 3:
88
89 // isEmpty() function returns 1 if queue is emtpy
90 // otherwise returns 0
91 if (isEmpty())
92 printf("Queue is empty.");
93 else
94 printf("Queue size => %d", size);
95
96 break;
97
98 case 4:
99 data = getRear(rear);
100
101 if (data == INT_MIN)
102 printf("Queue is empty.");
103 else
104 printf("Rear => %d", data);
105
106 break;
107
108 case 5:
109
110 data = getFront(front);
111
112 if (data == INT_MIN)
113 printf("Queue is empty.");
114 else
115 printf("Front => %d", data);
116
117 break;
118
119 case 0:
120 printf("Exiting from app.\n");
121 exit(0);
122
123 default:
124 printf("Invalid choice, please input number between (0-5).");
125 break;
126 }
127
128 printf("\n\n");
129 }
130 getch();
131 }
132
133
134
135 /**
136 * Enqueues/Insert an element at the rear of a queue.
137 * Function returns 1 on success otherwise returns 0.
138 */
139 int enqueue(Queue ** rear, Queue ** front, int data)
140 {
141 Queue * newNode = NULL;
142
143 // Check queue out of capacity error
144 if (isFull())
145 {
146 return 0;
147 }
148
149 // Create a new node of queue type
150 newNode = (Queue *) malloc (sizeof(Queue));
151
152 // Assign data to new node
153 newNode->data = data;
154
155 // Initially new node does not point anything
156 newNode->next = NULL;
157
158 // Link new node with existing last node
159 if ( (*rear) )
160 {
161 (*rear)->next = newNode;
162 }
163
164
165 // Make sure newly created node is at rear
166 *rear = newNode;
167
168 // Link first node to front if its NULL
169 if ( !( *front) )
170 {
171 *front = *rear;
172 }
173
174 // Increment quque size
175 size++;
176
177 return 1;
178 }
179
180
181 /**
182 * Dequeues/Removes an element from front of the queue.
183 * It returns the element on success otherwise returns
184 * INT_MIN as error code.
185 */
186 int dequeue(Queue ** front)
187 {
188 Queue *toDequque = NULL;
189 int data = INT_MIN;
190
191 // Queue empty error
192 if (isEmpty())
193 {
194 return INT_MIN;
195 }
196
197 // Get element and data to dequeue
198 toDequque = *front;
199 data = toDequque->data;
200
201 // Move front ahead
202 *front = (*front)->next;
203
204 // Decrement size
205 size--;
206
207 // Clear dequeued element from memory
208 free(toDequque);
209
210 return data;
211 }
212
213
214 /**
215 * Gets, element at rear of the queue. It returns the element
216 * at rear of the queue on success otherwise return INT_MIN as
217 * error code.
218 */
219 int getRear(Queue * rear)
220 {
221 // Return INT_MIN if queue is empty otherwise rear.
222 return (isEmpty())
223 ? INT_MIN
224 : rear->data;
225 }
226
227
228 /**
229 * Gets, element at front of the queue. It returns the element
230 * at front of the queue on success otherwise return INT_MIN as
231 * error code.
232 */
233 int getFront(Queue * front)
234 {
235 // Return INT_MIN if queue is empty otherwise front.
236 return (isEmpty())
237 ? INT_MIN
238 : front->data;
239 }
240
241
242 /**
243 * Checks, if queue is empty or not.
244 */
245 int isEmpty()
246 {
247 return (size <= 0);
248 }
249
250
251 /**
252 * Checks, if queue is within the maximum queue capacity.
253 */
254 int isFull()
255 {
256 return (size > CAPACITY);
257 }
You might also like
Next Roll Prediction
PDF
33% (3)
Next Roll Prediction
77 pages
Circular Queue Data Structure
PDF
No ratings yet
Circular Queue Data Structure
13 pages
Lectures Queue
PDF
No ratings yet
Lectures Queue
56 pages
Queue
PDF
No ratings yet
Queue
6 pages
Production and Optimization of Xylanase and α-Amylase from Non-Saccharomyces Yeasts (Pichia membranifaciens)
PDF
No ratings yet
Production and Optimization of Xylanase and α-Amylase from Non-Saccharomyces Yeasts (Pichia membranifaciens)
10 pages
Data Structure-Tree
PDF
No ratings yet
Data Structure-Tree
103 pages
Queue Data Structure
PDF
No ratings yet
Queue Data Structure
36 pages
7 Dsa
PDF
No ratings yet
7 Dsa
80 pages
Queues - Array Implementation
PDF
No ratings yet
Queues - Array Implementation
32 pages
Lect6 Queues
PDF
No ratings yet
Lect6 Queues
48 pages
Evaluation Scheme & Detailed Syllabus of Civil Engineering 3rd Year
PDF
No ratings yet
Evaluation Scheme & Detailed Syllabus of Civil Engineering 3rd Year
43 pages
Carel Probes and Sensors Selection and Optimal Installation Guide 2021 06 26
PDF
No ratings yet
Carel Probes and Sensors Selection and Optimal Installation Guide 2021 06 26
40 pages
Module-3 122500
PDF
No ratings yet
Module-3 122500
59 pages
VMC Shenoy
PDF
No ratings yet
VMC Shenoy
90 pages
RCC DESIGN Intro
PDF
No ratings yet
RCC DESIGN Intro
55 pages
Dsc-Module II
PDF
No ratings yet
Dsc-Module II
30 pages
Queue Program in C
PDF
No ratings yet
Queue Program in C
3 pages
Queue
PDF
No ratings yet
Queue
47 pages
Queue
PDF
No ratings yet
Queue
31 pages
Queue Lab Manual
PDF
No ratings yet
Queue Lab Manual
8 pages
Programs
PDF
No ratings yet
Programs
43 pages
Queues Unit 4
PDF
No ratings yet
Queues Unit 4
35 pages
04 Queues
PDF
No ratings yet
04 Queues
58 pages
Queue Data Structure Introduction
PDF
No ratings yet
Queue Data Structure Introduction
23 pages
Queues
PDF
No ratings yet
Queues
44 pages
Different Terrestrial Sampling Technique
PDF
No ratings yet
Different Terrestrial Sampling Technique
41 pages
5 Queues
PDF
No ratings yet
5 Queues
33 pages
Radar Link Budget Analysis Using MATLAB Radar Designer
PDF
No ratings yet
Radar Link Budget Analysis Using MATLAB Radar Designer
14 pages
Summary Performance Rating
PDF
No ratings yet
Summary Performance Rating
51 pages
Queue
PDF
No ratings yet
Queue
34 pages
Queue
PDF
No ratings yet
Queue
33 pages
Queue
PDF
No ratings yet
Queue
61 pages
Module 3.3 Queues
PDF
No ratings yet
Module 3.3 Queues
38 pages
Circular Queue - Practice
PDF
No ratings yet
Circular Queue - Practice
9 pages
Garv Dsa
PDF
No ratings yet
Garv Dsa
19 pages
Data Structure Unit-3
PDF
No ratings yet
Data Structure Unit-3
20 pages
Ilovepdf Merged
PDF
No ratings yet
Ilovepdf Merged
14 pages
Geetadsa13 15
PDF
No ratings yet
Geetadsa13 15
18 pages
CLASS-III - DATA - EXCEL - FUNCTIONS - FOMULAE Done
PDF
No ratings yet
CLASS-III - DATA - EXCEL - FUNCTIONS - FOMULAE Done
45 pages
Programs MODULE 3
PDF
No ratings yet
Programs MODULE 3
17 pages
Lab 1 - Introduction and Protocol
PDF
No ratings yet
Lab 1 - Introduction and Protocol
28 pages
Queue Tree Graph
PDF
No ratings yet
Queue Tree Graph
18 pages
Queue Using Linked List
PDF
No ratings yet
Queue Using Linked List
5 pages
Dsa 3
PDF
No ratings yet
Dsa 3
8 pages
Msbte JPR All Program Chapter-2
PDF
No ratings yet
Msbte JPR All Program Chapter-2
28 pages
Unit 5 (P2)
PDF
No ratings yet
Unit 5 (P2)
9 pages
Leverages
PDF
No ratings yet
Leverages
13 pages
Destination: Proposal
PDF
No ratings yet
Destination: Proposal
5 pages
Exp 4 - Dsa Lab File
PDF
No ratings yet
Exp 4 - Dsa Lab File
18 pages
Group 5 Development of Single Actuator Circuit
PDF
No ratings yet
Group 5 Development of Single Actuator Circuit
26 pages
CS DataStructure-Lecture 3-Queues Arrays and Linked
PDF
No ratings yet
CS DataStructure-Lecture 3-Queues Arrays and Linked
32 pages
LAB NO:-05 Date:-01/10/2024 Aim Objectives
PDF
No ratings yet
LAB NO:-05 Date:-01/10/2024 Aim Objectives
14 pages
DSA Chapter 5
PDF
No ratings yet
DSA Chapter 5
16 pages
Neon
PDF
No ratings yet
Neon
3 pages
Reaffirmed 2017
PDF
No ratings yet
Reaffirmed 2017
17 pages
Queue
PDF
No ratings yet
Queue
20 pages
Diodo 1N4148
PDF
No ratings yet
Diodo 1N4148
7 pages
5) Implementation of Queue
PDF
No ratings yet
5) Implementation of Queue
8 pages
Unit4 Queue Updated
PDF
No ratings yet
Unit4 Queue Updated
14 pages
Clutches, Brakes and Flywheels
PDF
No ratings yet
Clutches, Brakes and Flywheels
23 pages
Programs
PDF
No ratings yet
Programs
14 pages
Queue Using Array
PDF
No ratings yet
Queue Using Array
4 pages
Journals Price List For 2022: No. Title Journal Abbreviation Issn (Print) Issn (E-Only)
PDF
No ratings yet
Journals Price List For 2022: No. Title Journal Abbreviation Issn (Print) Issn (E-Only)
24 pages
Queue C Programs - Array
PDF
No ratings yet
Queue C Programs - Array
19 pages
Queue Implementation
PDF
No ratings yet
Queue Implementation
8 pages
Experiment No9
PDF
No ratings yet
Experiment No9
7 pages
Q1 Science Iis MPS Conso
PDF
No ratings yet
Q1 Science Iis MPS Conso
2 pages
Implementation of Queue
PDF
No ratings yet
Implementation of Queue
4 pages
Ds Exp8
PDF
No ratings yet
Ds Exp8
7 pages
Dsu Pr22 Main
PDF
No ratings yet
Dsu Pr22 Main
7 pages
4 Queue
PDF
No ratings yet
4 Queue
7 pages
Handout 3.3 - Queues
PDF
No ratings yet
Handout 3.3 - Queues
5 pages
Lab Manual Final
PDF
No ratings yet
Lab Manual Final
27 pages
Dsu Pr21 Main
PDF
No ratings yet
Dsu Pr21 Main
6 pages
DS Exp 5
PDF
No ratings yet
DS Exp 5
3 pages
JBL Tr125 Manual de Servicio
PDF
No ratings yet
JBL Tr125 Manual de Servicio
2 pages
Exp 2
PDF
No ratings yet
Exp 2
6 pages
Paris2020 Composite
PDF
No ratings yet
Paris2020 Composite
7 pages
Evaluation Reporting of Results Annex 2a Examples of Re Test Programmes For Quantitative Tests PDF
PDF
No ratings yet
Evaluation Reporting of Results Annex 2a Examples of Re Test Programmes For Quantitative Tests PDF
17 pages
Data Structures Using C
PDF
No ratings yet
Data Structures Using C
6 pages
10
PDF
No ratings yet
10
2 pages
Lab 2 Q 4
PDF
No ratings yet
Lab 2 Q 4
2 pages
Circular Queue
PDF
No ratings yet
Circular Queue
2 pages
Manual AN7001
PDF
No ratings yet
Manual AN7001
7 pages
G9 07 Rate and Ratio
PDF
No ratings yet
G9 07 Rate and Ratio
6 pages
Queue Codes
PDF
No ratings yet
Queue Codes
5 pages
Course - CS 120 Computer Programming Lab
PDF
No ratings yet
Course - CS 120 Computer Programming Lab
3 pages
Cpa Lab 2 3 19 (9) - C
PDF
No ratings yet
Cpa Lab 2 3 19 (9) - C
2 pages
C Program To Implement Queue
PDF
No ratings yet
C Program To Implement Queue
4 pages
Computer Engineering Laboratory Solution Primer
From Everand
Computer Engineering Laboratory Solution Primer
Karan Bhandari
No ratings yet