Anda di halaman 1dari 29

Data Structures

V. Balasubramanian AP/CSE SSNCE

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

Circularly Linked Lists


The last cell keep a pointer back to the first.

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

Other operations include:


Array implementation of STACK ADT


Simple to implement. We need to declare array size ahead of time

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;

Isempty to check whether stack is empty


int IsEmpty( Stack S ) { return S->TopOfStack == EmptyTOS; }

Isfull to check whether stack is full


int IsFull( Stack S ) { return S->TopOfStack == S->Capacity - 1; }

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;

Makeempty create a empty stack


Void MakeEmpty( Stack S ) { S->TopOfStack = EmptyTOS; }

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--; }

Topandpop-give top element and pop the stack


ElementType TopAndPop( Stack S ) { if( !IsEmpty( S ) ) return S->Array[ S->TopOfStack-- ]; Error( "Empty stack" ); return 0; /* Return value used to avoid warning */ }

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 ); } }

Use of the stack data structure: postfix notation


If you know Reverse Polish notation, heres another example: Consider the expression 13 2 * (5 * 2 4)
Think about your eye movements as you try to evaluate it This is inefficient for a computer! (has to read it several times)

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+

Anda mungkin juga menyukai