Queue_Implementations
Queue_Implementations
void enqueue();
void dequeue();
void peek();
void display();
int main()
{
int choice;
printf("Operations of Queue:
");
printf("1.Enqueue\n2.Dequeue\n3.Peek\n4.Display\n5.Exit\n");
while(1)
{
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: enqueue(); break;
case 2: dequeue(); break;
case 3: peek(); break;
case 4: display(); break;
case 5: return 0;
default: printf("\nInvalid choice, try again.\n");
}
}
}
void enqueue()
{
int n;
if(rear == SIZE - 1)
{
printf("\nQueue overflow\n");
}
else
{
printf("Enter the element to be added to the queue: ");
scanf("%d", &n);
if (front == -1)
front = 0; // Set front for the first element
rear = rear + 1;
queue[rear] = n;
}
}
void dequeue()
{
if(front == -1 || front > rear)
{
printf("\nQueue underflow\n");
}
else
{
printf("Dequeued element: %d\n", queue[front]);
front = front + 1;
void peek()
{
if(front == -1)
{
printf("\nQueue is empty, nothing to peek.\n");
}
else
{
printf("Front element of the queue: %d\n", queue[front]);
}
}
void display()
{
int i;
if(front == -1)
{
printf("\nQueue is empty (underflow)\n");
}
else
{
printf("\nElements present in the queue:\n");
for(i = front; i <= rear; i++)
{
printf("%d\t", queue[i]);
}
printf("\n");
}
}
struct Node {
int data;
struct Node* next;
};
void enqueue();
void dequeue();
void peek();
void display();
int main()
{
int choice;
printf("Operations of Queue:\n");
printf("1.Enqueue\n2.Dequeue\n3.Peek\n4.Display\n5.Exit\n");
while(1)
{
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: enqueue(); break;
case 2: dequeue(); break;
case 3: peek(); break;
case 4: display(); break;
case 5: return 0;
default: printf("\nInvalid choice, try again.\n");
}
}
}
void enqueue()
{
int item;
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL)
{
printf("\nMemory allocation failed\n");
return;
}
newNode->data = item;
newNode->next = NULL;
void dequeue()
{
if (front == NULL) // Check if the queue is empty
{
printf("\nQueue underflow\n");
}
else
{
struct Node* temp = front;
printf("Dequeued item: %d\n", temp->data);
front = front->next; // Move the front pointer
void peek()
{
if (front == NULL)
{
printf("\nQueue is empty, nothing to peek.\n");
}
else
{
printf("Front item: %d\n", front->data);
}
}
void display()
{
if (front == NULL) // Check if the queue is empty
{
printf("\nQueue is empty (underflow)\n");
}
else
{
struct Node* temp = front;
printf("\nElements present in the queue:\n");
while (temp != NULL)
{
printf("%d\t", temp->data);
temp = temp->next;
}
printf("\n");
}
}