Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 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