220 likes | 528 Views
Stack is an Abstract Data Type. We do not care about implementationWe can use a stack calling key functionsCreatePushPopStack TopFull?Empty?. Multiple implementations. An ADT can have several implementationsImplementations may be different, but implementation details are hiddenInterface functions are the sameApplication programs will not see any difference.
E N D
1. INFSCI 0015 - Data StructuresLecture 23: Stack Again -Linked-List Implementation Peter Brusilovsky
http://www2.sis.pitt.edu/~peterb/0015-011/
4. Multiple implementations An ADT can have several implementations
Implementations may be different, but implementation details are hidden
Interface functions are the same
Application programs will not see any difference
5. Why linked list implementation? Need to understand the concept of an abstract data type (see slide above)
Need to see nested implementations
Need to have more practice with dynamic data structures
Need to see some application of LLs
Every Data Structure course does it
9. Key Structures in C typedef struct node
{
int data ;
struct node *link ;
} STACK_NODE;
typedef struct stack
{
int count ;
STACK_NODE *top ;
} STACK;
10. createStack: C code static STACK *createStack (int par) /* dummy par */
{
STACK *stack ;
stack = (STACK *) malloc( sizeof (STACK) ) ;
if (stack)
{
stack->top = NULL ;
stack->count = 0;
} /* if */
return stack ;
} /* createStack */
12. stackPush: C code static int pushStack(STACK *stack, int dataIn) {
STACK_NODE *newPtr;
newPtr = (STACK_NODE *) malloc(sizeof( STACK_NODE));
if (newPtr == NULL)
return 0; /* no more space */
newPtr->data = dataIn;
newPtr->link = stack->top;
stack->top = newPtr;
( stack->count )++;
return 1;
} /* pushStack */
14. popStack: C code static int popStack (STACK *stack, int *dataOutPtr) {
STACK_NODE *dltPtr;
if (stack->count == 0)
return 0;
else {
dltPtr = stack->top;
*dataOutPtr = (stack->top)->data;
stack->top = (stack->top)->link;
free (dltPtr);
(stack->count)--;
} /* else */
return 1;
} /* popStack */
15. stackTop static int stackTop (STACK *stack, int *dataOutPtr)
{
if (stack->count > 0) {
*dataOutPtr = (stack->top)->data;
return 1;
} else
return 0;
} /* stackTop */
16. emptyStack and stackCount static int emptyStack (STACK *stack)
{
return (stack->count == 0);
} /* emptyStack */
static int stackCount(STACK *stack)
{
return stack->count;
} /* stackCount */
17. fullStack static int fullStack (STACK *stack)
{
STACK_NODE *temp;
if ( (temp = (STACK_NODE *)malloc (sizeof (STACK_NODE))) )
{
free ( temp );
return 0;
} /* if */
/* malloc failed */
return 1;
} /* fullStack */
18. destroyStack static STACK *destroyStack ( STACK *stack ) {
STACK_NODE *deletePtr;
if (stack) {
/* Delete all nodes in stack */
while ( stack->top != NULL ) {
deletePtr = stack->top ;
stack->top = stack->top->link;
free ( deletePtr );
} /* while */
/* Stack now empty. Destroy stack head node. */
free (stack);
} /* if stack */
return NULL;
} /* destroyStack */
20. Abstract Data Types We know a conceptual idea of a data type
We can work with objects of this type by using a set of predefined operations
We do not really care (and know) how it is implemented
Implementation can be changed - not even a bit in the application program will be changed
Different implementations are possible (could be used in different cases)
21. Stack and Queue are ADTs
There are many others classic ADTs
We will learn:
Lists
Trees Abstract Data Types
22. Compare implementations Array implementation LL implementation