Skip to content

Instantly share code, notes, and snippets.

@mycodeschool
Last active April 7, 2024 23:28
Show Gist options
  • Star 56 You must be signed in to star a gist
  • Fork 25 You must be signed in to fork a gist
  • Save mycodeschool/6878252 to your computer and use it in GitHub Desktop.
Save mycodeschool/6878252 to your computer and use it in GitHub Desktop.
This is a basic array based implementation of stack data structure in C.
// Stack - Array based implementation.
// Creating a stack of integers.
#include<stdio.h>
#define MAX_SIZE 101
int A[MAX_SIZE]; // integer array to store the stack
int top = -1; // variable to mark top of stack in array
// Push operation to insert an element on top of stack.
void Push(int x)
{
if(top == MAX_SIZE -1) { // overflow case.
printf("Error: stack overflow\n");
return;
}
A[++top] = x;
}
// Pop operation to remove an element from top of stack.
void Pop()
{
if(top == -1) { // If stack is empty, pop should throw error.
printf("Error: No element to pop\n");
return;
}
top--;
}
// Top operation to return element at top of stack.
int Top()
{
return A[top];
}
// This function will return 1 (true) if stack is empty, 0 (false) otherwise
int IsEmpty()
{
if(top == -1) return 1;
return 0;
}
// This function is just to test the implementation of stack.
// This will print all the elements in the stack at any stage.
void Print() {
int i;
printf("Stack: ");
for(i = 0;i<=top;i++)
printf("%d ",A[i]);
printf("\n");
}
int main() {
// Code to test the implementation.
// calling Print() after each push or pop to see the state of stack.
Push(2);Print();
Push(5);Print();
Push(10);Print();
Pop();Print();
Push(12);Print();
}
@gethubusercp
Copy link

Find this Guy, Where he is.....We want all Subjects of CS

his friend is no longer so the stopped uploading videos

So sad, how did you know?

You can know more about the author through this article-->https://www.freecodecamp.org/news/mycodeschool-youtube-channel-history/

@HaozeLiu
Copy link

Your video is very helpful for me,it really got me started with data structures.

@ericBlack1
Copy link

Thanks alot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment