[go: up one dir, main page]

0% found this document useful (0 votes)
7 views8 pages

Ds Lab Cycle 3

Uploaded by

lovelykdking
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Ds Lab Cycle 3

Uploaded by

lovelykdking
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

QUEUE USING ARRAY

#include<stdio.h>

#include<conio.h>

#define max 16

int queue[max];

int rear=-1;

int front=-1;

void insert();

void deleteq();

void display();

void main()

int choice;

clrscr();

while(1)

printf("\n\t Queue opration");

printf("\n 1.Insert element to Queue:");

printf("\n 2.Delete element from queue:");

printf("\n 3.Disply all element of queue:");

printf("\n 4.Quit:");

printf("\n Enter your choice:");

scanf("%d",&choice);

switch(choice)

{
case 1: insert();

break;

case 2:

deleteq();

break;

case 3:

display();

break;

case 4:

exit(1); break;

default:

printf("\n Wrong choice:");

}}}

void insert()

int item;

if(rear==max-1)

printf("\n Queue Overflowe!!");

else {

if(front==-1)

front=0;

printf("\n Insert the element in queue:");

scanf("%d",&item);

rear=rear+1;

queue[rear]=item;
}}

void deleteq()

if(front==-1||front>rear)

printf("\n Queue under flow:");

return;

else

printf("\n Element delete from queue is %d",queue[front]);

front=front+1;

}}

void display()

int i;

if(front==-1)

printf("\n Queue is empty:");

else{

printf("\n Elements of the queue:");

for(i=front;i<=rear;i++)

printf("\t%d",queue[i]);

}}
QUEUE USING LINKED LIST

PROGRAM:
#include<stdio.h>
#include<conio.h>
struct queue
{
int data;
struct queue *next;
}*front, *rear, *temp, *c, *q;
void addq();
void delq();
void display();
void main()
{
struct queue *front;
int reply,option,data;
clrscr();
front=NULL;
do
{
printf("\n\t Queue operations using linked list");
printf("\n 1.Add");
printf("\n 2.Delete");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\n Select proper option:");
scanf("%d",&option);
switch(option)
{
case 1:
addq();
break;
case 2:
delq();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n \t wrong option...!!");
}
}while(1);
}
void addq()
{
c=(struct queue*)malloc(sizeof(struct queue));
printf("\n Enter data");
scanf("%d",&c->data);
c->next=NULL;
if(front==NULL)
{
rear=front=c;
}
else
{
rear->next=c;
rear=rear->next;
}
printf("\n The element is %d inserted",c->data);
}
void delq()
{
if(front==NULL)
printf("Queue is empty");
else
{
q=front;
if(q!=NULL)
{
q=front;
front=front->next;
printf("Delete data:%d",q->data);
free(q);
}
}
}
void display()
{
c=front;
if(c==NULL)
printf("Queue is empty");
else
{
printf("\n Elements in the queue");
while(c!=NULL)
{
printf("\t%d",c->data);
c=c->next;
}
}
}

You might also like