Practical 7
Set a
a) Implement a stack library (ststack.h) of integers using a static implementation of the stack and
implementing the above six operations. Write a driver program that includes stack library and calls
different stackoperations.
#include<stdio.h>
#include<conio.h>
#define MAX 100
typedef struct Stack {
int arr[MAX];
int top;
} Stack;
void initStack(Stack* s);
int isFull(Stack* s);
int isEmpty(Stack* s);
void push(Stack* s, int value);
int pop(Stack* s);
int peek(Stack* s);
void display(Stack* s);
#include <stdio.h>
#include <stdlib.h>
#include "ststack.h"
void initStack(Stack* s)
s->top = -1;
#include “ststack.h”
int isFull(Stack* s) {
return s->top == MAX - 1;
int isEmpty(Stack* s) {
return s->top == -1;
}
void push(Stack* s, int value) {
if (isFull(s)) {
printf("Stack overflow! Cannot push %d\n", value);
return;
s->arr[++(s->top)] = value;
int pop(Stack* s) {
if (isEmpty(s)) {
printf("Stack underflow! Cannot pop\n");
return -1;
return s->arr[(s->top)--];
int peek(Stack* s) {
if (isEmpty(s)) {
printf("Stack is empty! Cannot peek\n");
return -1;
return s->arr[s->top];
void display(Stack* s) {
if (isEmpty(s)) {
printf("Stack is empty!\n");
return;
printf("Stack elements: ");
for (int i = 0; i <= s->top; i++) {
printf("%d ", s->arr[i]);
}
printf("\n");
int main() {
Stack s;
int choice, value;
initStack(&s);
while (1) {
printf("\nMenu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. IsEmpty\n");
printf("5. IsFull\n");
printf("6. Display\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(&s, value);
break;
case 2:
value = pop(&s);
if (value != -1) {
printf("Popped element: %d\n", value);
break;
case 3:
value = peek(&s);
if (value != -1) {
printf("Top element: %d\n", value);
break;
case 4:
if (isEmpty(&s)) {
printf("Stack is empty.\n");
} else {
printf("Stack is not empty.\n");
break;
case 5:
if (isFull(&s)) {
printf("Stack is full.\n");
} else {
printf("Stack is not full.\n");
break;
case 6:
display(&s);
break;
case 7:
exit(0);
default:
printf("Invalid choice. Try again.\n");
return 0;
}
Set b
a) Write a program to check whether the contents of two stacks are identical. Use stack library to
perform basic stack operations. Neither stack should be changed
#include <stdio.h>
#include <stdlib.h>
#include "ststack.h"
int areStacksIdentical(Stack s1, Stack s2) {
Stack aux1, aux2;
initStack(&aux1);
initStack(&aux2);
if ([Link] != [Link]) {
return 0; // Not identical if sizes are different
while (!isEmpty(&s1) && !isEmpty(&s2)) {
int top1 = peek(&s1);
int top2 = peek(&s2);
if (top1 != top2) {
return 0;
push(&aux1, pop(&s1));
push(&aux2, pop(&s2));
while (!isEmpty(&aux1)) {
push(&s1, pop(&aux1));
while (!isEmpty(&aux2)) {
push(&s2, pop(&aux2));
return 1;
int main() {
Stack s1, s2;
initStack(&s1);
initStack(&s2);
push(&s1, 10);
push(&s1, 20);
push(&s1, 30);
push(&s2, 10);
push(&s2, 20);
push(&s2, 30);
if (areStacksIdentical(s1, s2)) {
printf("The stacks are identical.\n");
} else {
printf("The stacks are not identical.\n");
return 0;
}
Set c
a) In dynamic implementation of stack, How to modify pop operation so that it also returns the
popped element as an argument of the pop function?
#include <stdio.h>
#include <stdlib.h>
typedef struct Stack {
int *array;
int top;
int capacity;
} Stack;
// Function to create a stack of given capacity
Stack* createStack(int capacity) {
Stack *stack = (Stack *)malloc(sizeof(Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (int *)malloc(stack->capacity * sizeof(int));
return stack;
// Function to check if the stack is full
int isFull(Stack *stack) {
return stack->top == stack->capacity - 1;
// Function to check if the stack is empty
int isEmpty(Stack *stack) {
return stack->top == -1;
}
// Function to push an item to stack
void push(Stack *stack, int item) {
if (isFull(stack)) {
printf("Stack overflow!\n");
return;
stack->array[++stack->top] = item;
printf("%d pushed to stack\n", item);
// Modified pop function to return the popped element
int pop(Stack *stack, int *poppedElement) {
if (isEmpty(stack)) {
printf("Stack underflow!\n");
return -1; // Indicate failure
*poppedElement = stack->array[stack->top--];
return 0; // Indicate success
// Function to free stack memory
void freeStack(Stack *stack) {
free(stack->array);
free(stack);
// Main function to demonstrate stack operations
int main() {
Stack *stack = createStack(10);
push(stack, 10);
push(stack, 20);
push(stack, 30);
int poppedElement;
if (pop(stack, &poppedElement) == 0) {
printf("Popped element: %d\n", poppedElement);
if (pop(stack, &poppedElement) == 0) {
printf("Popped element: %d\n", poppedElement);
if (pop(stack, &poppedElement) == 0) {
printf("Popped element: %d\n", poppedElement);
// Attempting to pop from an empty stack
pop(stack, &poppedElement);
// Freeing allocated memory
freeStack(stack);
return 0;