0% found this document useful (0 votes)
69 views

Array Implementation of A Queue AIM

1) The document describes implementations of a queue using arrays and linked lists in C. 2) For the array implementation, functions are defined for enqueue, dequeue, and display operations on the queue. 3) For the linked list implementation, a node structure is defined and functions are written for enqueue, dequeue, and display operations on the linked list queue.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Array Implementation of A Queue AIM

1) The document describes implementations of a queue using arrays and linked lists in C. 2) For the array implementation, functions are defined for enqueue, dequeue, and display operations on the queue. 3) For the linked list implementation, a node structure is defined and functions are written for enqueue, dequeue, and display operations on the linked list queue.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

ARRAY IMPLEMENTATION OF A QUEUE

AIM
To write a c program to represent queue using array.

CODING

#include<stdio.h>
#include<conio.h>
#define QSIZE 5
#define FULL 0
#define EMPTY 1
#define SOMEDATA 2

int front,rear,Qstatus;

void enqueue(int queue[],int data)


{
Qstatus=SOMEDATA;
rear++;
if(rear==QSIZE)
rear=0;
if(front==-1&&rear==(QSIZE-1)||rear==front)
Qstatus=FULL;
queue[rear]=data;
}

int dequeue(int queue[])


{
front++;
if(front==QSIZE)
front=0;
if(front==rear)
Qstatus=EMPTY;
else
Qstatus=SOMEDATA;
return(queue[front]);
}
void display(int queue[])
{
int i;
if(Qstatus==EMPTY)
printf("\nqueue is empty!!");
else
{
i=front;
printf("\nQueue contains...front-->");
do
{
printf("%d-->",queue[i=(i+1)%QSIZE]);
}while(i!=rear);
printf("rear\n");
if(Qstatus==FULL)
printf("\nqueue is full!!");
}
}

void main()
{
int queue[QSIZE],choice,data;
clrscr();

printf("Representation of queue using arrays\n");


printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit");
rear=-1;
front=-1;
Qstatus=EMPTY;
while(1)
{
printf("\nenter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: if(Qstatus==FULL)
printf("\nQueue overflow!!");
else
{
fflush(stdin);
printf("\nenter the element to be added:");
scanf("%d",&data);
enqueue(queue,data);
display(queue);
}
break;
case 2: if(Qstatus==EMPTY)
printf("\nQueue underflow!!");
else
{
printf("\nthe dequeued element is %d",dequeue(queue));
display(queue);
}
break;
case 3: display(queue);
break;
case 4: exit(0);
default:printf("\nenter valid choice");
}
}
}
OUTPUT
Representation of queue using arrays
1.Enqueue
2.Dequeue
3.Display
4.Exit
enter your choice:2

Queue underflow!!
enter your choice:1

enter the element to be added:11

Queue contains...front-->11-->rear

enter your choice:1

enter the element to be added:22

Queue contains...front-->11-->22-->rear

enter your choice:1

enter the element to be added:33

Queue contains...front-->11-->22-->33-->rear

enter your choice:2

the dequeued element is 11


Queue contains...front-->22-->33-->rear

enter your choice:

RESULT
Thus queue has been implemented using arrays.
LINKED LIST IMPLEMENTATION OF A QUEUE
AIM
To write a c program to represent queue using linked list.

CODING

#include<stdio.h>
#include<conio.h>

struct queue
{
int data;
struct queue *nextptr;
};

void enqueue(struct queue **frontptr,struct queue **rearptr,int value)


{
struct queue *newptr;
newptr=(struct queue *)malloc(sizeof(struct queue));
if(newptr!=NULL)
{
newptr->data=value;
newptr->nextptr=NULL;
if(*frontptr==NULL)
*frontptr=newptr;
else
(*rearptr)->nextptr=newptr;
*rearptr=newptr;
}
else
printf("\nmemory not available");
}

int dequeue(struct queue **frontptr,struct queue **rearptr)


{
int value;
struct queue *tempptr;
value=(*frontptr)->data;
tempptr=*frontptr;
*frontptr=(*frontptr)->nextptr;
if(*frontptr==NULL)
*rearptr=NULL;
free(tempptr);
return value;
}

void display(struct queue *currentptr)


{
if(currentptr==NULL)
printf("\n the queue is empty!!");
else
{
printf("\nQueue contains...front->");
while(currentptr!=NULL)
{
printf("%d->",currentptr->data);
currentptr=currentptr->nextptr;
}
}
}

void main()
{
struct queue *frontptr=NULL,*rearptr=NULL;
int choice,item;
clrscr();

printf("Representation of queue using linked list\n");


printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit");
while(1)
{
printf("\nenter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nenter the element to be added:");
fflush(stdin);
scanf("%d",&item);
enqueue(&frontptr,&rearptr,item);
display(frontptr);
break;
case 2: if(frontptr==NULL)
printf("\nQueue underflow!!");
else
{
item=dequeue(&frontptr,&rearptr);
printf("\nthe dequeued element is %d",item);
display(frontptr);
}
break;
case 3: display(frontptr);
break;
case 4: exit(0);
default:printf("\nenter valid choice");
}
}
}
OUTPUT
Representation of queue using linked list
1.Enqueue
2.Dequeue
3.Display
4.Exit
enter your choice:1

enter the element to be added:22

Queue contains...front->22->rear
enter your choice:1

enter the element to be added:33

Queue contains...front->22->33->rear
enter your choice:1

enter the element to be added:44

Queue contains...front->22->33->44->rear
enter your choice:2

the dequeued element is 22


Queue contains...front->33->44->rear
enter your choice:

RESULT
Thus queue has been implemented using linked list.

You might also like