Search This Blog
Welcome to 1printf(), your ultimate destination for C, C++, Linux, Data Structures, and Microcontroller programming! π πΉLearn advanced coding techniques in C& C++ πΉMaster Linux internals & shell scripting πΉDeep dive into Data Structures & Algorithms πΉExplore Embedded Systems & Microcontrollers (8051,UART, RTOS) πΉGet hands-on coding tutorials, project ideas,and interview preparation tips Whether you're a beginner or an experienced programmer, this channel will help you
Featured
- Get link
- X
- Other Apps
Queue Operations in C Using Array
✅ Queue Operations in C Using Array
#include<stdio.h>
#define size 5
int queue[size];
int front = -1, rear = -1;
int isempty()
{
return (front == -1 || front > rear);
}
int isfull()
{
return (rear == size - 1);
}
void enqueue(int value)
{
if(isfull())
{
printf("Queue overflow\n");
}
else
{
if(front == -1)
front = 0;
queue[++rear] = value;
printf("%d added to queue\n", value);
}
}
void dequeue()
{
if(isempty())
{
printf("Queue underflow\n");
}
else
{
printf("%d removed from queue\n", queue[front++]);
}
}
void peek()
{
if(isempty())
{
printf("Queue is empty\n");
}
else
{
printf("Front element is %d\n", queue[front]);
}
}
void display()
{
if(isempty())
{
printf("Queue is empty\n");
}
else
{
printf("Queue elements:\n");
for(int i = front; i <= rear; i++)
{
printf("%d\n", queue[i]);
}
}
}
int main()
{
int choice, data;
while(1)
{
printf("\n1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter data: ");
scanf("%d", &data);
enqueue(data);
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
return 0;
default:
printf("Invalid choice\n");
}
}
}
π Explanation:
This C program implements queue operations using an array. A queue follows the FIFO (First In First Out) principle.
- Enqueue: Adds an element at the rear of the queue.
- Dequeue: Removes an element from the front.
- Peek: Displays the front element without removing it.
- Display: Shows all elements from front to rear.
- Overflow: Occurs when queue is full.
- Underflow: Occurs when queue is empty.
This is a basic and important data structure implementation in C.
π§Ύ Sample Output:
1. Enqueue 2. Dequeue 3. Peek 4. Display 5. Exit Enter choice: 1 Enter data: 10 10 added to queue Enter choice: 1 Enter data: 20 20 added to queue Enter choice: 4 Queue elements: 10 20
π Keywords:
queue in C using array, enqueue dequeue peek display, FIFO queue implementation, data structures C, queue program C
π Hashtags:
#CProgramming #QueueInC #DataStructures #EnqueueDequeue #CodingBasics #1printf
Popular Posts
C++ Program for Hybrid Inheritance (All Types Together)
- Get link
- X
- Other Apps
C++ Program for Function Overloading Example
- Get link
- X
- Other Apps
Comments
Post a Comment