Python: Stacks and Queues (as an Array)

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Stacks, Queues, and Linked Lists
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
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.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials Stack and Queues.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
Data Structure Dr. Mohamed Khafagy.
CS Data Structures II Review COSC 2006 April 14, 2017
Stacks  Standard operations: IsEmpty … return true iff stack is empty IsFull … return true iff stack has no remaining capacity Top … return top element.
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.
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
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.
Chapter 3. Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Data Structures - Queues
Damian Gordon.  What is a queue?  It’s a structure that conforms to the principle of First In, First Out (FIFO).  The first item to join the.
Stacks and Queues Introduction to Computing Science and Programming I.
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.
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Chap 3 Stack and Queue. Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
STACK Data Structure
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
Python: Stacks and Queues (as a Linked List) Damian Gordon.
Python: Revision Damian Gordon. Python: Structured Programming Damian Gordon.
PseudoCode: Revision Damian Gordon. Parameter Passing, Scope, Local and Global Variables Damian Gordon.
CSCE 3110 Data Structures & Algorithm Analysis
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Stacks and Queues Chapter 4.
CC 215 Data Structures Queue ADT
Stacks and Queues.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Stack and Queue.
CMSC 341 Lecture 5 Stacks, Queues
i206: Lecture 10: Lists, Stacks, Queues
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
CSCE 3110 Data Structures & Algorithm Analysis
Instructor: Mr.Vahidipour
Queues.
Lesson Objectives Aims
2017, Fall Pusan National University Ki-Joune Li
COMPUTER 2430 Object Oriented Programming and Data Structures I
Revised based on textbook author’s notes.
Queues: Implemented using Arrays
ADT list.
2018, Fall Pusan National University Ki-Joune Li
Stacks: Implemented using Linked Lists
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Data structures.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Circular Queues: Implemented using Arrays
Queues: Implemented using Linked Lists
Stacks, Queues, and Deques
2019, Fall Pusan National University Ki-Joune Li
Presentation transcript:

Python: Stacks and Queues (as an Array) Damian Gordon

Stacks

Stacks A Stack is a pile of stuff: It’s a structure that conforms to the principle of Last In, First Out (LIFO). The last item to join the stack is the first item to be served.

Stacks Values are added to the top: 67 59 53 26 59 41 31

Stacks Values are removed from the top: 67 59 53 26 59 41 31

Stacks We will implement a stack as an array called Stack. The maximum length of the stack is called MaxSize. The current top of the stack is called StackTop.

Stacks Stack StackTop MaxSize We will implement a stack as an array called Stack. StackTop 31 41 1 59 2 3 26 4 53 5 59 6 Stack MaxSize

Stacks (Declaring) # PROGRAM StackAsArray: Stack = [31,41,59,26,0,0,0] MaxSize = 7 StackTop = 3 #END StackAsArray.

Stacks We will look at implementing the following modules: IsFull() Check if the stack is full IsEmpty() Push(N) Add a new item (N) to the top of the stack Pop() Remove the top value from the stack Top() Tell us what the top value of the stack is (without removing it).

Stacks (IsFull) def IsFull(): global StackTop if (StackTop + 1 == MaxSize): # THEN StackFull = True else: StackFull = False # ENDIF return StackFull #END IsFull. StackTop MaxSize

Stacks (IsFull) def IsFull2(): global StackTop return(StackTop + 1 == MaxSize) #END IsFull2. StackTop MaxSize

Stacks (IsEmpty) def IsEmpty(): global StackTop if (StackTop == -1): # THEN StackEmpty = True else: StackEmpty = False # ENDIF return StackEmpty #END IsEmpty. StackTop MaxSize

Stacks (IsEmpty) def IsEmpty2(): global StackTop return(StackTop == -1) #END IsEmpty2. StackTop MaxSize

Stacks (Push) def Push(N): global StackTop if (IsFull() == True): # THEN print("The stack is full") else: StackTop = StackTop + 1 Stack[StackTop] = N # ENDIF #END IsFull. StackTop MaxSize

Stacks (Pop) def Pop(): N = 0 global StackTop if (IsEmpty() == True): # THEN print("The stack is Empty") else: N = Stack[StackTop] StackTop = StackTop - 1 # ENDIF return N #END IsFull. StackTop MaxSize

Stacks (Top) def Top(): N = 0 global StackTop if (IsEmpty() == True): # THEN print("The stack is Empty") else: N = Stack[StackTop] # ENDIF return N #END IsFull. StackTop MaxSize

Queues

Queues We will remember queues: It’s a structure that conforms to the principle of First In, First Out (FIFO). The first item to join the queue is the first item to be served.

Queues Values are added to the back: 59 53 26 59 41 31 86

Queues Values are removed from the front: 86 59 53 26 59 41 31

Queues We will implement a queue as an array called Queue. The maximum length of the queue is called MaxSize. The current front of the queue is called Head. The current back of the queue is called Tail.

Queues Queue Head Tail MaxSize We will implement a queue as an array called Queue. Head Tail 31 1 41 2 59 3 26 53 4 5 6 Queue MaxSize

Queues (Declaring) # PROGRAM QueueAsArray: Queue = [0,0,59,26,53,59,0] MaxSize = 7 QueueHead = 2 QueueTail = 5 #END QueueAsArray.

