Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.

Slides:



Advertisements
Similar presentations
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
Advertisements

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.
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
1 Queues and Priority Queues Chapter 8. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Summary of lectures (1 to 11)
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
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.
Chapter 16 Stack and Queues part2
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Exam Review 5/28/15. Software Development (Review)  Make it work…then make it pretty can result in dangerous code  Better to take a unified, consistent.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Pointers OVERVIEW.
Lists Chapter 6 5/14/15 Adapted from instructor slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
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.
Review Dr. Yingwu Zhu. Outline ADT concept C++ Class List with class implementation.
1 Data Structures and Algorithms Stacks and Queues.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Exam1 Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lists Chapter.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
CS505 Data Structures and Algorithms
Chapter 12 – Data Structures
Copy Constructor / Destructors Stacks and Queues
Brief Review of ADTs and Class Implementation
Queues.
Linked Lists Chapter 6 Section 6.4 – 6.6
Dr. Bernard Chen Ph.D. University of Central Arkansas
Chapter 4 Linked Lists.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Dynamically Allocated Memory
CMSC 341 Lecture 5 Stacks, Queues
Pointers and Linked Lists
Queues.
Array Lists Chapter 6 Section 6.1 to 6.3
Lists.
Dynamic allocation (continued)
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Presentation transcript:

Review of Stacks and Queues Dr. Yingwu Zhu

Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of Queue class, see Textbook for details!

How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item Pop operation Properties Ordered collection of items Be accessed at only the top

Building a Stack Class Design a stack class  stack.h (header file) Implement the stack class  stack.cpp (implementation file)

Building a Stack Class Need to determine the data structure to store data items (array or linked list?) Need to determine the algorithms to perform operations on the data items

Building a Stack Class What data items do we need? An array/linked-list to hold stack elements An integer/pointer to indicate the top of stack What operations do we need? Constructor: build an empty stack Empty: check if a stack is empty Push: add an element on the top Top: return the top element Pop: remove the top element Display: show all the stack elements

A Static Array-Based Stack (p327) A static array to hold the stack elements, the position 0 as the bottom of the stack Problem?  the stack capacity cannot be changed during run time (waste of space or insufficient room for more data items)

Dynamic Array-Based Stack Advantage: allow users to specify the stack capacity in its declaration But, we need to do more ! (Due to the dynamically-allocated memory) Constructor: memory allocation and data member initialization Destructor: reclaim the dynamically-allocated memory, avoid “memory leak” Copy constructor: NO “shallow copy” (p339) Assignment operator: assign one object to another, NO “shallow copy”

Example Codes Explanation (p336) Figure7.6: const keyword Destructor (p338-9, Figure 7.7) Deallocate array allocated in constructor Avoid memory leak problem

Example Codes Explanation Copy constructor (p339) Initialization Passing value parameter Return a function value Creating a temporary storage value Default copy constructor: member-by- member copy Ensure deep copy

Example Codes Explanation Assignment operator (=) The default assignment operator only member- by-member copy, causing “shallow copy” and “memory leak” (by old array) Ensure deep copy Check if it is self-assignment (Fig 7.9, p343) If NO, then destroy the old array, allocate a new one Never forget: return *this;

Let’s do it typedef int DataType; class Stack { private: int myCapacity; int myTop; int* myArray; public: ……. };

Linked List-Based Stack Advantage: grow and shrink as needed Need only one data member: Pointer myTop Nodes allocated (but not part of stack class) Node declaration in Fig 7.11 (p. 353) Question: Is head or tail of a linked list used for stack top myTop ?

Implementing Linked Stack Operations Constructor: simply assign null pointer to myTop Empty: check myTop == NULL (0) Push: insertion at the head of the list Top: return the data to which myTop points View definitions in Fig Fig View definitions in Fig Fig. 7.12

Implementing Linked Stack Operations Pop Delete first node in the linked list ptr = myTop; myTop = myTop->next; delete ptr; Output Traverse the list for (ptr = myTop; ptr != 0; ptr = ptr->next) out data << endl; View definitions in Fig Fig View definitions in Fig Fig. 7.12

Stack.h typedef int DataType; class Stack { public: Stack(); Stack(const Stack& org); void push(const DataType& v); void pop(); DataType top() const; ~Stack(); private: class Node { public: DataType data; Node* next; Node(DataType v, Node* p) : data(v), next(0) { } }; typedef Node* NodePtr; NodePtr myTop; };

Exercises: implementation Close Textbook and Notes Copy constructor? Assignment Operator? Destructor?

Implementing Linked Stack Operations Destructor (p. 361) Traverse list to deallocate nodes “Never burn bridges before crossing them” Copy constructor (p. 360) Traverse linked list, copying each into new node Deep copy Watch for the empty object to be copied

Implementing Linked Stack Operations Assignment operator (=), p.361 Rule out self-assignment Destroy the old list, this->~Stack(); code reuse Similar to copy constructor

Any Question?

Introduction to Queue A sequence of data items (FIFO) Items can be removed only at the front Items can be added only at the back

Introduction to Queue Basic operations Construct a queue Check if empty Enqueue: add an element to back Dequeue: remove an element from front Front: return the first element Skip array-based queues Linked-list based queues Data members? Operations: above

Linked List-Based Queue (Chp. 8.3) Advantage: grow and shrink as needed Two data members: myFont, myBack Why need myBack? Avoid list traversal when enqueue()

Queue.h typedef int DataType; class Queue { public: //constructor //… member functions private: class Node { public: DataType data; Node* next; Node(DataType v, Node* p) : data(v), next(0) { } }; typedef Node* NodePtr; NodePtr myFront, myback; };

Linked Queue, p.418 Constructor: initializes myFront and myBack Front, p420 return myFront->data Dequeue, p421 Delete the first node (watch for empty queue) Equeue, p420 Insert the node at the back

Linked Queue Copy constructor: deep copy, p418 Assignment operator, p419 Watch for self-assignment Deep copy Avoid memory leak Destructor: reclaim memory, p418

Circular Linked Queue, p423 Treat the linked list as circular Last node points to the first node Alternatively keep pointer to last node rather than first node, so only needs one data member!

Implementing Circular List Queue Can you implement it?

Application of Queues Disk Scheduling in OS Disk requests from OS Disk has a queue Disk serves requests in queue by FIFO In reality, it may be not always FIFO, priority?

Question Time Any Question?

Lecture Reviews Difference between Stacks and Queues as ADT Different implementations of Stacks and Queues (Dynamic) Array or Linked List Strengths and weakness When we need copy constructor, destructor, assignment operator? Undertand Stacks and Queues via their applications