Dsa Lab 1
Dsa Lab 1
Dsa Lab 1
h>
#include<stdlib.h>
#define size 10
int top = -1;
int stack[size];
void pop()
{
int item;
if (top == -1)
printf("\nStack is Empty. Pop operation is not
allowed\n");
else
{
item = stack[top];
top--;
printf("\n%d is popped from stack\n",item);
}
}
void display()
{
printf("\nCurrent status of stack is:\n");
for(int i=0; i<=top; i++)
{
printf("%d\t",stack[i]);
}
}
void isEmpty()
{
if (top == -1)
printf("\nStack is Empty\n");
}
void isFull()
{
if (top == size-1)
printf("\nStack is Full or Stack Overflow\n");
}
int peek()
{
int item;
item = stack[top];
return item;
}
void main()
{
int choice,item,item1;
while(1)
{
printf("\nEnter the operation to be performed\n");
printf("1. PUSH\n2. POP\n3. Status of Stack\n4.
IsEmpty\n5. IsFull\n6. Peek\n7. Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter an element to push\n");
scanf("%d",&item);
push(item);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: isEmpty();
break;
case 5: isFull();
break;
case 6: item1 = peek();
printf("\nTop most element of stack is
%d\n",item1);
break;
case 7: exit(0);
break;
default: printf("\nWrong choice\n");
}
}
}
Following questions to be answered in lab record:
1. Since arrays have a fixed size, how would you handle situations
where the number of elements in a stack exceeds the initial
capacity of the array?
2. What are the trade-offs between stack using dynamic array and
static array?
3. Describe how a stack overflow condition is handled in an array-
based implementation of a stack. How would you modify the
implementation to avoid overflow errors or handle them more
efficiently?
4. Write a pop() function which prints the index of top element
and returns the element to main() function.