Queues We will look at implementing the following modules: IsFull() Check if the queue is full IsEmpty() AddToQ(N) Add a new item (N) to the back of the queue DeleteFromQ() Remove the front value from the queue ClearQ() Empty the queue

Queues (IsFull) def IsFull(): global QueueTail if (QueueTail + 1 == MaxSize): # THEN QueueFull = True else: QueueFull = False # ENDIF return QueueFull #END IsFull. Head Tail MaxSize

Queues (IsFull) def IsFull2(): global QueueTail return(QueueTail + 1 == MaxSize) #END IsFull2. Head Tail MaxSize

Queues (IsEmpty) def IsEmpty(): global QueueTail global QueueHead if (QueueTail == QueueHead): # THEN QueueEmpty = True else: QueueEmpty = False # ENDIF return QueueEmpty #END IsEmpty. Head Tail MaxSize

Queues (IsEmpty) def IsEmpty2(): global QueueTail global QueueHead return(QueueTail == QueueHead) #END IsEmpty2. Head Tail MaxSize

Queues (AddToQ) def AddToQ(N): global QueueTail if (IsFull() == True): # THEN print("The Queue is full") else: QueueTail = QueueTail + 1 Queue[QueueTail] = N # ENDIF #END AddToQ. Head Tail MaxSize

Queues (DeleteFromQ) def DeleteFromQ(): N = 0 global QueueHead if (IsEmpty() == True): # THEN print("The Queue is Empty") else: N = Queue[QueueHead] QueueHead = QueueHead + 1 # ENDIF return N #END DeleteFromQ. Head Tail MaxSize

Queues (ClearQ) def ClearQ(): global QueueTail global QueueHead QueueTail = -1 QueueHead = QueueTail #END ClearQ. Head Tail MaxSize

Circular Queues

Circular Queues Head Tail We can also have a circular queue: A queue where the start and end of the queue are joined together. 7 43 6 1 12 35 5 2 22 99 4 Tail 3

Circular Queues So Tail starts at 4, goes to 5, goes to 6, goes to 0, goes to 1, etc. So it’s Tail = Tail + 1, But… Tail = (Tail + 1) % 7 So Tail % MaxSize works as follows: 4 % MaxSize = 4 5 % MaxSize = 5 6 % MaxSize = 6 7 % MaxSize = 0 8 % MaxSize = 1

Circular Queues We will implement a queue as an array called Queue. The maximum length of the queue is called MaxSize. The current front of the queue is called Head. The current back of the queue is called Tail.

Circular Queues Queue Head Tail MaxSize We will implement a queue as an array called Queue. Head Tail 31 1 41 2 59 3 26 53 4 5 6 Queue MaxSize

Circular Queues (Declaring) # PROGRAM CQueueAsArray: Queue = [0,0,59,26,53,59,0] MaxSize = 7 QueueHead = 2 QueueTail = 5 #END CQueueAsArray.

Circular Queues We will look at implementing the following modules: IsFull() Check if the queue is full IsEmpty() AddToQ(N) Add a new item (N) to the back of the queue DeleteFromQ() Remove the front value from the queue ClearQ() Empty the queue

Circular Queues (IsFull) def IsFull(): global QueueTail global MaxSize if (QueueHead == (QueueTail + 1) % MaxSize): # THEN QueueFull = True else: QueueFull = False # ENDIF return QueueFull #END IsFull. 7 43 6 1 12 35 5 2 22 99 4 3

Circular Queues (IsFull) def IsFull2(): global QueueTail return(QueueHead == (QueueTail + 1) % MaxSize) #END IsFull2. 7 43 6 1 12 35 5 2 22 99 4 3

Circular Queues (IsEmpty) def IsEmpty(): global QueueTail global QueueHead if (QueueTail == QueueHead): # THEN QueueEmpty = True else: QueueEmpty = False # ENDIF return QueueEmpty #END IsEmpty. 7 43 6 1 12 35 5 2 22 99 4 3

Circular Queues (IsEmpty) def IsEmpty2(): global QueueTail global QueueHead return(QueueTail == QueueHead) #END IsEmpty2. 7 43 6 1 12 35 5 2 22 99 4 3

Circular Queues (AddToQ) def AddToQ(N): global QueueTail global MaxSize if (IsFull() == True): # THEN print("The Queue is full") else: QueueTail = (QueueTail + 1) % MaxSize Queue[QueueTail] = N # ENDIF #END AddToQ. 7 43 6 1 12 35 5 2 22 99 4 3

Circular Queues (DeleteFromQ) def DeleteFromQ(): global QueueHead global MaxSize N = 0 if (IsEmpty() == True): # THEN print("The Queue is Empty") else: N = Queue[QueueHead] QueueHead = (QueueHead + 1) % MaxSize # ENDIF return N #END DeleteFromQ. 7 43 6 1 12 35 5 2 22 99 4 3

Circular Queues (ClearQ) def ClearQ(): global QueueTail global QueueHead QueueTail = -1 QueueHead = QueueTail #END ClearQ. 7 43 6 1 12 35 5 2 22 99 4 3

etc.