0% found this document useful (0 votes)
4 views17 pages

Stack&Queue Assignment

The document contains an assignment on data structures focusing on implementing a two-way stack, a stack using a queue, and a queue using stacks. Each implementation includes C code with functionalities for pushing, popping, and displaying elements. The assignment is completed by a student named Ojasva with SAP ID 590026974.

Uploaded by

aryanxnn41
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)
4 views17 pages

Stack&Queue Assignment

The document contains an assignment on data structures focusing on implementing a two-way stack, a stack using a queue, and a queue using stacks. Each implementation includes C code with functionalities for pushing, popping, and displaying elements. The assignment is completed by a student named Ojasva with SAP ID 590026974.

Uploaded by

aryanxnn41
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

DSA – STACK AND QUEUE – ASSIGNMENT

NAME : OJASVA
SAP ID : 590026974
BATCH : 36
Q1. IMPLEMENT A 2 WAY STACK.
CODE :
#include <stdio.h>
#define MAX 10
int main()
{
int a[MAX];
int top1 = -1;
int top2 = MAX;
int ch, x, i;

while (1)
{
printf("\n1. Push in Stack 1");
printf("\n2. Push in Stack 2");
printf("\n3. Pop from Stack 1");
printf("\n4. Pop from Stack 2");
printf("\n5. Display");
printf("\n6. Exit");
printf("\nEnter choice: ");
scanf("%d", &ch);

switch (ch)
{
case 1:
if (top1 + 1 == top2)
{
printf("Overflow\n");
}
else
{
printf("Enter value: ");
scanf("%d", &x);
top1++;
a[top1] = x;
}
break;

case 2:
if (top1 + 1 == top2)
{
printf("Overflow\n");
}
else
{
printf("Enter value: ");
scanf("%d", &x);
top2--;
a[top2] = x;
}
break;

case 3:
if (top1 == -1)
{
printf("Stack 1 Underflow\n");
}
else
{
printf("Popped from Stack 1 = %d\n", a[top1]);
top1--;
}
break;

case 4:
if (top2 == MAX)
{
printf("Stack 2 Underflow\n");
}
else
{
printf("Popped from Stack 2 = %d\n", a[top2]);
top2++;
}
break;

case 5:
printf("Stack 1: ");
for (i = 0; i <= top1; i++)
{
printf("%d ", a[i]);
}

printf("\nStack 2: ");
for (i = MAX - 1; i >= top2; i--)
{
printf("%d ", a[i]);
}
printf("\n");
break;
case 6:
return 0;

default:
printf("Wrong choice\n");
}
}

return 0;
}

OUTPUT :
Q2 . IMPLEMENT STACK USING QUEUE.

CODE :
#include <stdio.h>
#define MAX 20

int main()
{
int q[MAX];
int front = -1, rear = -1;
int ch, x, i, size, temp;

while (1)
{
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter choice: ");
scanf("%d", &ch);

switch (ch)
{
case 1:
if (rear == MAX - 1)
{
printf("Stack Overflow\n");
}
else
{
printf("Enter value: ");
scanf("%d", &x);
if (front == -1)
front = 0;

rear++;
q[rear] = x;

size = rear - front;

for (i = 0; i < size; i++)


{
temp = q[front];
front++;

rear++;
q[rear] = temp;
}
}
break;

case 2:
if (front == -1 || front > rear)
{
printf("Stack Underflow\n");
}
else
{
printf("Popped element = %d\n", q[front]);
front++;

if (front > rear)


{
front = -1;
rear = -1;
}
}
break;

case 3:
if (front == -1 || front > rear)
{
printf("Stack is Empty\n");
}
else
{
printf("Stack elements: ");
for (i = front; i <= rear; i++)
{
printf("%d ", q[i]);
}
printf("\n");
}
break;

case 4:
return 0;

default:
printf("Wrong choice\n");
}
}

return 0;
}
OUTPUT :
Q3. IMPLEMENT QUEUE USING STACK .
CODE :
#include <stdio.h>
#define MAX 10
int main()
{
int s1[MAX], s2[MAX];
int top1 = -1, top2 = -1;
int ch, x, i, val;

while (1)
{
printf("\n1. Enqueue");
printf("\n2. Dequeue");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter choice: ");
scanf("%d", &ch);

switch (ch)
{
case 1:
if (top1 == MAX - 1)
{
printf("Queue Overflow\n");
}
else
{
printf("Enter value: ");
scanf("%d", &x);
top1++;
s1[top1] = x;
}
break;

case 2:
if (top1 == -1 && top2 == -1)
{
printf("Queue Underflow\n");
}
else
{
if (top2 == -1)
{
while (top1 != -1)
{
top2++;
s2[top2] = s1[top1];
top1--;
}
}
val = s2[top2];
top2--;
printf("Dequeued element = %d\n", val);
}
break;

case 3:
if (top1 == -1 && top2 == -1)
{
printf("Queue is Empty\n");
}
else
{
printf("Queue elements: ");

for (i = top2; i >= 0; i--)


{
printf("%d ", s2[i]);
}

for (i = 0; i <= top1; i++)


{
printf("%d ", s1[i]);
}
printf("\n");
}
break;

case 4:
return 0;

default:
printf("Wrong choice\n");
}
}

return 0;
}
OUTPUT :

You might also like