/* * stackops.c - singly linked list stack implementation * */ #include "mystack.h" void push(int value) { if (top == NULL) { stack = (Node *) malloc (sizeof(Node)); top = stack; stack -> value = value; stack -> next = NULL; } else { top -> next = (Node *) malloc (sizeof(Node)); top -> next -> next = NULL; top = top -> next; top -> value = value; } } Node * pop() { Node *tmp, *tmp2; tmp = top; // tmp points at top of stack tmp2 = stack; // point tmp2 at bottom of stack if (tmp2 != NULL) // on empty stack, don't do anything { if (tmp2 -> next != NULL) // two or more nodes in stack { while (tmp2 -> next -> next != NULL) // iterate to 2nd to last { tmp2 = tmp2 -> next; } tmp2 -> next = NULL; // cap off our stack top = tmp2; // tmp2 is the new top } else // only one node in stack { stack = top = NULL; } } return(tmp); }