8.C Program to implement a simple queue (Part B)

 C Program to implement a simple queue(Part B)

ALGORITHM :

Step 1: Start
Step 2: Define a constant MAX_SIZE to represent the maximum size of the queue.
Step 3: Declare a queue structure with necessary variables (e.g., front and rear to keep track of the front and rear of the queue, and an array to store elements).
Step 4: Implement the enqueue function to add an item to the rear of the queue.
        a. Check if the queue is full (rear == MAX_SIZE - 1).
        b. If not full, increment rear by 1 and add the item to the queue.
        c. If the queue is full, display an overflow error message.
Step 5: Implement the dequeue function to remove and return the front item from the queue.
        a. Check if the queue is empty (front == rear).
        b. If not empty, remove the front item from the queue, increment front by 1, and return the item.
        c. If the queue is empty, display an underflow error message or return a special value to indicate the queue is empty (e.g., -1).
Step 6: Implement the peek function to get the front item from the queue without removing it.
        a. Check if the queue is empty (front == rear).
        b. If not empty, return the front item from the queue.
        c. If the queue is empty, display an error message or return a special value to indicate the queue is empty (e.g., -1).
Step 7: Implement the is_empty function to check if the queue is empty.
        a. Check if the queue is empty (front == rear).
        b. If empty, return 1 (true); otherwise, return 0 (false).
Step 8: Implement the is_full function to check if the queue is full.
        a. Check if the queue is full (rear == MAX_SIZE - 1).
        b. If full, return 1 (true); otherwise, return 0 (false).
Step 9: End


SOURCE CODE :

#include<stdio.h>

#include<time.h>

#define  max 50

void insert();

void delete();

void display();

int queue_array[max];

int rear=-1;

int front=-1;

void main()

{

int choice;

clock_t start,end;

  double cpu_time_used;

clrscr();

start = clock();

while(1)

{

printf("1. insert \n");

printf("2. delete \n");

printf("3.display  \n");

printf("4.exit\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");

      }

  end = clock();

            cpu_time_used=((double)(end-start))/CLOCKS_PER_SEC;

      printf("the time taken for execution is %f seconds",cpu_time_used);

      getch();

  }

  }

void insert()

{

int add_item;

if(rear==max-1)

printf("queue is overflow \n");

else

{

if (front==-1)

front=0;

printf("insert 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");

}

}


OUTPUT :ll update soon


No comments:

Post a Comment