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();
}
@Bikash2510
Copy link

There are 6 error showing while compiling the programme

Copy link

ghost commented Aug 15, 2017

//good

@Dipk05
Copy link

Dipk05 commented Sep 17, 2017

Helpful

@sgrpwr
Copy link

sgrpwr commented Oct 6, 2017

This is the best implementation of stack I've every seen. Thank you :)

@patrickpatso
Copy link

thanks a lot sir

@rohitrohan2009
Copy link

Thanks a lot, sir. I have used this code in my assignment of Stack implementation.

@Inquizarus
Copy link

I just found this through youtube today, thanks for the clear explanations .

One question though!

Isn't this Pop implementation wrong? This implementation only decreases the top index value but
nothing is removed/cleared from the array, so in reality the 10 is still there when printing after the pop and is
then overwritten by the value 12 before next print.

I'm not very familiar with C code at all so perhaps this is something that is taken care of for you?

@aman-roy
Copy link

aman-roy commented Jun 7, 2018

@Inquizarus If you see the implementation of Print() function then your doubt will be cleared. When we are doing the print, We are only concerned about the values till the top. What is after that is just garbage value for us. Whenever we want to push a new element, Only then we increase the top value by 1 and put our value there. Now the print function will look only till our new top, not after that.

@arjun0699
Copy link

thanks for the whole series on data structures.

@atharva-mishra
Copy link

nice explanation

@uday966666
Copy link

how can you implement this using struct in c
not using arrays data structure

@javeedsanganakal
Copy link

javeedsanganakal commented Mar 27, 2019

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

@hmsathyajith
Copy link

what is that time-consuming concept? I couldn't get it.

@kazinoman
Copy link

if(We learn) {
printf("That's awesome Sir.");
}
printf("Try again to learn.");

@abhishek-dash
Copy link

very efficient!!!

@SanjeevAshoka
Copy link

Sir, while printing elements of the stack -you are not accessing it from the top(as stack follow the rule of LIFO).
How is this code is working?.......

@adityakishan2018
Copy link

@SanjeevAshoka He implemented print function just to verify the present state of stack after each pop/push operation. He need not follow LIFO order for that.

@asadasivam
Copy link

Is there a source code for stack using struct/ object implementation?

@konpozzhr
Copy link

Thank a lot sir

@shania-balkhi
Copy link

Thank you!

@gethubusercp
Copy link

I just found this through youtube today, thanks for the clear explanations .

One question though!

Isn't this Pop implementation wrong? This implementation only decreases the top index value but nothing is removed/cleared from the array, so in reality the 10 is still there when printing after the pop and is then overwritten by the value 12 before next print.

I'm not very familiar with C code at all so perhaps this is something that is taken care of for you?
When you change top, you will not print the extra number, for you just print elements from 0 to top, the element is still there, but it doesn't matter, for you can overwritten it with push operation.

@SanjeevAshoka
Copy link

SanjeevAshoka commented Oct 18, 2021 via email

@piyushpp07
Copy link

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

his friend is no longer so the stopped uploading videos

@ShubhamJagtap2000
Copy link

There are 6 error showing while compiling the programme

They have provided raw code, you can have your own one

@ShubhamJagtap2000
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?

@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