0% found this document useful (0 votes)
6 views

Queue-CQueue-Dynamic CQueue

The document provides an overview of queue data structures, including linear queues, circular queues, and dynamic circular queues, with accompanying C code implementations for each type. It explains operations such as insertion, deletion, and display of elements within the queue, while also addressing overflow and underflow conditions. The document highlights the advantages of circular queues over linear queues in terms of memory efficiency.

Uploaded by

colabpython39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Queue-CQueue-Dynamic CQueue

The document provides an overview of queue data structures, including linear queues, circular queues, and dynamic circular queues, with accompanying C code implementations for each type. It explains operations such as insertion, deletion, and display of elements within the queue, while also addressing overflow and underflow conditions. The document highlights the advantages of circular queues over linear queues in terms of memory efficiency.

Uploaded by

colabpython39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

A Queue is defined as a linear data structure that is open at both ends and

the operations are performed in First In First Out (FIFO) order.


#include <stdio.h> #define MAX 50
void insert(); void delete(); void display();
int queue_array[MAX]; int rear = - 1; int front = - 1;
main() {
int choice;
while (1) {
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
}
}
}
void insert() {
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else {
if (front == - 1)
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}
void delete() {
if (front == - 1 || front > rear) {
printf("Queue Underflow \n");
return ;
}
else {
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
}
void display() {
int i;
if (front == - 1)
printf("Queue is empty \n");
else {
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}
In a linear queue, the traversal through the queue is possible only once, i.e.,
once an element is deleted, we cannot insert another element in its position.
This disadvantage of a linear queue is overcome by a circular queue, thus
saving memory.
# include<stdio.h> # define MAX 5 int arr[MAX];
int f = -1; int r = -1;
void insert(int item) {
if((f == 0 && r == MAX-1) || (f == r+1)) {
printf("Queue Overflow \n");
return;
}
if (f == -1) {
f = 0; r = 0;
}
else {
if(r == MAX-1)
r = 0;
else
r = r+1;
}
arr[r] = item ;
}
void del() {
if (f == -1) {
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n", arr[f]);
if(f == r)
f = r = -1;
else {
if(f == MAX-1)
f = 0;
else
f = f+1;
}
}
void display() {
int fp = f, rp = r;
if(f == -1) {
printf("Queue is empty\n"); return;
}
printf("Queue elements :\n");
if( fp <= rp )
while(fp <= rp) {
printf("%d ", arr[fp]); fp++;
}
else {
while(fp <= MAX-1) {
printf("%d ", arr[fp]); fp++;
}
fp = 0;
while(fp <= rp) {
printf("%d ", arr[fp]); fp++;
}
}
printf("\n");
int main() {
int ch, item;
do {
printf("1.Insert\t 2.Delete\t 3.Display\t and 4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch) {
case 1 :
printf("Input the element for insertion in queue : ");
scanf("%d", &item); insert(item); break;
case 2 :
del(); break;
case 3:
display(); break;
case 4: break;
default: printf("Wrong choice\n");
}
}while(ch!=4);
return 0;
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *front = NULL;
struct node *rear = NULL;
void display();
void enqueue(int);
void dequeue();
int main() {
int n, ch;
do {
printf("\nQueue Menu\n1. Add \n2. Remove\n3. Display\n4.
Exit");
printf("\nEnter Choice 0-3? : ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter number ");
scanf("%d", &n);
enqueue(n);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
}
}while (ch != 0);
}
void enqueue(int item) {
struct node *nptr = malloc(sizeof(struct node));
nptr->data = item; nptr->next = NULL;
if (rear == NULL) {
front = nptr; rear = nptr;
}
else {
rear->next = nptr;
rear = rear->next;
}
}
void display() {
struct node *temp;
temp = front;
printf("\n");
while (temp != NULL) {
printf("%d\t", temp->data);
temp = temp->next;
}
}
void dequeue() {
if (front == NULL) {
printf("\n\nqueue is empty \n");
}
else {
struct node *temp;
temp = front;
front = front->next;
printf("\n\n%d deleted", temp->data);
free(temp);
}
}
Dynamic Circular Queue Operations:
#include<stdio.h>
#include<stdlib.h>
int capacity;
int *queue;
int front = -1, rear = -1;
int checkFull () {
if ((front == rear + 1) || (front == 0 && rear == capacity - 1)) {
return 1;
}
return 0;
}
int checkEmpty () {
if (front == -1) {
return 1;
}
return 0;
}
void enqueue (int value) {
if ((front == rear + 1) || (front == 0 && rear == capacity - 1)) {
//printf ("Overflow condition\n");
queue = (int*) realloc(queue, capacity*2);
rear=capacity;
front=0;
printf("Rear value=%d\n", rear);
*(queue+rear) = value;
capacity*=2;
} else {
if (front == -1)
front = 0;
rear = (rear + 1) % capacity;
*(queue+rear) = value;
printf ("%d was enqueued to circular queue\n", value);
}
}
int dequeue () {
int variable;
if (checkEmpty ()) {
printf ("Underflow condition\n");
return -1;
}
else {
variable = *(queue+front);
if (front == rear) {
front = rear = -1;
}
else {
front = (front + 1) % capacity;
}
printf ("%d was dequeued from circular queue\n", variable);
return 1;
}
}
void display() {
int fp = front, rp = rear;
if(front == -1) {
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( fp <= rp ) {
while(fp <= rp) {
printf("%d ", queue[fp]);
fp++;
}
} else {
while(fp <= capacity-1) {
printf("%d ", queue[fp]);
fp++;
}
fp = 0;
while(fp <= rp) {
printf("%d ", queue[fp]);
fp++;
}
}
printf("\n");
}
int main() {
int ch, val;
printf("Enter size of array elements\n");
scanf("%d", &capacity);
queue = (int*)malloc(sizeof(int)*capacity);
while (1) {
printf("1.Enqueue Operation\n");
printf("2.Dequeue Operation\n");
printf("3.Display the Queue\n");
printf("4.Exit\n");
printf("Enter your choice of operations : ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the value to be inserted\n");
scanf("%d", &val);
enqueue(val);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
free(queue);
exit(0);
default:
printf("Incorrect choice \n");
}
}
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy