Exp-1-2-Discrete Structure
Exp-1-2-Discrete Structure
Exp-1-2-Discrete Structure
#include<stdio.h>
#include<stdlib.h>
#define MAX 5 //Maximum number of elements that can be stored
int top=-1,stack[MAX];
void push();
void pop();
void display();
void main()
{
int ch;
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
void push()
{
int val;
if(top==MAX-1)
{
printf("\nStack is full!!");
}
else
{
printf("\nEnter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}
void pop()
{
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nDeleted element is %d",stack[top]);
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nStack is...\n");
for(i=top;i>=0;--i)
printf("%d\n",stack[i]);
}
}
OUTPUT:-
1.Push
2.Pop
3.Display
4.Exit
1.Push
2.Pop
3.Display
4.Exit
1.Push
2.Pop
3.Display
4.Exit
1.Push
2.Pop
3.Display
4.Exit
Enter your choice(1-4):3
Stack is...
78
45
23
1.Push
2.Pop
3.Display
4.Exit
Deleted element is 78
*** Stack Menu ***
1.Push
2.Pop
3.Display
4.Exit
Stack is...
45
23
1.Push
2.Pop
3.Display
4.Exit
#include<stdio.h>
#include<stdlib.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
//clrscr();
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\nQueue is Full");
else
{
printf("\nEnter the element:");
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\nQueue is empty");
}
else
{
printf("\nDeleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
if(front==rear)
printf("\nQueue is Empty");
else
{
printf("\nQueue Elements are:\n");
for(i=front;i<rear; i++)
{
printf("%d\n",queue[i]);
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
getch();
}
OUTPUT:-
Deleted Element is 34
Enter the Choice:3