/* FILE: stack.c * PURPOSE: * Functions that implement a simple stack. * (CST 8152 - 98W - Assignment 6) * HISTORY: * Ian D. Allen idallen@freenet.carleton.ca */ //FIXME-- add system includes here //FIXME-- add your local includes here /* The stack structure element, containing the Item structure. * The stack is a singly-linked list of structures with the most * recent element at the head of the list. */ typedef struct Element { Item item; struct Element *next; } Element; static Element *head; /* head of stack element list */ /* FUNCTION: push * PURPOSE: * Allocate memory for a new stack element, copy the passed Item * into the element, and push the element on the stack. * Failure to allocate memory is fatal; no error status is returned. */ void push( Item *item /* IN: pointer to item to push on stack */ ){ //FIXME-- allocate memory for a new stack element "E" //FIXME-- copy the passed Item into the new element "E" //FIXME-- point "E" to the first element in the stack //FIXME-- point the head pointer at the new element "E" } /* FUNCTION: pop * PURPOSE: * Pop the stack and return an Item to the caller. * An attempt to pop an empty stack is fatal. * Remember to free the popped memory. */ Item pop(void){ /* RET: popped item */ Element e; /* copy of top element on stack */ if( empty() ){ eprintf("Error: POP called at stack bottom"); abort(); /*NOTREACHED*/ } //FIXME-- pick up a copy of the head element on the stack //FIXME-- free the element pointed to by the head pointer //FIXME-- point the head pointer at the next element in the stack return e.item; /* return the Item we popped */ } /* FUNCTION: empty * PURPOSE: * Return TRUE if the stack is empty. */ Boolean empty(void){ /* RET: TRUE if stack is empty */ return (head == NULL); }