data structures and algorithms lecture notes 4 prepared by İnanç tahrali
TRANSCRIPT
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:PushPopTopisEmpty
4
THE STACK ADT
Only the top element is accessible !
7
Stack S
Push (X,S)Pop (S)
Top (S)
{{12}}
{{5}}
{{3}}
Top
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 Object>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 <class Object>Stack<Object>::Stack( ) { topOfStack = NULL; }
/* Copy constructor */template <class Object>Stack<Object>::Stack( const Stack<Object> & rhs ){ topOfStack = NULL; *this = rhs; }
/* Destructor */template <class Object>Stack<Object>::~Stack( ) { makeEmpty( ); }
/* Test if the stack is logically full */template <class Object>bool Stack<Object>::isFull( ) const { return false; }
/* Test if the stack is logically empty.template <class Object>bool Stack<Object>::isEmpty( ) const { return topOfStack == NULL; }
/* Make the stack logically empty.template <class Object>void Stack<Object>::makeEmpty( ) { while( !isEmpty( ) ) pop( ); }
9
/* Push onto a stacktemplate <class Object>void Stack<Object>::push( const Object & x ){
topOfStack = new ListNode( x, topOfStack);}
/* Return top element.template <class Object>const Object & Stack<Object>::top( ) const{
if( isEmpty( ) ) throw Underflow( ); return topOfStack->element;}
10
/* Pop from stack template <class Object>void Stack<Object>::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 <class Object> Object Stack<Object>::topAndPop( ) {
Object topItem = top( ); pop( ); return topItem;
}
11
/* Deep copytemplate <class Object>const Stack<Object> & Stack<Object>:: operator=( const Stack<Object> & 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 TopofStackStack [TopOfStack] = X
Pop Decrement TopOfStack Top return Stack [TopOfStack]
16
template <class Object>
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<Object> theArray;
int topOfStack;
};
17
/* Construct the stack.
template <class Object>
Stack<Object>::Stack( int capacity ) : theArray( capacity ) {
topOfStack = -1; }
/* Test if the stack is logically empty.
template <class Object>
bool Stack<Object>::isEmpty( ) const {
return topOfStack == -1; }
/* Test if the stack is logically full.
template <class Object>
bool Stack<Object>::isFull( ) const {
return topOfStack == theArray.size( ) - 1; }
/* Make the stack logically empty.
template <class Object>
void Stack<Object>::makeEmpty( ) {
topOfStack = -1; }
18
/* Push onto a stacktemplate <class Object>void Stack<Object>::push( const Object & x ) { if( isFull( ) ) throw Overflow( ); theArray[ ++topOfStack ] = x;}
/* Return top of stacktemplate <class Object>const Object & Stack<Object>::top( ) const { if( isEmpty( ) ) throw Underflow( ); return theArray[ topOfStack ];}
/* Pop from stacktemplate <class Object>void Stack<Object>::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 ERRORif 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 Expressions5+4*2 infix expressions(5+4)*2 paranthesis 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 *
65Top of Stack
11Top of Stack
118Top of Stack
88Top of Stack
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
Queueenqueuedequeue
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 frontreturn Queue[front-1]
29
template <class Object>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<Object> theArray; int currentSize; int front; int back;
void increment( int & x );};
30
/*Construct the queue.template <class Object>Queue<Object>::Queue(int capacity):theArray(capacity){
makeEmpty( );}
/* Make the queue logically empty.
template <class Object>
void Queue<Object>::makeEmpty( )
{
currentSize = 0;
front = 0;
back = -1;
}
31
/* Enqueue - Insert x into the queue.template <class Object>void Queue<Object>::enqueue( const Object & x ) { if( isFull( ) ) throw Overflow( ); increment( back ); theArray[ back ] = x; currentSize++;}
/* Internal method to increment x with wraparound.template <class Object>void Queue<Object>::increment( int & x ) { if( ++x == theArray.size( ) ) x = 0;}