Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Slides:



Advertisements
Similar presentations
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Advertisements

Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
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.
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.
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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.
Stacks.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 7 Ming Li Department of.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
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.
Priority Queues Briana B. Morrison Adapted from Alan Eugenio Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118.
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 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
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
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
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.
C++ STL CSCI 3110.
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,
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
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,
Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Applications of Stacks and Queues Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.
Lecture 7 : Intro. to STL (Standard Template Library)
Data Structures for Midterm 2. C++ Data Structure Runtimes.
1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
1 Queues Queue API Application: Radix Sort Implementation: Using Deque Using Deque Circular Array Circular Array Priority Queue Priority Queue API Implementation.
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.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
STACK Data Structure
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
Main Index Contents 11 Main Index Contents Sets Defined by a key along with other data Sets Defined by a key along with other data Key-Value Data Key-Value.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Cpt S 122 – Data Structures Abstract Data Types
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Standard Template Library (STL)
ENERGY 211 / CME 211 Lecture 19 November 3, 2008.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 19: Stacks and Queues.
Object Oriented Programming COP3330 / CGS5409
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
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.
Visit for more Learning Resources
Stacks.
The List Container and Iterators
CMPT 225 Lecture 7 – Stack.
Presentation transcript:

Main Index Contents 11 Main Index Contents Week 4 – Stacks

Main Index Contents 2 VectorList Access an element at position/index i Direct access, V[i] Has to iterate from a known position, may use distance functiondistance Insert or remove an element at positions other than end Cause to relocate elements No relocation, just rebuild the link Sequential search an element with specific value, return the position/index Sequentially search

Main Index Contents 3 3 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative Containers VectorStackSet, Multiset DequeQueue Map, Mutltimap ListPriority Queue

Main Index Contents 4 Stacks A stack is a sequence of items that are accessible at only one end of the sequence.stack Last in, first out, (LIFO)

Main Index Contents 5 Pushing/Popping a Stack Because a pop removes the item that last added to the stack, we say that a stack has LIFO (last-in/first-out) ordering.

Main Index Contents 66 Main Index Contents CLASS stack Operations stack(); Create an empty stack bool empty(); Check whether the stack is empty. Return true if it is empty and false otherwise.

Main Index Contents 77 Main Index Contents CLASS stack Operations void pop(); Remove the item from the top of the stack. Precondition:The stack is not empty. Postcondition:Either the stack is empty or the stack has a new topmost item from a previous push. void push(const T& item); Insert the argument item at the top of the stack. Postcondition: The stack has a new item at the top.

Main Index Contents 88 Main Index Contents CLASS stack Operations int size() const; Return the number of items on the stack. T& top() const; Return a reference to the value of the item at the top of the stack. Precondition:The stack is not empty.

What would be the output on screen?

Since stack is specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from one end of the container. Stack does not have iterators. How do we remove/insert an element at positions other than top? How do we search for an element with specific value?

Main Index Contents 11 Main Index Contents Uncoupling Stack Elements

Main Index Contents 12 Main Index Contents Uncoupling Stack Elements

Main Index Contents 13 Main Index Contents Uncoupling Stack Elements

Main Index Contents 14 Main Index Contents Uncoupling Stack Elements

Main Index Contents 15 Main Index Contents Uncoupling Stack Elements

Main Index Contents 16 Main Index Contents Uncoupling Stack Elements

Applications of Stack Backtracking in a Maze: We start from one point, there are several paths. Suppose we choose a random path. After following a certain path, we realize that the path we have chosen is wrong. So we need to find a way by which we can return to the beginning of that path. This is done by pushing that points that we have reached into a stack. We can pop the last point from the stack and thus return to the last point and continue our quest to find the right path.

19 Underlying storage structure Container classes The storage structure should be selected so that the container operations can be implemented efficiently.

Stack implementation stacksstacks are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements (pushed/popped from the "back", which is known as the top of the stack). The underlying container may be any of the standard container class templates (vector, list, deque). As lone as the container could support the following operations:vectorlistdeque empty size back push_back pop_back

Stack implementation template class miniStack { public: miniStack(); void push(const T& item); void pop(); T& top(); const T& top() const; bool empty(); int size() const; private: vector stackVector; };

Main Index Contents Reading Chapter 3.6 Next week: Queue, Tree and etc… No HW due this weekend! HW3 is due next Friday. (Submission before Thursday will receive 20 additional points) 24