0% found this document useful (0 votes)
10 views6 pages

Stack Operations and Palindrome Check

The document outlines a lab assignment focused on implementing stack operations (Push, Pop, Peek, Display) and checking if a number is a palindrome using a stack. It includes a theoretical overview of stack data structures, an array-based implementation in C, and a series of test cases to validate the functionality. The objective is to enhance understanding of stack operations and their applications in programming.

Uploaded by

cinephiles10
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)
10 views6 pages

Stack Operations and Palindrome Check

The document outlines a lab assignment focused on implementing stack operations (Push, Pop, Peek, Display) and checking if a number is a palindrome using a stack. It includes a theoretical overview of stack data structures, an array-based implementation in C, and a series of test cases to validate the functionality. The objective is to enhance understanding of stack operations and their applications in programming.

Uploaded by

cinephiles10
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

Data Structures Lab (Journal Template)

Date: ________________________

Problem Statement:
Implement stack operations (Push, Pop, Peek, Display) and check whether a given number
is a palindrome using a stack.

Objective of the Term Work:


To understand and implement stack data structure operations and use the stack to check if
a number is a palindrome.

Theory:
Abstract Data Type (ADT): Stack
A Stack is a linear data structure that follows the LIFO (Last In First Out) principle.
Operations on stack:
1. Push(x): Insert an element into the stack.
2. Pop(): Remove the top element from the stack.
3. Peek(): Return the top element without removing it.
4. isEmpty(): Check if the stack is empty.
5. isFull(): Check if the stack is full.

A stack can be implemented using arrays or linked lists. In this experiment, an array-based
stack is used to perform operations and check whether a number is a palindrome.

Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define MAX 10 // Maximum size of the stack

struct Stack {
int top;
unsigned capacity;
char array[MAX];
};

struct Stack* createStack(unsigned capacity) {


struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
return stack;
}

bool isFull(struct Stack* stack) {


return stack->top == stack->capacity - 1;
}

bool isEmpty(struct Stack* stack) {


return stack->top == -1;
}

void push(struct Stack* stack, char item) {


if (isFull(stack)) {
printf("Stack Overflow\n");
return;
}
stack->array[++stack->top] = item;
printf("Pushed to stack: %c\n", item);
}

char pop(struct Stack* stack) {


if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;
}
return stack->array[stack->top--];
}

char peek(struct Stack* stack) {


if (isEmpty(stack)) {
printf("Stack is Empty\n");
return -1;
}
return stack->array[stack->top];
}

void display(struct Stack* stack) {


if (isEmpty(stack)) {
printf("Stack is empty\n");
return;
}
printf("Stack elements are:\n");
for (int i = stack->top; i >= 0; i--)
printf("%c\n", stack->array[i]);
}

bool isPalindrome(char str[], struct Stack* stack) {


int length = strlen(str);

for (int i = 0; i < length; i++) {


push(stack, str[i]);
}

for (int i = 0; i < length; i++) {


if (str[i] != pop(stack))
return false;
}

return true;
}

int main() {
struct Stack* stack = createStack(MAX);
int choice;
char item;

while (1) {
printf("\n1. Push to stack\n2. Pop from stack\n3. Display top element\n4. Display all
stack elements\n5. Check if a number is a palindrome\n6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter a character to push: ");
scanf(" %c", &item);
push(stack, item);
break;
case 2:
item = pop(stack);
if (item != -1)
printf("Popped from stack: %c\n", item);
break;
case 3:
item = peek(stack);
if (item != -1)
printf("Top element is: %c\n", item);
break;
case 4:
display(stack);
break;
case 5: {
char number[100];
printf("Enter a number: ");
scanf("%s", number);
if (isPalindrome(number, stack))
printf("The number is a palindrome.\n");
else
printf("The number is not a palindrome.\n");
break;
}
case 6:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}

Output:
Sample Input/Output:

Enter your choice: 1


Enter a character to push: 10
Pushed to stack: 10

Enter your choice: 1


Enter a character to push: 20
Pushed to stack: 20

Enter your choice: 4


Stack elements are:
30
20
10

Enter your choice: 2


Popped from stack: 30

Enter your choice: 5


Enter a number: 12321
The number is a palindrome.

Enter your choice: 6


Exiting...

Test Cases:
Test Case # Test Case Test Case Expected Actual Pass/Fail
Name Description Output Output

1 Pushing Push to Items Items Pass


Integers stack with pushed pushed
elements successfully successfully
like 10, 20,
30

2 Popping Pop after Top element Top element Pass


Integers pushing few popped popped
elements

3 Top Element Display top Displays Displays Pass


element correct top correct top
element element

4 Display All Display all All elements All elements Pass


Elements stack displayed displayed
elements

5 Palindrome Check with Palindrome Palindrome Pass


Positive number 121 detected detected

6 Palindrome Check with Not Not Pass


Negative number 123 palindrome palindrome

7 Empty Stack Pop without Stack Stack Pass


Pop pushing Underflow Underflow
elements message message

8 Full Stack Push beyond Stack Stack Pass


Push capacity Overflow Overflow
message message

9 Exit Choose exit Program Program Pass


Program option exits cleanly exits cleanly

References:
[1] Data Structures using C by Reema Thareja, Oxford University Press.
[2] [Link]

You might also like