0
907views
Queue ADT using linked list.
1 Answer
0
2views
  • 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 of the start of the queue.

enter image description here

Fig, Linked Queue.

Program:

include < stdio.h>

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 a 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)

printf ("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 a 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:

<

Print f ("in entering 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

Please log in to add an answer.