Experiment 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Experiment No.

1
Stack ADT using Array
Write a program to implement Stack ADT using Array

#include <stdio.h>
#define SIZE 5 // Define the maximum size of the stack

// Stack structure
struct Stack {
int items[SIZE];
int top;
};

// Initialize the stack


void initialize(struct Stack* stack) {
stack->top = -1;
}

// Check if the stack is full


int isFull(struct Stack* stack) {
return (stack->top == SIZE - 1);
}

// Check if the stack is empty


int isEmpty(struct Stack* stack) {
return (stack->top == -1);
}

// Push an element onto the stack


void push(struct Stack* stack, int value) {
if (isFull(stack)) {
printf("Stack is full! Cannot push %d\n", value);
} else {
stack->items[++(stack->top)] = value;
printf("Pushed %d onto the stack\n", value);
}
}

// Pop an element from the stack


int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty! Cannot pop\n");
return -1;
} else {
int value = stack->items[(stack->top)--];
printf("Popped %d from the stack\n", value);
return value;
}
}

// Peek the top element of the stack


int peek(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty! Nothing to peek\n");
return -1;
} else {
return stack->items[stack->top];
}
}

// Display the elements in the stack


void display(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
} else {
printf("Stack elements are: ");
for (int i = stack->top; i >= 0; i--) {
printf("%d ", stack->items[i]);
}
printf("\n");
}
}

// Main function to test stack operations


int main() {
struct Stack stack;
initialize(&stack);

int choice, value;

while (1) {
printf("\nStack Menu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the value to push: ");
scanf("%d", &value);
push(&stack, value);
break;

case 2:
pop(&stack);
break;

case 3:
value = peek(&stack);
if (value != -1) {
printf("Top element is %d\n", value);
}
break;

case 4:
display(&stack);
break;

case 5:
printf("Exiting...\n");
return 0;

default:
printf("Invalid choice! Please try again.\n");
break;
}
}
}

OUTPUT
Stack Menu:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 4
Stack is empty
Stack Menu:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 4
Stack is empty

Stack Menu:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the value to push: 56
Pushed 56 onto the stack

Stack Menu:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the value to push: 56
Pushed 56 onto the stack

Stack Menu:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the value to push: 48
Pushed 48 onto the stack

Stack Menu:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the value to push: 78
Pushed 78 onto the stack

Stack Menu:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 4
Stack elements are: 78 48 56 56

Stack Menu:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 2
Popped 78 from the stack

Stack Menu:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 2
Popped 48 from the stack

Stack Menu:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 3
Top element is 56

Stack Menu:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 4
Stack elements are: 56 56

Stack Menu:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: . 5
Exiting...

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy