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

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Stack & Queues COP 3502.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
 Balancing Symbols 3. Applications
Stack and Queue COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Chapter 16 Stacks & Queues. Objective In this chapter we will learn:  Stacks  Queues  Different implementations (arrays and linked list) of both 
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types (ADTs) Lists Stacks Queues.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture6.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Stacks.
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
CMSC 341 Lecture 5 Stacks, Queues
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
C++ Plus Data Structures
Stacks and Queues 1.
Abstract Data Type Abstract Data Type as a design tool
Stacks CS-240 Dick Steflik.
CMSC 341 Stacks and Queues 4/17/2019.
CMSC 341 Stacks and Queues 4/17/2019.
Stacks.
CMSC 341 Stacks and Queues 4/29/2019.
CMSC 341 Lecture 7.
Data Structures & Programming
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 ? * * + 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 : *

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