0% found this document useful (0 votes)
14 views

Stack Queue

The document contains C code definitions for stack and queue data structures using arrays. It defines functions for push, pop, and display operations on both structures. The main function provides a menu for the user to select these operations and the structures are initialized with size input from the user. Push adds an element, pop removes and displays an element, and display prints all elements. Underflow and overflow conditions are checked during push and pop operations.

Uploaded by

Hit Malaviya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Stack Queue

The document contains C code definitions for stack and queue data structures using arrays. It defines functions for push, pop, and display operations on both structures. The main function provides a menu for the user to select these operations and the structures are initialized with size input from the user. Push adds an element, pop removes and displays an element, and display prints all elements. Underflow and overflow conditions are checked during push and pop operations.

Uploaded by

Hit Malaviya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Stack

#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]);
}
}
}

You might also like