Skip to main content

Exercise 6: Queue Operations

Exercise 6:

i) Implement a queue using arrays and linked lists.

// C Program for Queue using Arrays.

#include <stdio.h>
#include <stdlib.h>
#define CAPACITY 50
int queue[CAPACITY];
int front=-1;
int rear=-1;
int isEmpty();
int isFull();
void enqueue(int data);
void dequeue();
void display();
int peek();
int main()
{ int ch,data,val; while(1)
    { printf("--------------------------------------\n"); printf(" QUEUE ARRAY IMPLEMENTATION PROGRAM \n"); printf("--------------------------------------\n"); printf("1. Enqueue\n"); printf("2. Dequeue\n"); printf("3. Display\n"); printf("4. Peek\n"); printf("5. Exit\n"); printf("--------------------------------------\n"); printf("Select an option: "); scanf("%d",&ch); switch(ch)
        { case 1: printf("\nEnter data to enqueue: "); scanf("%d",&data); enqueue(data); break; case 2: dequeue(); break; case 3: display(); break; case 4: val=peek(); if(val!=-1) printf("\nFront element is %d",val); break; case 5: printf("Exiting from app.\n"); exit(0); default: printf("Invalid choice, please input number between (1-5).\n"); } printf("\n\n"); } return 0; } int isFull()
{ if(rear==CAPACITY-1)
        return 1; else
        return 0; } int isEmpty()
{ if(front==-1 || front>rear)
        return 1; else
        return 0; } void enqueue(int ele)
{ if(isFull())
    { printf("\nQueue is overflow"); }
    else if(front==-1)
    { front=rear=0; queue[rear]=ele; }
    else
    { rear++; queue[rear]=ele; } } void dequeue()
{ int ele; if(isEmpty())
    { printf("\nQueue is empty"); }
    else
    { ele=queue[front]; printf("\nDeleted element from the queue is %d",ele); front++; if(front>rear)
        { front=rear=-1; } } } void display()
{ int i; if(isEmpty())
    { printf("\nQueue is empty"); }
    else
    { printf("\nThe elements of queue are\n"); for(i=front;i<=rear;i++)
        { printf("%d\t",queue[i]); } } } int peek()
{ if(isEmpty())
    { printf("\nQueue is empty"); return -1; }
    else
    { return queue[front]; } }

// C Program for Queue using Linked list.

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *rear, *front;
void enqueue(int ele);
void dequeue();
void peek();
void display();
void main()
{
    int ele, choice;
    rear = front = NULL;
    while (1)
    {
        clrscr();
        printf("\n Queue Operations\n");
        printf("1. Enqueue\n");
        printf("2. Dequeue\n");
        printf("3. Peek\n");
        printf("4. Display\n");
        printf("5. Exit");
        printf("\nEnter Your Choice: ");
        scanf("%d", &choice);
        switch (choice)
        {
            case 1:
                printf("\nEnter The Element: ");
                scanf("%d", &ele);
                enqueue(ele);
                printf("Given value %d inserted\n", ele);
                break;
            case 2:
                dequeue();
                break;
            case 3:
                peek();
                break;
            case 4:
                display();
                break;
            case 5:
                exit(0);
            default:
                printf("Wrong choice\n");
        }
    }
}
void enqueue(int value)
{
    struct node *newNode = (struct node *)malloc(sizeof(struct node));
    newNode->data = value;
    newNode->next = NULL;
    if (rear == NULL) // Queue is empty
    {
        front = rear = newNode;
        return;
    }
    rear->next = newNode; // Link new node at end
    rear = newNode;       // Update rear pointer
}
void dequeue()
{
    if (front == NULL)
    {
        printf("Queue is empty.\n");
        return;
    }
    struct node *temp = front; // Backup front node
    front = front->next;       // Move front to next node
    if (front == NULL)
    {
        rear = NULL; // Queue becomes empty
    }
    free(temp);
    printf("Value Deleted\n");
}
void peek()
{
    if (front == NULL)
    {
        printf("Queue is empty.\n");
        return;
    }
    printf("%d value is peek in queue\n", front->data);
}
void display()
{
    struct node *temp;
    if (front == NULL)
    {
        printf("\nList is empty\n");
    }
    else
    {
        temp = front;
        while (temp != NULL)
        {
            printf("%d -> ", temp->data);
            temp = temp->next;
        }
        printf("NULL\n");
    }
    getch();
}

ii) Develop a program to simulate a simple printer queue system.

// C Program for a simple printer queue system:

