Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<stdio.h>
- void init(void);
- void add(int);
- int pop(void);
- void cleanup(void);
- void error(void);
- struct queue{
- int a;
- struct queue * next;
- };
- struct queue * last = 0;
- struct queue * now = 0;
- int count = 0;
- void init(){
- if((now = malloc(sizeof(struct queue))) == NULL) error();
- last = now;
- }
- void add(int x){
- if(now == 0) error();
- if((now->next = malloc(sizeof(struct queue))) == NULL) error();
- now->a = x;
- now = now->next;
- count++;
- }
- int pop(){
- if((now == 0) || (count == 0)) error();
- int temp;
- extern int count;
- struct queue * past = last;
- temp = last->a;
- last = last->next;
- count--;
- free(past);
- return temp;
- }
- void cleanup(){
- if(count == 0){
- last = 0;
- now = 0;
- }
- else{
- struct queue * past = last;
- while(count != 1){
- last = last->next;
- free(past);
- }
- free(last);
- }
- }
- void error(){
- fputs("ERROR", stderr);
- cleanup();
- exit(EXIT_FAILURE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement