DS03_Ch03b - 3 The Stack ADT 1. ADT 1 2 3 4 5 6 6 5 6 5 A...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 3 The Stack ADT 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. 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. 1/12 3 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 S But, the calls to malloc and free are expensive. Easy! Simply keep another stack as a recycle bin . 2/12 3 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 )....
View Full Document

Page1 / 12

DS03_Ch03b - 3 The Stack ADT 1. ADT 1 2 3 4 5 6 6 5 6 5 A...

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

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