Implement Stack in C Array

Check out all articles in Technical Article Structure on Medium

Yu-Ming, CHANG (he/him)
2 min readDec 25, 2021

General Introduction

Stack is a linear data structure with an order of FILO, which means First In Last Out.

It is common to see stack in real world, such as plates in a restaurant. The plate at the bottom is the first to put in, but will be the last one to be used. Stack is also present at the bottle that you store your coffee beans, or even your Pringles.

In programming world, stack is used when the processing order is not important, or when the purpose of that program is similar to checking syntax correctness. Picture below is from Geeks for Geeks. I find it very useful to understand the logic of stack.

Implementation

To implement stack in programming language, we often need to code 6 functions.

  1. createStack — this function will be used to allocate memory and initialize stack, where stack is a custom class
  2. isEmpty — this function returns TRUE if the stack is empty
  3. isFull — this function returns TRUE if the stack is full
  4. Push — to add a new item to the top of the stack
  5. Pop — to retrieve an item from the top, and remove it from the stack
  6. Top — to see the item on the top of the stack

Gist Example

--

--

Yu-Ming, CHANG (he/him)

I enjoy the positive mind flow when writing code to solve a problem. This is my journey to become a software developer, though now working as a product owner