Stack using Array in C

Stack using Array in C

Here you will get code to learn the Stack using Array in C programming using data structure.

Stack using Array

In computer science, a stack is a linear data structure that follows to the Last-In-First-Out (LIFO) concept. It stores and manages a collection of elements where items are added and removed from one end, called the top. Common operations on a stack include push (add) and pop (remove) operations.

stack using array

Algorithm for Stack using Array

1. Create a fixed-size array to store stack elements.

2. Initialize a variable ‘top’ to -1 to represent an empty stack.

3. Define functions for basic stack operations:

Push(item): Increment ‘top’ and add ‘item’ to the array at the ‘top’ index.
Pop(): Return the element at the ‘top’ index and decrement ‘top’.
IsEmpty(): Check if ‘top’ is -1 to determine if the stack is empty.
IsFull(): Check if ‘top’ is equal to the maximum array size minus one.

4. Optionally, define a function ‘Peek()’ to retrieve the top element without removing it.

5. Use these operations to manipulate the stack as needed.

Program code Stack using Array in C

Output

1.Push
2.Pop
3.Display
4.Exit

Enter your choice: 1

Enters the number to be pushed : 10

Enter your choice: 1

Enters the number to be pushed : 20

Enter your choice: 1

Enters the number to be pushed : 30

Enter your choice: 3

Contents of stack : 30 20 10
Enter your choice: 2

Deleted one is 30
Enter your choice: 3

Contents of stack : 20 10
Enter your choice: 4

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top