Stack Queue
Stack Queue
Stack Queue
#include<stdio.h>
#include<stdlib.h>
void push();
void pop();
void Display();
int a[20],n,top=-1;
int main()
{
printf("Enter Size: ");
scanf("%d",&n);
int ch;
while(1)
{
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: Display();
break;
case 4: exit(0);
break;
}
}
return 0;
}
void push()
{
int x;
if(top>=n-1)
{
printf("Stack is Overflow");
}
else
{
top++;
printf("Enter Data:");
scanf("%d",&x);
a[top]=x;
}
}
void pop()
{
int x;
if(top==-1)
{
printf("Stack is Underflow");
}
else
{
x=a[top];
top--;
printf("Value Deleted is: %d",x);
}
}
void Display()
{
if(top==-1)
{
printf("Stack is Empty");
}
else
{
printf("Stack:\n");
for(int i=0;i<=top;i++)
{
printf("%d\n",a[i]);
}
}
}
Queue
#include<stdio.h>
#include<stdlib.h>
void push();
void pop();
void Display();
int a[20],n,front=-1,rear=-1;
int main()
{
printf("Enter Size: ");
scanf("%d",&n);
int ch;
while(1)
{
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: Display();
break;
case 4: exit(0);
break;
}
}
return 0;
}
void push()
{
int x;
if(rear>=n-1)
{
printf("Stack is Overflow");
}
else
{
rear++;
printf("Enter Data:");
scanf("%d",&x);
a[rear]=x;
if(front==-1)
{
front=0;
}
}
}
void pop()
{
int x;
if(front==-1)
{
printf("Stack is Underflow");
}
else
{
x=a[front];
if(rear==front)
{
front=rear=-1;
}
else
{
front++;
}
printf("Value Deleted is: %d",x);
}
}
void Display()
{
if(rear==-1)
{
printf("Stack is Empty");
}
else
{
printf("Stack:\n");
for(int i=0;i<=rear;i++)
{
printf("%d\n",a[i]);
}
}
}