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
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
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
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save 10_Queue With Linked List For Later
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
CS DataStructure-Lecture 3-Queues Arrays and Linked
PDF
No ratings yet
CS DataStructure-Lecture 3-Queues Arrays and Linked
32 pages
Circular Queue Data Structure
PDF
No ratings yet
Circular Queue Data Structure
13 pages
Queue
PDF
No ratings yet
Queue
6 pages
Programs
PDF
No ratings yet
Programs
43 pages
Module 3.3 Queues
PDF
No ratings yet
Module 3.3 Queues
38 pages
10
PDF
No ratings yet
10
2 pages
04 Queues
PDF
No ratings yet
04 Queues
58 pages
Dsu Pr22 Main
PDF
No ratings yet
Dsu Pr22 Main
7 pages
Queue
PDF
No ratings yet
Queue
20 pages
Queue Implementation
PDF
No ratings yet
Queue Implementation
8 pages
Module-3 122500
PDF
No ratings yet
Module-3 122500
59 pages
Unit4 Queue Updated
PDF
No ratings yet
Unit4 Queue Updated
14 pages
Queue
PDF
No ratings yet
Queue
47 pages
Queue
PDF
No ratings yet
Queue
34 pages
Circular Queue
PDF
No ratings yet
Circular Queue
2 pages
Experiment No9
PDF
No ratings yet
Experiment No9
7 pages
Ds Exp8
PDF
No ratings yet
Ds Exp8
7 pages
Lab 2 Q 4
PDF
No ratings yet
Lab 2 Q 4
2 pages
Programs
PDF
No ratings yet
Programs
14 pages
Data Structure Unit-3
PDF
No ratings yet
Data Structure Unit-3
20 pages
DS Exp 5
PDF
No ratings yet
DS Exp 5
3 pages
5) Implementation of Queue
PDF
No ratings yet
5) Implementation of Queue
8 pages
Implementation of Queue
PDF
No ratings yet
Implementation of Queue
4 pages
Garv Dsa
PDF
No ratings yet
Garv Dsa
19 pages
Queue Tree Graph
PDF
No ratings yet
Queue Tree Graph
18 pages
7 Dsa
PDF
No ratings yet
7 Dsa
80 pages
Dsa 3
PDF
No ratings yet
Dsa 3
8 pages
Queue Using Array
PDF
No ratings yet
Queue Using Array
4 pages
Dsu Pr21 Main
PDF
No ratings yet
Dsu Pr21 Main
6 pages
Exp 2
PDF
No ratings yet
Exp 2
6 pages
Queue Program in C
PDF
No ratings yet
Queue Program in C
3 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
4 Queue
PDF
No ratings yet
4 Queue
7 pages
Queue C Programs - Array
PDF
No ratings yet
Queue C Programs - Array
19 pages
Data Structures Using C
PDF
No ratings yet
Data Structures Using C
6 pages
Geetadsa13 15
PDF
No ratings yet
Geetadsa13 15
18 pages
C Program To Implement Queue
PDF
No ratings yet
C Program To Implement Queue
4 pages
Queues - Array Implementation
PDF
No ratings yet
Queues - Array Implementation
32 pages
Queues
PDF
No ratings yet
Queues
44 pages
Data Structure-Tree
PDF
No ratings yet
Data Structure-Tree
103 pages
Lectures Queue
PDF
No ratings yet
Lectures Queue
56 pages
Dsc-Module II
PDF
No ratings yet
Dsc-Module II
30 pages
Exp 4 - Dsa Lab File
PDF
No ratings yet
Exp 4 - Dsa Lab File
18 pages
Handout 3.3 - Queues
PDF
No ratings yet
Handout 3.3 - Queues
5 pages
Queue Using Linked List
PDF
No ratings yet
Queue Using Linked List
5 pages
Queue Data Structure
PDF
No ratings yet
Queue Data Structure
36 pages
DSA Chapter 5
PDF
No ratings yet
DSA Chapter 5
16 pages
Lect6 Queues
PDF
No ratings yet
Lect6 Queues
48 pages
Circular Queue - Practice
PDF
No ratings yet
Circular Queue - Practice
9 pages
Queue
PDF
No ratings yet
Queue
33 pages
Queue
PDF
No ratings yet
Queue
31 pages
Queue
PDF
No ratings yet
Queue
61 pages
Ilovepdf Merged
PDF
No ratings yet
Ilovepdf Merged
14 pages
DotNet Technology Complete Note BCA
PDF
No ratings yet
DotNet Technology Complete Note BCA
154 pages
Programs MODULE 3
PDF
No ratings yet
Programs MODULE 3
17 pages
5 Queues
PDF
No ratings yet
5 Queues
33 pages
Queue Data Structure Introduction
PDF
No ratings yet
Queue Data Structure Introduction
23 pages
Ai Unit 1 Notes
PDF
No ratings yet
Ai Unit 1 Notes
21 pages
JulyAugust 2022
PDF
No ratings yet
JulyAugust 2022
1 page
Class 5th Maths PT-1
PDF
No ratings yet
Class 5th Maths PT-1
4 pages
Queues Unit 4
PDF
No ratings yet
Queues Unit 4
35 pages
Queue Codes
PDF
No ratings yet
Queue Codes
5 pages
Rational Numbers and Long Division
PDF
No ratings yet
Rational Numbers and Long Division
13 pages
Chapter3 ProblemSolvingBySearching
PDF
No ratings yet
Chapter3 ProblemSolvingBySearching
61 pages
Activity Sheet Decimal
PDF
No ratings yet
Activity Sheet Decimal
2 pages
Queue Lab Manual
PDF
No ratings yet
Queue Lab Manual
8 pages
GNU Make
PDF
No ratings yet
GNU Make
382 pages
Chapter 1 - Logic-2
PDF
No ratings yet
Chapter 1 - Logic-2
23 pages
DSA Assignment PDF
PDF
No ratings yet
DSA Assignment PDF
50 pages
Lecture 2 Coding Decoding
PDF
No ratings yet
Lecture 2 Coding Decoding
22 pages
BCA 2017 Onwards
PDF
No ratings yet
BCA 2017 Onwards
75 pages
Research Papers: Mr. Jitendra Nasriwala
PDF
No ratings yet
Research Papers: Mr. Jitendra Nasriwala
6 pages
USCSP 369-Java II Labbook
PDF
No ratings yet
USCSP 369-Java II Labbook
56 pages
Chapter 10
PDF
No ratings yet
Chapter 10
43 pages
Class - 10 - AI - Practical - File 2023-24
PDF
No ratings yet
Class - 10 - AI - Practical - File 2023-24
20 pages
Lecture TWP Python A03 1a General Features of Python
PDF
No ratings yet
Lecture TWP Python A03 1a General Features of Python
37 pages
Lect34 Huffman Coding
PDF
No ratings yet
Lect34 Huffman Coding
13 pages
TI EML0610Q 144LD 244LD FF en 240FF 20
PDF
No ratings yet
TI EML0610Q 144LD 244LD FF en 240FF 20
31 pages
Digital Logic Design Lecture 1.1
PDF
No ratings yet
Digital Logic Design Lecture 1.1
23 pages
Instagram Automation
PDF
No ratings yet
Instagram Automation
4 pages
Brahmakshatriya 2021
PDF
No ratings yet
Brahmakshatriya 2021
13 pages
Introduction To Computation and Programming Using Python With Application To Understanding Data 2nd Edition Edition Guttag
PDF
No ratings yet
Introduction To Computation and Programming Using Python With Application To Understanding Data 2nd Edition Edition Guttag
47 pages
FJA-Net A Fuzzy Joint Attention Guided Network For Classification of Glaucoma Stages
PDF
No ratings yet
FJA-Net A Fuzzy Joint Attention Guided Network For Classification of Glaucoma Stages
11 pages
Producer Consumer
PDF
No ratings yet
Producer Consumer
11 pages
INTERNAL
PDF
No ratings yet
INTERNAL
11 pages
Adaline and Madaline Neural Network Architecture
PDF
No ratings yet
Adaline and Madaline Neural Network Architecture
9 pages
88 Hash
PDF
No ratings yet
88 Hash
3 pages
Sample Quetions Midterm of CSE-425 Summer 2024
PDF
No ratings yet
Sample Quetions Midterm of CSE-425 Summer 2024
2 pages
Shortcuts
PDF
No ratings yet
Shortcuts
2 pages
Computer Engineering Laboratory Solution Primer
From Everand
Computer Engineering Laboratory Solution Primer
Karan Bhandari
No ratings yet