Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.

Slides:



Advertisements
Similar presentations
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Advertisements

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.
Lists + CS3240, L. Grewe 1. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked.
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
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.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
What is a Queue? A queue is a FIFO “first in, first out” structure.
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.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Inheritance CS 308 – Data Structures “the mechanism by which one class acquires the properties of another class” the properties of another class”
1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
1 Chapter 4 Stack and Queue ADT. 2 Stacks of Coins and Bills.
Queues.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
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.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
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,
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Inheritance CS 302 – Data Structures Section 2.4 (pp ) and Section 6.7.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
Data Structures: Stacks Queues 1. 2 Stack ADT: What is a Stack? a stack is a varying-length, collection of homogeneous elements Insertion and Deletion.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Data Structures and Algorithms Stacks and Queues.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree.
1 C++ Plus Data Structures Abstract Data Types Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
CC 215 Data Structures Queue ADT
Queues.
C++ Plus Data Structures
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Chapter 5 ADTs Stack and Queue.
Chapter 16-2 Linked Structures
Chapter 19: Stacks and Queues.
C++ Plus Data Structures
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
C++ Plus Data Structures
CSI 1340 Introduction to Computer Science II
Queues.
Presentation transcript:

Chapter 5 ADTs Stack and Queue

2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular implementation of a stack Implement the Stack ADT, in both an array- based implementation and a linked implementation Describe the structure of a queue and its operations at a logical level

3 Goals Demonstrate the effect of queue operations using a particular implementation of a queue Implement the Queue ADT, using both a an array-based implementation and a linked implementation Use inheritance to create a Counted Queue ADT

4 Stacks of Coins and Bills

5 Stacks of Boxes and Books TOP OF THE STACK

6 Stacks What do these composite objects all have in common?

7 Stacks Stack An abstract data type in which elements are added and removed from only one end (FIFO)

Stacks What operations would be appropriate for a stack? 8

Stacks Transformers –Push –Pop Observers –IsEmpty –IsFull –Top change state observe state 9 What about an iterator?

10 Stacks For what type of problems would stacks be useful?

11 Stacks class StackType { public: StackType(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType item); void Pop(); ItemType Top() const; Logical Level

12 Array-Based Implementation private: int top; ItemType items[MAX_ITEMS]; }; [0] [1] [2] …. [M..] stack.items.top Physical Level

13 Array-Based Implementation Give a series of operations that could produce this situation

14 Array-Based Implementation To what do we initialize top ? 0 or -1 Do we increment or store first? 0: store and increment -1: increment and store Which is better?

15 Array-Based Implementation Before we code, we must consider error conditions Stack overflow The condition that results from trying to push an element on to a full stack Stack underflow The condition that results from trying to pop an empty stack

16 Array-Based Implementation StackType::StackType() { top = -1; } bool StackType::IsEmpty() const { return ( top = -1); } bool StackType::IsFull() const { return ( top == MAX_ITEMS); } What does const mean?

17 Array-Based Implementation void StackType::Push(ItemType newItem) { if (IsFull()) throw FullStack(); top++; items[top] = newItem; } What is FullStack() ?

18 Array-Based Implementation void StackType::Pop() { if (IsEmpty()) throw EmptyStack(); top- -; } ItemType StackType::Top() const { if (IsEmpty()) throw EmptyStack(); return (items[top]); } What is EmptyStack ?

19 Array-Based Implementation Which functions have to be changed if we dynamically allocate the array items ? Do we need to add any new functions?

20 Linked Implementation The logical level (public part of the class declaration) stays the same; the implementation level (private part of the class declaration) changes private: NodeType* topPtr; } Can we “borrow” code from UnsortedType for Push and Pop ?