#include <stdio.h>
#define MAX 10
// Structure for one job
struct PrintJob {
    char name[20];
    int pages;
};
// Queue (single array of structures)
struct PrintJob queue[MAX];
int front = -1, rear = -1;
// Check empty
int isEmpty()
{ return (front == -1); } // Enqueue void enqueue(struct PrintJob job)
{ if (rear == MAX - 1)
{ printf("Queue Full!\n"); return; } if (front == -1) front = 0; rear++; queue[rear] = job; } // Dequeue struct PrintJob dequeue()
{ struct PrintJob job = queue[front]; if (front == rear) front = rear = -1; else front++; return job; } // Simulation void simulatePrinterQueue(int n)
{ struct PrintJob job; // Enqueue jobs for (int i = 1; i <= n; i++)
{ printf("Enter pages for Job%d: ", i); scanf("%d", &job.pages); sprintf(job.name, "Job-%d", i); enqueue(job); } printf("\nPrinting Jobs:\n"); // Dequeue and print while (!isEmpty())
{ job = dequeue(); printf("Printing %s (%d pages)\n", job.name, job.pages); } } int main()
{ int n; printf("Enter number of jobs: "); scanf("%d", &n); simulatePrinterQueue(n); return 0; }

iii) Solve problems involving circular queues.

// C Program for Circular Queues:

#include <stdio.h>
#define max 6
int queue[max];
int front = -1;
int rear = -1;
// Enqueue operation
void enqueue(int element)
{
    if (front == (rear + 1) % max)   // Queue Full
    {
        printf("\nQueue is overflow..");
    }
    else if (front == -1 && rear == -1)   // Queue Empty
    {
        front = rear = 0;
        queue[rear] = element;
    }
    else
    {
        rear = (rear + 1) % max;
        queue[rear] = element;
    }
}
// Dequeue operation
void dequeue()
{
    if (front == -1 && rear == -1)   // Queue Empty
    {
        printf("\nQueue is underflow..");
    }
    else if (front == rear)   // Only one element
    {
        printf("\nThe dequeued element is %d", queue[front]);
        front = rear = -1;
    }
    else
    {
        printf("\nThe dequeued element is %d", queue[front]);
        front = (front + 1) % max;
    }
}
// Peek operation
void peek()
{
    if (front == -1 && rear == -1)   // Queue Empty
    {
        printf("\nQueue is empty..");
    }
    else
    {
        printf("\nFront element is %d", queue[front]);
    }
}
// Display operation
void display()
{
    int i = front;
    if (front == -1 && rear == -1)
    {
        printf("\nQueue is empty..");
    }
    else
    {
        printf("\nElements in Queue are: ");
        while (i != rear)
        {
            printf("%d ", queue[i]);
            i = (i + 1) % max;
        }
        printf("%d", queue[i]);
    }
}
int main()
{
    int choice = 1, x;
    while (choice != 0)
    {
        printf("\n1. Insert");
        printf("\n2. Delete");
        printf("\n3. Display");
        printf("\n4. Peek");
        printf("\n0. Exit");
        printf("\nEnter your choice: ");
        scanf("%d", &choice);
        switch (choice)
        {
            case 1:
                printf("Enter element: ");
                scanf("%d", &x);
                enqueue(x);
                break;
            case 2:
                dequeue();
                break;
            case 3:
                display();
                break;
            case 4:
                peek();
                break;
        }
    }
    return 0;
}

Comments

Popular posts from this blog

23CS02 - DATA STRUCTURES (Theory)

Data Structures is a fundamental subject in computer science that deals with organizing, storing, and managing data efficiently. It helps in performing operations such as insertion, deletion, searching, and sorting in an optimized way.

23CS52 - DATA STRUCTURES (Lab)

Data Structures Lab is designed to provide hands-on experience in implementing and analyzing various data structures using the C programming language. Through a series of practical exercises, students learn to implement both linear and non-linear data structures such as arrays, linked lists, stacks, queues, trees, and hashing techniques.

Operating Systems - 20CS11 Lecture Notes

  COURSE OUTCOMES (COs): At the end of the course, students will be able to CO1 Demonstrate the underlying principles and techniques of the operating system     (Understand-l2) CO2 Interpret scheduling and communication methods of processes handled by operating systems (Understand-L2) CO3 Distinguish the process synchronization methods and deadlock handling approaches employed in operating systems (Understand-L2) CO4 Classify memory management techniques and virtual memory mechanisms     (Understand-L2) CO5 Interpret the strategies of disk scheduling algorithms and file system architecture(Understand-L2) Unit-Wise Lecture Notes: