C++ STL Stack, Queue, and Deque

Slides:



Advertisements
Similar presentations
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Advertisements

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.
The Standard Template Library – part 2. auto_ptr Regular pointers may cause memory leaks Regular pointers may cause memory leaks void f() { SomeClass.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Standard Template Library. Homework List HW will be posted on webpage Due Nov 15.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Writing Your Own STL Container Ray Lischner
Standard Template Library There are 3 key components in the STL –Containers –Iterators –Algorithms Promote reuse More debugged May be more efficient.
Stack and Queue.
Data Structures Using C++ 2E
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.
Containers Overview and Class Vector
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.
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 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.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Collection Classes Eric Roberts CS 106B January 14, 2013 (Part 1: Vectors, Grids, Stacks, and Queues)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
1 Stacks and Queues Reading: Sections 3.6 and 3.7.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Queue Queue – First In / First Out (FIFO) data structure that supports two operations: push for adding new element at the end of the queue pop for removing.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture 7 : Intro. to STL (Standard Template Library)
Data Structures for Midterm 2. C++ Data Structure Runtimes.
A gentle introduction to the standard template library (STL) Some references:
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
C++ Review STL CONTAINERS.
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.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Collection types CS Chakrabarti Motivation  Thus far the only collection types we have used are vector and matrix  Problem #1: given an input.
CSCI-383 Object-Oriented Programming & Design Lecture 30.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
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.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
CS212: Object Oriented Analysis and Design
Introduction to olympic programming
Standard Template Library
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Vectors Holds a set of elements, like an array
Standard Template Library (STL)
C++ STL Vector Container
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
C++ STL List Container C++ STL list container Examples
Recitation Outline C++ STL associative containers Examples
Recursive Linked List Operations
Level 1 STL – Linear DS fb.com/acmicpcfcicu. Static Arrays Creating 1D, 2D, ND Static Arrays. Accessing Time. Traversing a static array with N dimensions.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Lecture 8 : Intro. to STL (Standard Template Library)
Evaluation of List Implementations
Jordi Cortadella and Jordi Petit Department of Computer Science
Lab4 problems More about templates Some STL
Recitation Outline Hash tables in C++ STL Examples Recursive example
Data Structures & Programming
Presentation transcript:

C++ STL Stack, Queue, and Deque C++ STL stack and queue containers Problems Checking if symbols are balanced Checking if input strings are palindrome A programming contest problem (steps) C++ STL deque container Word puzzle problem All examples are in Examples/r5

C++ STL Stack stack(): zero parameter default constructor stack(const Container & con): one parameter constructor empty(): check if the stack is empty push(cont T& val): push value val into the stack pop(): delete the top element from the stack size(): return the number of elements in stack top(): return a reference to the top elements in stack More information http://www.cppreference.com/wiki/stl/stack/start

C++ STL Queue queue(): zero parameter default constructor queue(const Container & con): one parameter constructor empty(): check if the queue is empty push(cont T& val): add element val to the end of the queue pop(): delete the front element from the queue size(): return the number of elements in queue front(): return a reference to the front element in queue back(): return reference to the last element in queue More information http://www.cppreference.com/wiki/stl/queue/start

P1: Checking If Symbols are Balanced Problem description (balancingsymbols.pdf) Outline of the solution (using stack) Source code (which contains a run-time error) examples/r5/balancingsymbols_wrong.cpp Example input file: balancingsymbols.input Demo how to debug this program using gdb with coredump file while (has next symbol) if next symbol is [ or ( stk.push(symbol); else if next symbol is ] or ) if stk.top() == symbol stk.pop() else not balanced end while if (!stk.empty())

P2: Checking if Input Strings are Palindrome Problem statement (palindrome.pdf) Basic idea Using a queue and stack to save input string in both queue and stack Pop out characters from queue and stack and compare them one by one Note that queue provides the forward reading, while stack provides the backward reading Palindrome if characters from queue and stack are all equal Source code examples/r5/palindrome.cpp Example input file: palindrome.input

Palindrome: Recursive Version How to recursively determine if a string is palindrome? See examples/r5/palindrome_recursive.cpp

Steps This is a programming contest problem Problem statement A bit more complicated than our normal problems But you can see the usages of STL containers and recursive algorithms Problem statement steps.pdf Solutions steps_stack.cpp (using stack to maintain the steps) steps_recursive.cpp (a recursive solution, note how we pass parameter total) Test case steps_input.txt steps_output.txt To compile Make steps_stack.x (or make steps_recursive.x)

STL Deque Container Header file <deque> Important member functions begin() and end(): return iterators front() and back(): reference to first and last elements clear(): delete all elements push_front(): insert at front push_back(): insert at end pop_front(): delete first element pop_back(): delete last element size(): number of elements empty(): if vector is emtpy resize(): change vector size More information at http://www.cplusplus.com/reference/deque/deque/

Word Puzzle Problem description Explaining the source code word_puzzle.pdf Explaining the source code word_puzzle_deque.h, word_puzzle_deque.cpp Pay attention to the use of I/O streams, stringstreams, strings, and deque To compile: make