Skip to content

Commit

Permalink
stack implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
shiningflash authored Sep 3, 2019
1 parent 4ddd607 commit f1df20f
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <bits/stdc++.h>
using namespace std;
#define SIZE 10

typedef struct {
int top;
int arr[SIZE];
} Stack;

void push(Stack *st, int item) {
if (st->top == SIZE) {
printf("Error: Stack is full\n");
return;
}
st->arr[st->top] = item;
st->top = st->top + 1;
}

int pop(Stack *st) {
if (st->top == 0) {
printf("Error: Stack is empty\n");
exit(EXIT_FAILURE);
}
st->top = st->top - 1;
return st->arr[st->top];
}

int main() {
Stack mystack;
mystack.top = 0;
push(&mystack, 1);
push(&mystack, 2);
push(&mystack, 3);
printf("%d\n", pop(&mystack));
printf("%d\n", pop(&mystack));
printf("%d\n", pop(&mystack));
printf("%d\n", pop(&mystack));
return 0;
}

0 comments on commit f1df20f

Please sign in to comment.