Skip to content

Instantly share code, notes, and snippets.

@clive819
Last active November 5, 2016 12:41
Show Gist options
  • Save clive819/ea33fe3ccffbf05f3d7edde23640c485 to your computer and use it in GitHub Desktop.
Save clive819/ea33fe3ccffbf05f3d7edde23640c485 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
struct NODE{
int data;
struct NODE *link;
};
struct STACK{
int count;
struct NODE *top;
};
bool is_empty(struct STACK *s){
if (s->count == 0) {
return true;
}
return false;
}
void push(struct STACK *s, int score){
struct NODE *pnew = (struct NODE*)malloc(sizeof(struct NODE)); //new NODE;
pnew->data = score;
if (is_empty(s)) {
pnew->link = NULL;
}
else{
pnew->link = s->top;
}
s->count++;
s->top = pnew;
}
int pop(struct STACK *s){
if (!is_empty(s)) {
int data = s->top->data;
struct NODE *old = s->top;
s->top = s->top->link;
s->count--;
free(old);
return data;
}
return NULL;
}
int main(){
struct STACK s1, s2;
s1.count=0;
s2.count=0;
push(&s1, 88);
push(&s1, 60);
push(&s1, 37);
push(&s1, 50);
for (int i=0; i<4; i++) {
int data = pop(&s1);
printf("stack1 - %d : %d\n", i, data);
push(&s2, data);
}
printf("\n");
for (int i=0; i<4; i++) {
printf("stack2 - %d : %d\n", i, pop(&s2));
}
printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment