DS-chapter3(Stack,Queue)

DS-chapter3(Stack,Queue) - 3.3 The Stack ADT 1 ADT The...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
3.3 The Stack ADT
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
1. ADT 1 2 3 4 5 6 6 5 6 5 A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are made at the top only. Objects : A finite ordered list with zero or more elements. Operations : Int IsEmpty ( Stack S ); Stack CreateStack ( ); DisposeStack ( Stack S ); MakeEmpty ( Stack S ); Push ( ElementType X, Stack S ); ElementType Top ( Stack S ); Pop ( Stack S ); Note: A Pop (or Top ) on an empty stack is an error in the stack ADT. Push on a full stack is an implementation error but not an ADT error. The Stack ADT
Background image of page 2
The Stack ADT 2. Implementations Linked List Implementation (with a header node) NULL Element Element Element Push: TmpCell->Next = S->Next S->Next = TmpCell Top: FirstCell = S->Next S->Next = S->Next->Next free ( FirstCell ) return S->Next->Element S Element TmpCell S Pop: Element FirstCell But, the calls to malloc and free are expensive. Easy! Simply keep another stack as a recycle bin .
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The Stack ADT Array Implementation struct StackRecord { int Capacity ; /* size of stack */ int TopOfStack; /* the top pointer */ /* ++ for push, -- for pop, -1 for empty stack */ ElementType *Array; /* array for stack elements */ } ; Note: The stack model must be well encapsulated . That is, no part of your code, except for the stack routines, can attempt to access the Array or TopOfStack variable. Error check must be done before Push or Pop ( Top ).
Background image of page 4
The Stack ADT struct StackRecord { int Capacity; int TopOfStack; ElementType *Array; }; #define EmptyTOS ( -1) #define MinStackSize ( 5) implementations of stack operations void MakeEmpty(Stack S) { S->TopOfStack = EmptyTOS; } void Push(ElementType X, Stack S ) { if ( IsFull( S) ) Error ( “Full Stack”); else
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 15

DS-chapter3(Stack,Queue) - 3.3 The Stack ADT 1 ADT The...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online