1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Stack & Queues COP 3502.
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.
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.
Stacks and Queues Sections 3.6 and 3.7. Stack ADT Collections:  Elements of some proper type T Operations:  void push(T t)  void pop()  T top() 
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Data Structures & Algorithms
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.
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.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
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.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 7 Ming Li Department of.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
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.
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.”
DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
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,
Main Index Contents 11 Main Index Contents Week 4 – Stacks.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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,
Data Structures Using C++
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
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.
CS342 Data Structures End-of-semester Review S2002.
1 Stacks and Queues Reading: Sections 3.6 and 3.7.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
COSC 2007 Data Structures II
The List ADT Reading: Sections 3.2, 3.3, 3.5.
Exam Review 2 Chapter 5 – 9 CSC212 FG CS Dept, CCNY.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Final Exam Review COP4530.
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Stacks Stacks.
Homework 4 questions???.
Stacks Stack: restricted variant of list
Andy Wang Data Structures, Algorithms, and Generic Programming
CMSC 341 Lecture 5 Stacks, Queues
Depth First Search—Backtracking
Chapter 19: Stacks and Queues.
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
Stacks, Queues, and Deques
Final Exam Review COP4530.
CSC 143 Stacks [Chapter 6].
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.
Chapter 3 Lists, Stacks, and Queues
Data Structures & Programming
Data Structures & Programming
Presentation transcript:

1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

2 Stack ADT - LIFO Collections: –Elements of some proper type T Operations: –Feature: Last In, First Out –void push(T t) –void pop() –T top() –bool empty() –unsigned int size() –constructor and destructor

3 Stack Model—LIFO Empty stack S –S.empty() is true –S.top() not defined –S.size() == 0 food chain stack

4 Stack Model—LIFO S.push(“mosquito”) –S.empty() is false –S.top() == “mosquito” –S.size() == 1 mosquito food chain stack

5 Stack Model—LIFO S.push(“fish”) –S.empty() is false –S.top() == “fish” –S.size() == 2 fish mosquito food chain stack

6 Stack Model—LIFO S.push(“raccoon”) –S.empty() is false –S.top() == “raccoon” –S.size() == 3 raccoon fish mosquito food chain stack

7 Stack Model—LIFO S.pop() –S.empty() is false –S.top() == “fish” –S.size() == 2 fish mosquito food chain stack

8 Implementations and Uses of Stack ADT Implementations –Any list implementation –list and vector C++ STL –Vector/List ADTs push_back()/pop_back() push_front()/? Uses –Depth first search / backtracking –Evaluating postfix expressions –Converting infix to postfix –Function calls (runtime stack) –Recursion

9 Depth First Search—Backtracking Problem –Discover a path from start to goal Solution –Start from Node start –Stop If node is goal –Go deep If there is an unvisited neighbor, go there –Backtrack Retreat along the path to find an unvisited neighbor, if cannot go deeper Outcome –If there is a path from start to goal, DFS finds one such path start goal

10 Depth First Search—Backtracking (2) Stack start goal 1 Push

11 Depth First Search—Backtracking (3) Stack start goal 2 1 Push

12 Depth First Search—Backtracking (4) Stack start goal Push

13 Depth First Search—Backtracking (5) Stack start goal Push

14 Depth First Search—Backtracking (6) Stack start goal Push

15 Depth First Search—Backtracking (7) Stack start goal Push Pop

16 Depth First Search—Backtracking (8) Stack start goal Push Pop

17 Depth First Search—Backtracking (9) Stack start goal 2 1 Push Pop

18 Depth First Search—Backtracking (10) Stack start goal 1 Push Pop

19 Depth First Search—Backtracking (11) Stack start goal 3 1 Push

20 Depth First Search—Backtracking (12) Stack start goal Push

21 Depth First Search—Backtracking (13) Stack start goal Push