21 Linked Implementation void StackType::Push(ItemType newItem) { if (IsFull()) throw FullStack(); else { NodeType* location; location = new NodeType; location->info = newItem; location>next = topPtr; topPtr = location; }

22 Linked Implementation void StackType::Pop() { if (IsEmpty()) throw EmptyStack(); else { NodeType* tempPtr; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; } Does this work for stacks of one item? More than one item?

23 Linked Implementation More than one item

24 Linked Implementation One item

25 Linked Implementation What about the constructor, destructor, and observer functions? We can borrow all but Top from class UnsortedType ItemType StackType::Top() { if (IsEmpty()) throw EmptyStack else return topPtr->info; }

26 Queues

27 Queues

28 Queues What do these composite objects all have in common?

29 Queues Queue An abstract data type in which elements are added to the rear and removed from the front; a “first in, first out” (FIFO) structure

Queues What operations would be appropriate for a queue? 30

Queues Transformers –MakeEmpty –Enqueue –Dequeue Observers –IsEmpty –IsFull change state observe state 31 What about an iterator?

32 Queues For what type of problems would stacks be useful?

33 Queues class QueType { public: QueType(int max); QueType(); ~QueType(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType item); void Dequeue(ItemType& item); Logical Level

34 Array-Based Implementation private: ItemType* items; // Dynamically allocated array int maxQue; // Whatever else we need } Implementation level

35 Array-Based Implementation One data structure: An array with the front of the queue fixed in the first position Enqueue A, B, C, D Dequeue Move elements down What’s wrong with this design?

36 Array-Based Implementation Another data structure: An array where the front floats What happens if we add X, Y, and Z ?

37 Array-Based Implementation We can let the queue wrap around in the array; i.e. treat the array as a circular structure

38 Array-Based Implementation Empty Queue Full Queue How can we tell the difference?

39 Array-Based Implementation A third data structure:Front indicates the slot preceding the front item;it is reserved and not used Empty Queue Full Queue

40 Array-Based Implementation private: int front; int rear; int maxQue; ItemType* items; } Complete implementation level To what do we initialize front and rear ?

41 Array-Based Implementation QueType::QueType(int max) { maxQue = max + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue]; } bool QueType::IsEmpty( ) { return ( rear == front );} bool QueType::IsFull( ) { return ( (rear + 1) % maxQue == front )} Why is the array declared max + 1 ?

42 Array-Based Implementation void QueType::Enqueue(ItemType new Item) { if (IsFul()) throw FullQueue(); else { rear = (rear + 1) % maxQue; items[rear] = newItem; }

43 Array-Based Implementation void QueType::Dequeue(ItemType& item) { if (IsEmpty()) throw EmptyQueue(); else { front = (front + 1) % maxQue; item = items[front]; }

44 Counted Queue A queue that has an additional function GetLength that returns the number of items in the queue What functions must be defined in class CountedQue?

45 Counted Queue class CountedQueType : public QueType { public: CountedQueType(); void Enqueue(ItemType newItem); void Dequeue(ItemType& item); int GetLength( ) const; private: int length; };

46 Counted Queue

47 Counted Queue void CountedQueType::Enqueue(ItemType newItem) { try { QueType::Enqueue(newItem); length++; } catch(FullQueue) { throw FullQueue();} } Why must the call to Enqueue be embedded within a try/catch statement? What is this?

48 Counted Queue Does the derived class have access to the base class’s private data members in C++?

49 Linked Implementation Data structure for linked queue

50 Linked Implementation Enqueue(newItem) Set newNode to the address of newly allocated node Set Info(newNode) to newItem Set Next(newNode) to NULL Set Next(rear) to newNode If queue is empty Set front to newNode else Set Next(rear) to newNode

51 Linked Implementation Dequeue(item) Set tempPtr to front Set item to Info(front) Set front to Next(front) if queue is now empty Set rear to NULL Deallocate Node(tempPtr)

52 Linked Implementation A circular linked queue uses only one external pointer: rear How do you access front?

53 Storage Requirements What does this table tell you ?

54 C++ Tips cerr Error output stream available in ; intended for error messages exit(n) A function that cleans up and terminates a program, available in