Summary: in this tutorial, you will learn about stack data structure and how to implement a C stack using an array.
Introduction to stack data structure
A stack is a data structure that works based on the principle of last-in-first-out (LIFO). It means the last element that was added to the stack must be the first one to be removed.
Stack data structure has many applications such as parsing the syntax of expressions, managing run-time memory (used in Java virtual machine) and using in searching algorithms.
C stack operations
There are two main operations that you can perform in the stack:
push
: allows you to insert an element into the stack.pop
: allows you to remove an element from the stack.
In addition, a stack can have:
empty
: check if the stack is emptyfull
: check if the stack is fullinit
: initialize the stack pointerdisplay
: display the content of the stack
C stack implementation using array
The following is C source code that demonstrates a stack data structure.
Stack header file: stack.h
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
void push(int *s,int* top, int element);
int pop(int *s,int *top);
int full(int *top,const int size);
int empty(int *top);
void init(int *top);
void display(int *s,int *top);
#endif // STACK_H_INCLUDED
Code language: C++ (cpp)
Stack code file: stack.c
(stack implementation in C)
/*
initialize stack pointer
*/
void init(int *top)
{
*top = 0;
}
/*
push an element into stack
precondition: the stack is not full
*/
void push(int *s,int* top, int element)
{
s[(*top)++] = element;
}
/*
remove an element from stack
precondition: stack is not empty
*/
int pop(int *s,int *top)
{
return s[--(*top)];
}
/*
return 1 if stack is full, otherwise return 0
*/
int full(int *top,const int size)
{
return *top == size ? 1 : 0;
}
/*
return 1 if the stack is empty, otherwise return 0
*/
int empty(int *top)
{
return *top == 0 ? 1 : 0;
}
/*
display stack content
*/
void display(int *s,int *top)
{
printf("Stack: ");
int i;
for(i = 0; i < *top; i++)
{
printf("%d ",s[i]);
}
printf("\n");
}
Code language: C++ (cpp)
We can test the stack data structure that uses an array in the main.c file (stack program in C)
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
int main()
{
const int SIZE = 5; /* stack size */
int top, elem;
int stack[SIZE];
init(&top);
printf("--Push elements into stack --\n");
/* push elements into stack */
while(!full(&top,SIZE))
{
printf("Enter a number to push into the stack:");
scanf("%d",&elem);
push(stack,&top,elem);
display(stack,&top);
}
printf("Stack is full\n\n");
printf("--Pop elements into stack --\n");
while(!empty(&top))
{
elem = pop(stack,&top);
printf("pop %d from the stack\n",elem);
display(stack,&top);
}
printf("Stack is empty\n");
return 0;
}
Code language: C++ (cpp)
The following is the output of the program:
Code language: plaintext (plaintext)--Push elements into stack -- Enter a number to push into the stack:1 Stack: 1 Enter a number to push into the stack:2 Stack: 1 2 Enter a number to push into the stack:3 Stack: 1 2 3 Enter a number to push into the stack:4 Stack: 1 2 3 4 Enter a number to push into the stack:5 Stack: 1 2 3 4 5 Stack is full --Pop elements into stack -- pop 5 from the stack Stack: 1 2 3 4 pop 4 from the stack Stack: 1 2 3 pop 3 from the stack Stack: 1 2 pop 2 from the stack Stack: 1 pop 1 from the stack Stack: Stack is empty
In this tutorial, you have learned what a stack is and how to implement C stack data structure using an array.