Skip to content

Instantly share code, notes, and snippets.

@catupper
Created September 13, 2012 16:10
Show Gist options
  • Select an option

  • Save catupper/3715406 to your computer and use it in GitHub Desktop.

Select an option

Save catupper/3715406 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<stdlib.h>
typedef int it; //various type
struct stack_s{
it x;
struct stack_s * before;
struct stack_s * next;
};
struct my_stack{
int len;
struct stack_s * last;
};
it top(struct my_stack * st){
if(st -> len == 0)return -1;
return st -> last -> x;
}
void push(struct my_stack * st, it val){
if(st->len != 0){
st -> last -> next = (struct stack_s *)malloc(sizeof(struct stack_s));
st -> last -> next -> before = st -> last;
st -> last = st -> last -> next;
st -> last -> x = val;
st -> last -> next = 0;
st -> len += 1;
}
else{
st -> last = (struct stack_s *) malloc(sizeof(struct stack_s));
st -> last -> x = val;
st -> last -> before = 0;
st -> last -> next = 0;
st -> len += 1;
}
}
int pop(struct my_stack * st){
if(st -> len == 0)return 0;
if(st -> len == 1){
st -> last = 0;
st -> len -= 1;
}
else{
st -> last = st -> last -> before;
st -> len -= 1;
free(st -> last -> next);
}
return 1;
}
int len(struct my_stack * st){
return st -> len;
}
int main(){
int i,n;
struct my_stack test;
test . len = 0;
while(1){
scanf("%d",&n);
if(n == -1)return 0;
if(n == 0)printf("%d, %d\n",pop(&test) ,top(&test));
if(n > 0)push(&test,n);
}
}
#include<stdio.h>
#include<stdlib.h>
typedef int it;
struct stack_s{
it x;
struct stack_s * before;
};
struct my_stack{
struct stack_s * last;
};
it top(struct my_stack * st){
if(st -> last == 0)return -1;
return st -> last -> x;
}
void push(struct my_stack * st, it val){
if(st -> last != 0){
struct stack_s * before = st -> last;
st -> last = (struct stack_s *)malloc(sizeof(struct stack_s));
st -> last -> before = before;
st -> last -> x = val;
}
else{
st -> last = (struct stack_s *) malloc(sizeof(struct stack_s));
st -> last -> x = val;
st -> last -> before = 0;
}
}
int pop(struct my_stack * st){
if(st -> last == 0)return 0;
if(st -> last -> before == 0){
st -> last = 0;
}
else{
struct stack_s * next = st -> last;
st -> last = st -> last -> before;
free(next);
}
return 1;
}
int main(){
int i,n;
struct my_stack test;
test . last = 0;
while(1){
scanf("%d",&n);
if(n == -1)return 0;
if(n == 0)printf("%d, %d\n",pop(&test) ,top(&test));
if(n > 0)push(&test,n);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment