Radix Sort
N numbers in the range 0 to Np1. 64,8,216,512,27,729,0,1,343,125. First pass 0 0 1 1 512 343 64 2 3 4 125 216 27 5 6 7 8 8 729 9
Second pass
8 1 0 0 216 729 512 27 125 1 2 3 343 64
Third pass
64 27 8 1 0 0 125 216 343 512 729
Stack ADT
A linear structure of fixed or variable size Only the top of the stack is used to add, remove and examine - LIFO
push: add an object to the top of the stack pop: remove an object from the top of the stack peek: examine the object at the top of the stack (pop without removal) isEmpty: determines whether or not the stack is empty depth: returns the number of objects in the stack equals: tests two stacks for equality toString: converts a stack to a String
Stack Def
#define EmptyTOS ( -1 ) #define MinStackSize ( 5 ) typedef int ElementType; struct StackRecord { int Capacity; int TopOfStack; ElementType *Array; };
functions
typedef int ElementType; /* START: fig3_45.txt */ #ifndef _Stack_h #define _Stack_h struct StackRecord; typedef struct StackRecord *Stack; int IsEmpty( Stack S ); int IsFull( Stack S ); Stack CreateStack( int MaxElements ); void DisposeStack( Stack S ); void MakeEmpty( Stack S ); void Push( ElementType X, Stack S ); ElementType Top( Stack S ); void Pop( Stack S ); ElementType TopAndPop( Stack S );
push
To push a elt X onto stack, increment topofstack and set stack[topofstack]=x;
Create a stack
3-5 allocates stack structure, 6-8 allocate the stack array line 1 checks the stack size should have more than min elements Stack CreateStack( int MaxElements ) { Stack S; /* 1*/ /* 2*/ /* 3*/ /* 4*/ /* 5*/ /* 6*/ /* 7*/ /* 8*/ /* 9*/ /*10*/ /*11*/ } if( MaxElements < MinStackSize ) Error( "Stack size is too small" ); S = malloc( sizeof( struct StackRecord ) ); if( S == NULL ) FatalError( "Out of space!!!" ); S->Array = malloc( sizeof( ElementType ) * MaxElements ); if( S->Array == NULL ) FatalError( "Out of space!!!" ); S->Capacity = MaxElements; MakeEmpty( S ); return S;
disposestack
void DisposeStack( Stack S ) { if( S != NULL ) { free( S->Array ); free( S ); } }
push
Void Push( ElementType X, Stack S ) { if( IsFull( S ) ) Error( "Full stack" ); else S->Array[ ++S->TopOfStack ] = X; }
peek
ElementType Top( Stack S ) { if( !IsEmpty( S ) ) return S->Array[ S->TopOfStack ]; Error( "Empty stack" ); return 0; /* Return value used to avoid warning */ }
pop
Void Pop( Stack S ) { if( IsEmpty( S ) ) Error( "Empty stack" ); else S->TopOfStack--; }
Stack ADT
Linked list implementation typedef struct Node *PtrToNode; typedef PtrToNode Stack; struct Node { ElementType Element; PtrToNode Next; };
functions
typedef int ElementType; /* START: fig3_39.txt */ #ifndef _Stack_h #define _Stack_h struct Node; typedef struct Node *PtrToNode; typedef PtrToNode Stack; int IsEmpty( Stack S ); Stack CreateStack( void ); void DisposeStack( Stack S ); void MakeEmpty( Stack S ); void Push( ElementType X, Stack S ); ElementType Top( Stack S ); void Pop( Stack S );
isempty
int IsEmpty( Stack S ) { return S->Next == NULL; }
create
Stack CreateStack( void ) { Stack S; S = malloc( sizeof( struct Node ) ); if( S == NULL ) FatalError( "Out of space!!!" ); S->Next = NULL; MakeEmpty( S ); return S; }
makeempty
Void MakeEmpty( Stack S ) { if( S == NULL ) Error( "Must use CreateStack first" ); else while( !IsEmpty( S ) ) Pop( S ); }
disposestack
void DisposeStack( Stack S ) { MakeEmpty( S ); free( S ); }
push
void Push( ElementType X, Stack S ) { PtrToNode TmpCell; TmpCell = malloc( sizeof( struct Node ) ); if( TmpCell == NULL ) FatalError( "Out of space!!!" ); else { TmpCell->Element = X; TmpCell->Next = S->Next; S->Next = TmpCell; } }
Top or peek
ElementType Top( Stack S ) { if( !IsEmpty( S ) ) return S->Next->Element; Error( "Empty stack" ); return 0; /* Return value used to avoid warning */ }
pop
void Pop( Stack S ) { PtrToNode FirstCell; if( IsEmpty( S ) ) Error( "Empty stack" ); else { FirstCell = S->Next; S->Next = S->Next->Next; free( FirstCell ); } }
Postfix notation is much more efficient Infix: The operator is written between the operands Postfix: The operator is written after the operands
infix: infix: infix: 3+4 2+3*4 2*3+4 postfix: postfix: postfix: 34+ 234*+ 23*4+