22 DFS Implementation DFS() { stack S; // mark the start location as visited S.push(start); while (S is not empty) { t = S.top(); if (t == goal) Success(S); if (// t has unvisited neighbors) { // choose an unvisited neighbor n // mark n visited; S.push(n); } else { BackTrack(S); } Failure(S); }

23 DFS Implementation (2) BackTrack(S) { while (!S.empty() && S.top() has no unvisited neighbors) { S.pop(); } Success(S) { // print success while (!S.empty()) { output(S.top()); S.pop(); }

24 Runtime Stack Runtime environment –Static Executable code Global variables –Stack Push for each function call Pop for each function return Local variables –Heap Dynamically allocated memories new and delete static stack heap program memory

25 Queue ADT - FIFO Collection –Elements of some proper type T Operations –Feature: First In, First Out –void push(T t) –void pop() –T front() –bool empty() –unsigned int size() –Constructors and destructors

26 Queue Model—FIFO Empty Q animal parade queue

27 Queue Model—FIFO Q.Push(“ant”) ant front back animal parade queue

28 Queue Model—FIFO Q.Push(“bee”) antbee front back animal parade queue

29 Queue Model—FIFO Q.Push(“cat”) antbeecat front back animal parade queue

30 Queue Model—FIFO Q.Push(“dog”) antbeecatdog front back animal parade queue

31 Queue Model—FIFO Q.Pop() beecatdog front back animal parade queue

32 Queue Model—FIFO Q.Pop() catdog front back animal parade queue

33 Queue Model—FIFO Q.Push(“eel”) Q.Pop() eel front back animal parade queue

34 Implementations and Uses of Queue ADT Implementations –Any list implementation push_front()/pop_back() push_back()/? Uses –Buffers –Breadth first search –Simulations –Producer-Consumer Problems

35 Breadth First Search Problem –Find a shortest path from start to goal Solution –Start from Node start –Visit All neighbors of the node –Stop If a neighbor is goal –Otherwise Visit neighbors two hops away –Repeat (Stop/Otherwise) Visiting neighbors N hops away start goal

36 Breadth First Search (2) Queue start goal 1 Push

37 Breadth First Search (3) Queue start goal Pop

38 Breadth First Search (4) Queue start goal 234 Push

39 Breadth First Search (5) Queue start goal Pop 34

40 Breadth First Search (6) Queue start goal 3456 Push

41 Breadth First Search (7) Queue start goal 456 Pop

42 Breadth First Search (8) Queue start goal Push

43 Breadth First Search (9) Queue start goal 5678 Pop

44 Breadth First Search (10) Queue start goal 678 Pop

45 Breadth First Search (11) Queue start goal 78 Pop

46 Breadth First Search (12) Queue start goal 789 Push

47 Breadth First Search (13) Queue start goal 89 Pop

48 Breadth First Search (14) Queue start goal 8910 Push

49 BFS Implementation BFS { queue Q; // mark the start location as visited Q.push(start); while (Q is not empty) { t = Q.front(); for (// each unvisited neighbor n of node t) { Q.push(n); if (n == goal) Success(S); } Q.pop(); } Failure(Q); }

50 Adaptor Class Adapts the public interface of another class Adaptee: the class being used Adaptor: the new class being defined –Uses protected object of the adaptee type –Uses the adaptee’s methods to define adaptor methods Stack and Queue implemented via adaptor classes

51 Stack Adaptor Requirements Stack –push() –pop() –top() –empty() –size() Can use List, Deque –Push(): push_back() –Pop(): pop_back()

52 Class Stack template class Stack { protected: Container c; public: void push(const T & x) { c.push_back(x); } void pop() { c.pop_back(); } T top() const { return c.back(); } int empty() const { return c.empty(); } unsigned int size() const { return c.size(); } void clear() { c.clear(); } }; Declaration –Stack > floatStack; –Stack > intStack; For STL stack container –template > class stack; –stack charStack;

53 Queue Adaptor Requirements Queue –push() –pop () –front() –empty() –size() Can use List, Deque –Push(): push_front() –Pop(): pop_back()

54 Class Queue template class Queue { protected: Container c; public: void push(const T & x) { c.push_back(x); } void pop() { c.pop_front(); } T front() const { return c.front(); } int empty() const { return c.empty(); } unsigned int size() const { return c.size(); } void clear() { c.clear(); } }; Declaration Queue > floatQueue; Queue > intQueue; For STL stack container template > class queue; queue charQueue;

55 Circular Array front back animal parade queue

56 Circular Array Q.Push(“ant”) ant front back animal parade queue

57 Queue Model—FIFO Q.Push(“bee”) antbee front back animal parade queue

58 Queue Model—FIFO Q.Push(“cat”) catantbee front back animal parade queue

59 Queue Model—FIFO Q.Push(“dog”) catdogantbee front back animal parade queue

60 Queue Model—FIFO Q.Pop() catdogbee front back animal parade queue

61 Queue Model—FIFO Q.Pop() catdog front back animal parade queue

62 Expanding the Array abc abc Where are front and back? Why can’t we use all four locations? cabcab c ababc