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

Dsa CH 4 Program

Uploaded by

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

Dsa CH 4 Program

Uploaded by

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

STACK:

#include <stdio.h>
#include<conio.h>
#define MAX 5
void push(int data);
void pop();
void stackTop();
void display();
int tos=-1,stack[MAX];

int main()
{
int data=1,choice;

clrscr();
do
{ printf("\n***STACK OPERATIONS***");
printf("\n1. PUSH\n2.POP\n3.STACKTOP\n4.DISPLAY\n5.EXIT");
printf("\n Enter The Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n Enter Data to add into stack:");
scanf("%d",&data);
push(data);
display();
break;
case 2: pop();
display();
break;
case 3: stackTop();
display();
break;
case 4: display();
break;
case 5: exit(0);
default: printf("\n Enter Correct Choice");
}

}while(choice!=5);
getch();
return 0;
}
void push(int data)
{
if(tos==(MAX-1))
printf("\n Stack is Full cant Push Element...");
else
{ tos++;
stack[tos]=data;
printf("\n After Push Operation Stack is:");

}
}
void pop()
{
if(tos==-1)
printf("\n Stack is Underflow POP Operation is not possible");
else
{
printf("\n Element Pooped is: %d",stack[tos]);
tos--;
}
}
void stackTop()
{
if(tos==-1)
printf("\n Stack is Empty.");
else
printf("Data at the top of the stack is: %d",stack[tos]);
}
void display()
{
int i;
if(tos==-1)
printf("\n Stack is Empty.");
else
{
for(i=tos;i>=0;i--)
{
printf("\n\t\t\t\t%d",stack[i]);
printf("\n\t\t\t\t______");

}
}

}
Queue:

#include <stdio.h>
#include<conio.h>
#define MAX 5
void EnQueue(int data);
void DeQueue();
void QueueFront();
void QueueRear();
void display();
int front=-1,rear=-1,queue[MAX];

int main()
{
int data,choice;
clrscr();
do
{
printf("\n***QUEUE OPERATIONS***");
printf("\n1. EnQueue\n2.DeQueue\n3.QueueFront\,4.QueueRear\n5.EXIT");
printf("\n Enter The Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n Enter Data to add into Queue:");
scanf("%d",&data);
EnQueue(data);
break;
case 2: DeQueue();
break;
case 3: QueueFront();
break;
case 4: QueueRear();
break;
case 5: exit(0);
default: printf("\n Enter Correct Choice");
}

}while(choice!=5);
getch();
return 0;
}

void EnQueue(int data)


{
if(rear==(MAX-1))
printf("\n Queue is Full...");
else
{
if(rear==-1)
front=0;

rear++;
queue[rear]=data;
printf("\n After Enqueu Operation Queue is:");
display();

}
printf("\n FRONT=%d \t REAR=%d",front,rear);
}
void DeQueue()
{
if(front==-1)
printf("\n Queue is Underflow DeQueue Operation is not possible");
else
{
printf("\n Element Dequeued is: %d\n",queue[front]);
if(front==rear)
front=rear=-1;
else
front++;
display();
printf("\n FRONT=%d \t REAR=%d",front,rear);
}
}
void QueueFront()
{
if(rear==-1)
printf("\n Queue is Empty.");
else
printf("Data at the Front end of the Queue is: %d",queue[front]);
}
void QueueRear()
{
if(rear==-1)
printf("\n Queue is Empty.");
else
printf("Data at the Rear end of the Queue is: %d",queue[rear]);
}

void display()
{
int i;
if(rear==-1)
printf("\n Queue is Empty.");
else
{ printf("FRONT:");
for(i=front;i<=rear;i++)
{
printf("%d",queue[i]);
printf(" <== ");

}
printf(":REAR");
}

You might also like