Part 2 Stack Data Structure Algoritm and C Program Examples 1
Part 2 Stack Data Structure Algoritm and C Program Examples 1
What is a Stack?
A stack is a linear data structure where elements are stored in the LIFO (Last In First
Out) principle where the last element inserted would be the first element to be deleted. A
stack is an Abstract Data Type (ADT), that is popularly used in most programming
languages. It is named stack because it has the similar operations as the real-world
stacks, for example − a pack of cards or a pile of plates, etc.
Stack is considered a complex data structure because it uses other data structures for
implementation, such as Arrays, Linked lists, etc.
Stack Representation
A stack allows all data operations at one end only. At any given time, we can only access
the top element of a stack.
A stack can be implemented by means of Array, Structure, Pointer, and Linked List. Stack
can either be a fixed size one or it may have a sense of dynamic resizing. Here, we are
going to implement stack using arrays, which makes it a fixed size stack implementation.
https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm 1/12
10/29/24, 10:57 AM Stack Data Structure
Explore our latest online courses and learn new skills at your own pace. Enroll and
become a certified expert to boost your career.
The most fundamental operations in the stack ADT include: push(), pop(), peek(), isFull(),
isEmpty(). These are all built-in operations to carry out data manipulation and to check
the status of the stack.
Stack uses pointers that always point to the topmost element within the stack, hence
called as the top pointer.
Algorithm
Example
Following are the implementations of this operation in various programming languages −
Open Compiler
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm 2/12
10/29/24, 10:57 AM Stack Data Structure
/* Main function */
int main(){
int i;
push(44);
push(10);
push(62);
push(123);
push(15);
printf("Stack Elements: \n");
Output
Stack Elements:
44 10 62 123 15 0 0 0
Note − In Java we have used to built-in method push() to perform this operation.
https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm 3/12
10/29/24, 10:57 AM Stack Data Structure
Algorithm
Example
Following are the implementations of this operation in various programming languages −
Open Compiler
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm 4/12
10/29/24, 10:57 AM Stack Data Structure
/* Main function */
int main(){
int i;
push(44);
push(10);
push(62);
push(123);
push(15);
printf("Stack Elements: \n");
https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm 5/12
10/29/24, 10:57 AM Stack Data Structure
while(!isempty()) {
int data = pop();
printf("%d ",data);
}
return 0;
}
Output
Stack Elements:
44 10 62 123 15 0 0 0
Elements popped:
15 123 62 10 44
Algorithm
1. START
2. return the element at the top of the stack
3. END
Example
Following are the implementations of this operation in various programming languages −
Open Compiler
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm 6/12
10/29/24, 10:57 AM Stack Data Structure
/* Main function */
int main(){
int i;
push(44);
push(10);
push(62);
push(123);
push(15);
printf("Stack Elements: \n");
https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm 7/12
10/29/24, 10:57 AM Stack Data Structure
Output
Stack Elements:
44 10 62 123 15 0 0 0
Element at top of the stack: 15
Algorithm
1. START
2. If the size of the stack is equal to the top position of the stack,
the stack is full. Return 1.
3. Otherwise, return 0.
4. END
Example
Following are the implementations of this operation in various programming languages −
Open Compiler
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
/* Main function */
https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm 8/12
10/29/24, 10:57 AM Stack Data Structure
int main(){
printf("Stack full: %s\n" , isfull()?"true":"false");
return 0;
}
Output
Stack full: false
Algorithm
1. START
2. If the top value is -1, the stack is empty. Return 1.
3. Otherwise, return 0.
4. END
Example
Following are the implementations of this operation in various programming languages −
Open Compiler
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm 9/12
10/29/24, 10:57 AM Stack Data Structure
/* Main function */
int main() {
printf("Stack empty: %s\n" , isempty()?"true":"false");
return 0;
}
Output
Stack empty: true
Open Compiler
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
/* Check if the stack is empty */
int isempty(){
if(top == -1)
return 1;
else
return 0;
}
/* Check if the stack is full */
int isfull(){
if(top == MAXSIZE)
return 1;
else
return 0;
}
https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm 10/12
10/29/24, 10:57 AM Stack Data Structure
return stack[top];
}
https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm 11/12
10/29/24, 10:57 AM Stack Data Structure
return 0;
}
Output
Element at top of the stack: 15
Elements:
15123
62
10
44
Stack full: false
Stack empty: true
Stack Implementation in C
Click to check the implementation of Stack Program using C
https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm 12/12