CSC 161 Grinnell College Spring, 2009 Imperative Problem Solving and Data Structures

# Stacks with Arrays

## Goals

This laboratory exercise provides practice with an array implementation of the Stack Abstract Data Type.

The reading describes a stack as an object that can store data and that has the following operations:

• InitializeStack
Sets topPosition to -1 to indicate an empty stack.
• Empty
Empty returns true or false, depending upon whether the stack contains any items or not.
• Full (optional)
Full returns true or false, depending upon whether the stack contains as much data as it can hold.
• Push
Push adds the specified item to the top of the stack.
• Pop
If the stack is not empty, Pop removes the top item from the stack, and this item is returned.
If the stack is empty, nothing is returned, and an error is reported.
• Top
If the stack is not empty, the top item is returned, but the contents of the stack are not changed.
If the stack is empty, nothing is returned, and an error is reported.

Within C, there is no way to combine underlying data structures (e.g., arrays) and operations within a single structure. (Such a combined ADT can be done in C++ or Java, but we leave those languages for other courses!) Instead, we will define variables for each stack needed. For each operation, we will pass the relevant variables as parameters, so we can use the same functions for multiple stacks. This approach requires that the data items for the stack have the same type (e.g., a stack of doubles or a stack of strings).

For this lab, we will focus on arrays of strings, and this leads to the following declarations for stacks of bills, magazines, and notes:

```   #define MaxStack  50  /* MaxStack stands for the size of all stack arrays */

typedef struct {
int topPosition;
char * stackArray [MaxStack];
} stringStack;      /* type for a stack of strings */
```

With this framework, the full and push operations might be defined as follows:

```   int full (stringStack stack) {
/* determine if there are more positions in a stackArray */
return (stack.topPosition == (MaxStack-1));
}

int push (stringStack *stack, char * item) {
/* return -1 if stack full */
if (full (*stack)) {
printf ("attempt to push item onto an already full stack\n");
return -1;
}

/* add item to stack */
(stack->topPosition) ++;
stack->stackArray[stack->topPosition] = item;
}
```
1. Complete the implementation of a stack of strings by implementing the remaining operations:

• void initializeStack (stringStack * stack) (sets topPosition of stack to -1)
• int empty (stringStack stack)
• char * pop (stringStack *stack) (returns 0 if stack is empty, otherwise removes the top string from the stack and returns it)
• char * top (stringStack stack) (returns 0 if stack is empty, otherwise returns the top string on the stack)
2. Declare and initialize three stacks (for bills, magazines, and notes) within your main program. Then test your code, including the following:

• Push "mortgage" onto the bill stack
• Push "doctor's bill" onto the bill stack
• Push "credit card" on the the bill stack
• Check if the bill stack is empty
• Check if the magazine stack is empty
• Push "Communications of the ACM - March 2009" onto the magazine stack
• Push "CS Education Bulletin - Spring 2009" onto the magazine stack
• Print the top of the bill stack (stack is not empty, so use top, perhaps within a printf statement)
• Print the top of the magazine stack
• Print the top of the notes stack (careful, stack is empty)
• Pop two items off the bill stack and print each
• Pop two items off the magazine stack and print each
• Check if either the bill stack or the magazine stack is empty.
3. Expand the code for the stack ADT implementation by adding these procedures:

• int size (stringStack stack)
(returns the number of items currently on the stack),

• void print (stringStack stack)
(prints all of the current elements on the stack), and

• char * get_nth (stringStack stack, int nth)
(returns the string at the nth position from the top of the stack).
4. The stack described by the push operation above just maintains a reference to a string that is passed into it. Revise the push operation so that it allocates space for a new string and copies the string parameter into this new space. (Thus, if the original string were changed later in the program, the string on the stack remains the same.)

## Work to be turned in:

• Code, with test runs, for parts 1-4.

This document is available on the World Wide Web as

```http://www.walker.cs.grinnell.edu/courses/161.sp09/labs/lab-stacks-arrays.shtml
```