Ds Lab Cycle 3
Ds Lab Cycle 3
#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 4.Quit:");
scanf("%d",&choice);
switch(choice)
{
case 1: insert();
break;
case 2:
deleteq();
break;
case 3:
display();
break;
case 4:
exit(1); break;
default:
}}}
void insert()
int item;
if(rear==max-1)
else {
if(front==-1)
front=0;
scanf("%d",&item);
rear=rear+1;
queue[rear]=item;
}}
void deleteq()
if(front==-1||front>rear)
return;
else
front=front+1;
}}
void display()
int i;
if(front==-1)
else{
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;
}
}
}