Tuesday, September 26, 2006
For this lab, you will work on implementing the stack ADT using an array and a linked list. When you are done, you will be able to use either implementation in programming problems that require a stack.
Implementing the Stack ADT using an array
First, download the following header file for the stack ADT:
stack.h | Download |
---|---|
/* stack.h Header file defining the stack ADT Nadeem Abdul Hamid Fall 2006 - Berry College - CSC220 */ #ifndef _STACK #define _STACK #include <stdbool.h> /* define the STACK type; the actual structure will be defined somewhere in the implementation (.c file) */ typedef struct stack* STACK; /* operations on stacks */ STACK createStack(); void destroyStack(STACK s); int size(STACK s); bool isEmpty(STACK s); bool push(STACK s, void* item); void* pop(STACK s); void* top(STACK s); #endif |
|
stack.h | Download |
Now, edit a file called 'arraystack.c' to provide definitions for the 7 functions declared in the header file above. The stack data structure can be defined at the top of this file as:
/* the stack data structure */ struct stack { void** data; /* array of void* pointers */ int t; /* top index, -1 if empty */ int cap; /* current capacity of the data array */ };
What you might do to make sure that the stack doesn't run out of room, even with an array, is have a constant like
#define INITIAL_CAPACITY 2and have the
createStack
function allocate an array of
this size initially. In the push
function, then, you
can have a test like the following to dynamically reallocate space
for the data array if it gets full:
if (s->t == s->cap-1) { /* out of space -- make the array bigger */ void *newdata; newdata = (void*) realloc(s->data, 2 * s->cap * sizeof(void*)); if (!newdata) return false; s->cap *= 2; /* double the current capacity */ s->data = newdata; }
Testing your implementation
Download the following program to test your stack implementation. This program pushes 20 randomly generated characters on the stack and then prints them back as they are popped off. In the output the characters should appear in reverse order.
stacktest.c | Download |
---|---|
#include <stdio.h> #include <stdlib.h> #include "stack.h" int main() { STACK s = createStack(); int i; for (i = 0; i < 20; i++) { char *cp = (char*) malloc(1); *cp = rand() % 26 + 'A'; push(s, cp); printf("%c", *cp); } printf("\n"); while (!isEmpty(s)) { char *cp = pop(s); printf("%c", *cp); free(cp); } printf("\n"); destroyStack(s); return 0; } |
|
stacktest.c | Download |
Since you will probably have several different programs to be
built by the end of this lab, you can set up separate a Makefile for
each one. Download a Makefile for this section here:
Makefile.arraystack. You
then would run make
with the -f
command
line option to specify the makefile to use:
make -f Makefile.arraystack
Implementing the Stack ADT using a linked list
Now, implement the stack ADT using a linked list structure. You may reuse the node files we had worked on in a previous lab:
Now, the stack structure itself can be defined as:struct stack { int size; LIST top; };
Use this makefile to compile and test your list implementation: Makefile.liststack
Decimal to Binary Conversion
Adapt Program 3-16 in the textbook to use your data
structures. Store your code in a file called dec2bin.c
and you can use the following makefile (with either of your stack
implementations):
Makefile.dec2bin