0% found this document useful (0 votes)
28 views4 pages

Dsu Uef

The document contains code for implementing a circular queue and stack data structures using arrays in C. It includes functions to enqueue, dequeue, display the queue and push, pop, display the stack. The main function runs a loop taking user input to perform these operations and test the implementations.

Uploaded by

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

Dsu Uef

The document contains code for implementing a circular queue and stack data structures using arrays in C. It includes functions to enqueue, dequeue, display the queue and push, pop, display the stack. The main function runs a loop taking user input to perform these operations and test the implementations.

Uploaded by

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

#include<stdio.

h>
#define MAX_SIZE 5
#include<stdlib.h>

int front =-1,rear=-1;


int queue[MAX_SIZE];

void enqueue(int value)


{
if(front ==0 && rear==MAX_SIZE-1)
{
printf("\nQueue is full");

else if(front==-1)
{
front=rear=0;

}
else if (rear==MAX_SIZE-1 && front !=0)
{
rear=0;
}
else
{
rear++;
queue[rear]=value;
printf("%d enqueued to queue",value );
}
}

int dequeue()
{
int value ;
if(front==-1)
{
printf("\n Queue is empty \n ");
return -1;
}
value=queue[front];
if (front==rear)
front=rear-1;

else if(front==MAX_SIZE-1)
{
front=0;
}
else
{
front++;
}
return value;

}
void display()
{
int i;
if(front==-1)
{
printf("Queue is empty");
return;
}

printf("Elements in the queue are \n ");


if(rear>=front)
for(i=front;i<rear;i++)
{
printf("%d ",queue[i]);
}
else
{
for(i=front;i<MAX_SIZE;i++)
{
printf("%d ",queue[i]);
}
for(i=0;i<=rear;i++)
{
printf("%d",queue[i]);
}
}
printf("\n");
}
int main()
{
int ch, value ;
while(1)
{
printf("\n Circular queue operations \n ");
printf("\n 1. Enqueue \n 2. deueue\n 3.display \n4.exit ");
printf("\n ----------------------------------\n Enter the choice");
scanf(" %d",&ch);

switch(ch)
{
case 1:
printf("\n Enter the elemnets");
scanf("%d",&value);
enqueue(value);
break;
case 2:
value=dequeue();
if(value==-1)
{
printf("dequeued element %d",value);

}
break;
case 3:
display();
break;
case 4:
printf("exiting from the program");
return 0;
}
}
}
***********************************************************************************
***********************************************

// Online C compiler to run C program online


#include <stdio.h>
#include<stdlib.h>
#define MAX 5
int stack[MAX];
int top=-1;
void push(int value)
{
if(top==MAX-1)
{
printf("Stack is full");

}
else
{
top++;
stack[top]=value;
printf("%d is pushed to the stack",value);

}
}
void pop()
{
if(top==-1)
{
printf("stack is empty");

}
else
{
printf("%d is popped from the stack",stack[top--]);

}
}
void display()
{
int i;
if(top==-1)
{
printf("Stack is empty");

}
else
{
for(i=0;i<top;i++)
{
printf("%d",stack[i]);
}
}
}
int main()
{
int value;
int ch;
while(1)
{
printf("\n ********stack operations**********\n");
printf("\n Menu \n 1.Push 2.Pop 3.Display 4.Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the value");
scanf("%d",&value);
push(value);
break;
case 2:
pop();
break;
case 3:display();
break;
case 4:printf("Exiting from the the loop");
return 0;
default : exit(0);
}

}
}

You might also like