UMBC CS 201, Fall 98
UMBC CMSC 201 & 201H Fall '98 CSEE | 201 | 201 F'98 | lectures | news | help

The Stack
Using a linked list implementation

The Code

/************************************************\ * Filename: stack.c * * Author: Sue Bogar * * Date Written: 4/25/98 * * Description: This file contains the functions * * necessary to work with a stack. * * This set of functions provide the operations * * needed including adding an item to the stack, * * push; deleting an item from the stack, pop; * * determining if the stack is empty and the * * printing the items in the stack. Since the * * stack is being implemented as a linked list, * * some functions needed for a list have been * * added to this file. Those functions are * * CreateNode and GetData. * \************************************************/ #include <stdio.h> #include <stdlib.h> #include "stack.h" /****************** * Push takes a pointer to nodePtr as its first * argument, which holds the address of top. * The second argument is a pointer to the node * that's to be inserted. Push will insert the * item at the top of the stack. The address of * top is passed into this function, because the * function may need to change the address held * in top. ******************/ void Push (nodePtr *topPtr, nodePtr temp) { if (IsEmpty (*topPtr)) { *topPtr = temp; } else { temp -> next = *topPtr; *topPtr = temp; } } /****************** * Pop takes a pointer to a nodePtr as its * only argument. It will hold the address * of top. This function removes an item from * the stack and returns the data value stored * there. This function may alter the value * of top. ******************/ int Pop (nodePtr *topPtr) { int value; nodePtr temp = *topPtr; if (IsEmpty (*topPtr)) { printf ("Can't pop an empty stack\n"); return -1; } else { value = (*topPtr) -> data; *topPtr = (*topPtr) -> next; free (temp); return value; } } /****************** * IsEmpty takes a nodePtr as its first * argument, which is top. It determines * if the stack is empty and returns 1 (true) * if the stack is empty and 0 (false) if it * is not empty. ******************/ int IsEmpty (nodePtr top) { /* If the pointer to the list is NULL then there is no list. The list is empty, so we return true. */ if(top == NULL) { return 1; } else { return 0; } } /****************** * PrintStack takes a nodePtr as an argument * which is initially the top. The stack is * traversed and the value of the data member * of each item is printed. ******************/ void PrintStack (nodePtr curr) { if (IsEmpty (curr)) { printf ("The stack is empty\n\n"); } else { printf ("The stack contains :\n"); /*Until the end of the list*/ while (curr != NULL) { /* print the current data item */ printf("%d ", curr -> data); /* move to the next node */ curr = curr -> next; } printf ("\n\n"); } } /****************** * CreateNode mallocs the space needed to * hold a struct of type node, initializes * the members, and returns a pointer to * the new node. ******************/ nodePtr CreateNode (void) { nodePtr newNode; /* Get the space needed for the node */ newNode = (nodePtr) malloc (sizeof(node)); if (newNode == NULL) { printf ("Out of Memory - CreateNode\n"); exit (-1); } /* Initialize the members */ newNode -> data = 0; newNode -> next = NULL; return newNode; } /****************** * GetData gets the value of data * from the user and puts it into the * node pointed to by the nodePtr it * receives as an argument. ******************/ void GetData (nodePtr temp) { int value; printf ("\nEnter the value of data : "); scanf ("%d", &value); temp -> data = value; }

The Driver

/* Driver to test the stack (linked list implementation) */ /* Bogar, 4/25/98 */ #include <stdio.h> #include <stdlib.h> #include "stack.h" main () { int value; nodePtr top, temp; top = NULL; PrintStack (top); temp = CreateNode(); GetData (temp); printf("Pushing the value, %d\n", temp -> data); Push (&top, temp); PrintStack (top); temp = CreateNode(); GetData (temp); printf("Pushing the value, %d\n", temp -> data); Push (&top, temp); PrintStack (top); temp = CreateNode(); GetData (temp); printf("Pushing the value, %d\n", temp -> data); Push (&top, temp); PrintStack (top); printf ("Popping :\n"); value = Pop (&top); printf ("The value was %d\n", value); PrintStack (top); printf ("Popping :\n"); value = Pop (&top); printf ("The value was %d\n", value); PrintStack (top); printf ("Popping :\n"); value = Pop (&top); printf ("The value was %d\n", value); PrintStack (top); printf ("Popping :\n"); value = Pop (&top); printf ("The value was %d\n", value); PrintStack (top); }

The Output

retriever[102] a.out The stack is empty Enter the value of data : 7 Pushing the value, 7 The stack contains : 7 Enter the value of data : 4 Pushing the value, 4 The stack contains : 4 7 Enter the value of data : 2 Pushing the value, 2 The stack contains : 2 4 7 Popping : The value was 2 The stack contains : 4 7 Popping : The value was 4 The stack contains : 7 Popping : The value was 7 The stack is empty Popping : Can't pop an empty stack The value was -1 The stack is empty retriever[103]


CSEE | 201 | 201 F'98 | lectures | news | help

Wednesday, 02-Dec-1998 09:15:06 EST