0
• A queue has two basic operations: Insert and delete, the insert operation adds an element to the end of the queue and the delete operation removes the element from the front or the start of the queue. Program:

include < conio.h>

struct node

<

int data;

struct node * next;

3;

struct queue

<

struct node * front;

struct node * rear;

3;

struct queue * q ;

void create queue (struct queue * q)

<

q $\rightarrow$ rear = NULL;

q $\rightarrow$ front = NULL;

3

struct queue * insert (struct queue *q , int val)

<

struct node * ptr;

ptr = (struct node *) malloc (size of struct node *));

ptr $\rightarrow$ data = val;

if (q $\rightarrow$ front = = NULL)

<

q $\rightarrow$ front = ptr;

q $\rightarrow$ rear = ptr;

q $\rightarrow$ front $\rightarrow$ next = q $\rightarrow$ rear $\rightarrow$ next = NULL;

3

else

<

q $\rightarrow$ rear $\rightarrow$ next = ptr;

q $\rightarrow$ rear = ptr;

q $\rightarrow$ rear $\rightarrow$ next = NULL;

3

return q;

3

struct queue * delete (struct queue * q)

<

struct node * ptr;

ptr = q $\rightarrow$ front;

if (q $\rightarrow$ front = NULL)

print f ("in underflow");

else

<

q $\rightarrow$ front = q $\rightarrow$ next;

print f ("in deleted value = %d " , ptr $\rightarrow$ data );

free (ptr);

3

return q ;

struct queue * display (struct queue * q )

<

struct node *ptr;

ptr = q $\rightarrow$ front;

if (prt = = NULL)

print f ("in queue is empty");

else

<

while (prt ! = q $\rightarrow$ rear)

<

print f (" 1 + % d " , ptr $\rightarrow$ data);

ptr = ptr $\rightarrow$ next;

3

print f (" 1 + % d " , ptr $\rightarrow$ data);

3

return q ;

3

void main ( )

<

int ral , option;

create queue (q);

( );

do

<

prnt f ("in menu \n I - Insert\ n2 - Delete \n s - Display \n enter your option");

scan f ("%d " , & option);

switch (option)

<

Case 1 :

<

Prnt f ("in enter element to be inserted");

scan f (" % d " , & ravl);

q = insert (q , val);

3 break;

Case 2 :

<

q = delete (q);

3 break;

Case 3 :

<

q = Display (q) ;

3 break;

3 while (option ! = 5);

getch ( ) ;

3

renu • 23 views
 written 4 weeks ago by RB • 100