0% found this document useful (0 votes)
8 views9 pages

DSA Lab A46 Experiment 1

Uploaded by

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

DSA Lab A46 Experiment 1

Uploaded by

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

DSA lab Experiment 1:

Exp 1A:
/* WAP to do queue opertions on Stack */
/* Batch: A3; 2nd Year; Roll No: 46; 2024-25 */

#include <stdio.h>
#include <conio.h>
#define MAX 4

void enqueue();
void dequeue();
void display();
int front=-1, rear=-1;
int queue[MAX];

int main()
{
int option;
while(option!=4)
{
clrscr();
printf("\n\n **** Menu ****\n");
printf("\n 1.Add Elements");
printf("\n 2.Delete Elements");
printf("\n 3.Display Elements");
printf("\n 4.Exit\n");
printf("\n Enter your Choice: ");
scanf("%d", &option);
switch(option)
{
case 1:
{
enqueue();
break;
}

case 2:
{
dequeue();
break;
}

case 3:
{
display();
break;
}

case 4:
{
printf("\nThanks for Joining!");
break;
}
default:
printf("You have entered an invalid input");
}
getch();
}
return 0;
}

void enqueue()
{
if (rear == MAX - 1)
{
printf("Queue is Overflow.\n");
}
else
{
if (front == -1 && rear == -1)
{
front = rear = 0;
}
else
{
rear++;
}

printf("\nEnter the element that you want to insert: ");


scanf("%d", &queue[rear]);
}
}

void dequeue()
{
if (front == -1 || front > rear)
{
printf("\nThe Queue is Underflow\n");
}
else
{
int value = queue[front];
front++;
printf("\nThe deleted value is %d.\n", value);
if (front > rear)
{
front = rear = -1; // Reset the queue if it's empty
}
}
}

void display()
{
int i;
if (front == -1)
{
printf("Queue is Empty.\n");
}
else
{
printf("The Following are the queue you Filled in: ");
for (i = front; i <= rear; i++)
{
printf("%d ", queue[i]);
}
printf("\n");
}
}

Output:
Exp 1B:
/* WAP for stack operations using array */
/* Batch: A3; 2nd Year; Roll No: 46; 2024-25 */

#include <stdio.h>
#include <conio.h>
#define MAX 3
int stack [MAX];
int top=-1;
void push(int value,int stack[]);
int pop(int stack[]);
int peek(int stack[]);
int main()
{
int option, value, i;
do
{
clrscr();
printf("\n **** Menu ****\n");
printf("1.Push Operation\n");
printf("2.Pop Operation\n");
printf("3.Peek Operation\n");
printf("4.Display Stack\n");
printf("5.Exit\n");
printf("\nEnter your Choice: ");
scanf("%d", &option);
switch(option)
{
case 1:
{
clrscr();
printf("\nEnter the elements you want to add to
stack: ");
scanf("%d", &value);
push(value,stack);
getch();
break;
}

case 2:
{
clrscr();
value=pop(stack);
if(value!=-1)
printf("\nYour deleted value from stack is %d",
value);
getch();
break;
}

case 3:
{
clrscr();
value=peek(stack);
if(value!=-1)
printf("The top value to the stack is %d", value);
getch();
break;
}

case 4:
{
clrscr();
printf("\nThe elements stored in the Stack are as
follows:\n");
if(top==-1)
{
printf("The stack is empty\n");
}
else
{
for(i=top; i>=0; i--)
{
printf("%d", stack[i]);
if(i > 0)
{
printf(" | ");
}
}
printf("\n");
}
getch();
break;
}

case 5:
{
clrscr();
printf("Thanks for Joining!\n");
getch();
break;
}

default:
{
clrscr();
printf("You have entered invalid number.\n");
getch();
break;
}
}
}while(option!=5);
return 0;
}

void push(int value, int stack[])


{
if(top==MAX-1)
{
printf("Stack is Overflow.\n");
}
else
{
top++;
stack[top]=value;
}
}

int pop(int stack[])


{
int value;
if(top==-1)
{
printf("Stack is underflow.");
return -1;
}
else
{
value=stack[top];
top--;
return value;
}
}

int peek(int stack[])


{
if(top==-1)
{
printf("Stack is Empty.\n");
return -1;
}
else
return (stack[top]);
}

Output:

You might also like