 # DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.

## Presentation on theme: "DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI."— Presentation transcript:

DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI

2 ROAD MAP Abstract Data Types (ADT) The List ADT Array implementation of lists Linked list implementation of lists Cursor implementation of lists The Stack ADT Linked list implementation of stacks Array implementation of stacks The Queue ADT Link list implementation of queues Array implementation of queues

3 THE STACK ADT A stack is a list Insertions and deletions from one end (top) LIFO (Last In Fist Out) Operations: Push Pop Top isEmpty

4 THE STACK ADT Only the top element is accessible !

5 Implementation of Stacks 1. Linked List Implementation insert / delete at the front of the list performs “push” by inserting performs “pop” by deleting all operations take constant time

template class Stack { public: Stack( ); Stack( const Stack & rhs ); ~Stack( ); bool isEmpty( ) const; bool isFull( ) const; const Object & top( ) const; void makeEmpty( ); void pop( ); void push( const Object & x ); Object topAndPop( ); const Stack & operator=( const Stack & rhs );

7 private: struct ListNode { Object element; ListNode *next; ListNode( const Object & theElement, ListNode * n = NULL ) : element( theElement ), next( n ) { } }; ListNode *topOfStack; };

8 /* Construct the stack */ template Stack ::Stack( ) { topOfStack = NULL; } /* Copy constructor */ template Stack ::Stack( const Stack & rhs ) { topOfStack = NULL; *this = rhs;} /* Destructor */ template Stack ::~Stack( ) { makeEmpty( ); } /* Test if the stack is logically full */ template bool Stack ::isFull( ) const { return false; } /* Test if the stack is logically empty. template bool Stack ::isEmpty( ) const { return topOfStack == NULL; } /* Make the stack logically empty. template void Stack ::makeEmpty( ) { while( !isEmpty( ) ) pop( ); }

9 /* Push onto a stack template void Stack ::push( const Object & x ) { topOfStack = new ListNode( x, topOfStack); } /* Return top element. template const Object & Stack ::top( ) const { if( isEmpty( ) ) throw Underflow( ); return topOfStack->element; }

10 /* Pop from stack template void Stack ::pop( ) { if( isEmpty( ) ) throw Underflow( ); ListNode *oldTop = topOfStack; topOfStack = topOfStack->next; delete oldTop; } /*Return and remove the most recently inserted item from the stack. */ template Object Stack ::topAndPop( ) { Object topItem = top( ); pop( ); return topItem; }

