Skip to content
Snippets Groups Projects
stack.c 996 B
Newer Older
  • Learn to ignore specific revisions
  • Douglas Guptill's avatar
    Douglas Guptill committed
    #include <stdlib.h>
    
    
    Douglas Guptill's avatar
    Douglas Guptill committed
    	static struct node
    	{ int key; struct node *next; };
    	static struct node *head, *z, *t;
    
    Douglas Guptill's avatar
    Douglas Guptill committed
            stackinit() {stackinit_();}
    
    Douglas Guptill's avatar
    Douglas Guptill committed
    	stackinit_() 
    	   {
    	     head = (struct node *) malloc(sizeof *head);
    	     z = (struct node *) malloc(sizeof *z);
    	     head->next = z; head->key=0;
    	     z->next = z;
    	     z->key = 0;
    	   }
    
    Douglas Guptill's avatar
    Douglas Guptill committed
            push (p) int *p; {push_(p);}
    
    Douglas Guptill's avatar
    Douglas Guptill committed
    	push_(p)
               int *p;
    	   {
    	     int v;
    	     v = *p;
    	     t = (struct node *) malloc(sizeof *t);	
    	     t->key = v; t->next = head->next;	
    	     head->next =t;	
    	   }
    
    Douglas Guptill's avatar
    Douglas Guptill committed
            pop (x) int *x; {pop_(x);}
    
    Douglas Guptill's avatar
    Douglas Guptill committed
    	pop_(x)
               int *x;
    	   {
    	     t = head->next; head->next = t->next;
    	     *x = t->key;
    	     free(t);
    	   }
    
    Douglas Guptill's avatar
    Douglas Guptill committed
            stackempty (i) int *i; {stackempty_(i);}
    
    Douglas Guptill's avatar
    Douglas Guptill committed
    	stackempty_(i)
              int *i;
    	  { 
    	    *i = 0;
                if(head->next == z) *i = 1;
              }
    
    Douglas Guptill's avatar
    Douglas Guptill committed
            stackflush () {stackflush_();}
    
    Douglas Guptill's avatar
    Douglas Guptill committed
            stackflush_()
               {
                 free(head);
                 free(z);
               }