Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create stacks_using_arrays.c #1439

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions data structures/stack/c/stacks_using_arrays.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//Kumar Ankit
#include<stdio.h>
#include<stdlib.h>
#define MAX 20

typedef struct{
int data[MAX];
int top;
}STACK;

//Note: When *S is used in a function, (*S).top is same as S->top

int push(STACK *S, int v){
if(S->top == MAX-1){
printf("Overflow\n");
return 1;
}
S->top++;
S->data[S->top] = v;
return 0;
}

int pop(STACK *S, int *v){
if(S->top == -1){
printf("Underflow\n");
return 1;
}
*v = S->data[S->top];
S->top--;
return 0;
}

void display(STACK *S){//using recursion
if(S->top == -1) return;
int u;
pop(S, &u);
printf("%d ", u);
display(S);
push(S, u);
}

int main(){
int m;
STACK S1;
S1.top = -1;
int k = push(&S1, 15);
display(&S1);
int q = pop(&S1, &m);
return 0;
}