11 /* Deep copy template const Stack & Stack :: operator=( const Stack & rhs ) { if( this != &rhs ) { makeEmpty( ); if( rhs.isEmpty( ) ) return *this; ListNode *rptr = rhs.topOfStack; ListNode *ptr = new ListNode( rptr->element ); topOfStack = ptr; for(rptr = rptr->next; rptr != NULL; rptr = rptr->next ) ptr = ptr->next = new ListNode( rptr->element ); } return *this; }

12 Implementation of Stacks 1. Linked List Implementation All operations take constant time new and free operations are slow Use a second stack ( freeStack ) Move deleted stack nodes to freeStack Take new nodes from freeStack if not empty Use array implementation Use cursor implementation

13 ROAD MAP Abstract Data Types (ADT) The List ADT Array implementation of lists Linked list implementation of lists Cursor implementation of lists The Stack ADT Linked list implementation of stacks Array implementation of stacks The Queue ADT Link list implementation of queues Array implementation of queues

14 Implementation of Stacks 2. Array Implementation more popular solution need to declare the size usually the max stack size is known keep an array Stack an index TopOfStack (-1 is empty)

15 Array Implementation of Stacks Push  Increment TopofStack Stack [TopOfStack] = X Pop  Decrement TopOfStack Top  return Stack [TopOfStack]

16 template class Stack { public: explicit Stack( int capacity = 10 ); bool isEmpty( ) const; bool isFull( ) const; const Object & top( ) const; void makeEmpty( ); void pop( ); void push( const Object & x ); Object topAndPop( ); private: vector theArray; int topOfStack; };

17 /* Construct the stack. template Stack ::Stack( int capacity ) : theArray( capacity ) { topOfStack = -1; } /* Test if the stack is logically empty. template bool Stack ::isEmpty( ) const { return topOfStack == -1; } /* Test if the stack is logically full. template bool Stack ::isFull( ) const { return topOfStack == theArray.size( ) - 1; } /* Make the stack logically empty. template void Stack ::makeEmpty( ) { topOfStack = -1; }

18 /* Push onto a stack template void Stack ::push( const Object & x ) { if( isFull( ) ) throw Overflow( ); theArray[ ++topOfStack ] = x; } /* Return top of stack template const Object & Stack ::top( ) const { if( isEmpty( ) ) throw Underflow( ); return theArray[ topOfStack ]; } /* Pop from stack template void Stack ::pop( ) { if( isEmpty( ) ) throw Underflow( ); topOfStack--; }

19 Applications Balancing Symbols check whether each [ or ( has a correcponding ) or ] [ ( ) ] √ [ ( ] ) X

20 Applications Balancing Symbols Algorithm Keep a stack Read the charecters one by one If the charecter is ( or [ push into stack If the charecter is ) or ] pop the stack if the stack is empty ERROR if the symbol popped does not match with character ERROR Continue until the end of file At the end of file if the stack is not empty ERROR else SUCCESS

21 Applications PostFix Expressions 5+4*2infix expressions (5+4)*2paranthesis is necessary What if you have a simple calculator ? 5 4 + 2 * 5 4 2 * + How to evaluate postfix expressions ? use a stack a number is seen  push onto stack an operator is seen  pop two values from stack apply operation push the result onto stack

22 Applications PostFix Expressions Example : 6 5 + 8 *

23 Applications Function Calls When a function is called Local variables and status should be saved When the function returns Saved values needs to be stored

24 ROAD MAP Abstract Data Types (ADT) The List ADT Array implementation of lists Linked list implementation of lists Cursor imlementation of lists The Stack ADT Linked list implementation of stacks Array implementation of stacks The Queue ADT Array implementation of queues Linked List implementation of queues

25 THE QUEUE ADT A queue is a list insertions at one end deletions at other end FIFO (First In First Out) Operations ?

26 THE QUEUE ADT Operations Enqueue insert an element at the end of the list (rear) Dequeue delete the element at the start of the list (front) IsEmpty check whether the queue has an element or not

27 Implementation of Queue ADT Linked list implementation of queues keep two pointers head and tail Array implementation of queues keep positions of front and rear keep track of the size of queue

28 Array Implementation of Queues Enqueue  Increment queueSize Increment back Queue [back] = X Dequeue  Decrement queueSize Increment front return Queue[front-1]

29 template class Queue { public: explicit Queue( int capacity = 10 ); bool isEmpty( ) const; bool isFull( ) const; const Object & getFront( ) const; void makeEmpty( ); Object dequeue( ); void enqueue( const Object & x ); private: vector theArray; int currentSize; int front; int back; void increment( int & x ); };

30 /*Construct the queue. template Queue ::Queue(int capacity):theArray(capacity) { makeEmpty( ); } /* Make the queue logically empty. template void Queue ::makeEmpty( ) { currentSize = 0; front = 0; back = -1; }

31 /* Enqueue - Insert x into the queue. template void Queue ::enqueue( const Object & x ) { if( isFull( ) ) throw Overflow( ); increment( back ); theArray[ back ] = x; currentSize++; } /* Internal method to increment x with wraparound. template void Queue ::increment( int & x ) { if( ++x == theArray.size( ) ) x = 0; }

32 /* Dequeue – Remove last recently inserted item template Object Queue ::dequeue( ) { if( isEmpty( ) ) throw Underflow( ); currentSize--; Object frontItem = theArray[ front ]; increment( front ); return frontItem; }

Download ppt "DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI."

Similar presentations