Last active
July 8, 2024 06:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
commented
Oct 18, 2021
no, Because while pushing next time that value get overridden.
…On Mon, Oct 18, 2021 at 12:52 PM gethubusercp ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
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.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/6878252#gistcomment-3930688>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AOXR5WE7ZRL3ONIUHPTLZ63UHPDM3ANCNFSM4HM2NGJA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
Find this Guy, Where he is.....We want all Subjects of CS
his friend is no longer so the stopped uploading videos
There are 6 error showing while compiling the programme
They have provided raw code, you can have your own one
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?
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/
Your video is very helpful for me,it really got me started with data structures.
Thanks alot
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment