1 Joe Meehean.  Conceptual Picture access only to top item last-in-first-out (LIFO) item 1 item 2 item 3 Values in Values out 2.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
The Stack Data Structure. Classic structure What is a Stack? An abstract data type in which accesses are made at only one end Last In First Out (LIFO)
Stack & Queues COP 3502.
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.
Lists List L = x0 x1 x2 x3 … xn-1 n = # elements
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
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.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
CS Data Structures II Review COSC 2006 April 14, 2017
Chapter 3: Abstract Data Types Lists, Stacks Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Stacks A stack is a data structure that only allows items to be inserted and removed at one end We call this end the top of the stack The other end is.
Data Structures & Algorithms
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Summary of lectures (1 to 11)
Stacks, Queues, and Deques
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.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Chapter 16 Stack and Queues part2
Chapter 7 Stacks II CS Data Structures I COSC 2006
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 6.6, 11, 16 Stacks 1CSCI 3333 Data Structures.
1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without.
Pointers, Stacks and Memory Addressing Computer Science and Programming Concepts.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
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 (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
CSC 205 Programming II The ADT Stack. Recap: ADT Abstract Data Type A collection of data (objects) A set of operations on that data Add Remove Retrieve.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
1 Joe Meehean. 2  empty is the queue empty  size  enqueue (add) add item to end of queue  dequeue (remove) remove and return item at front of queue.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Stack Data Structure By Marwa M. A. Elfattah. Stack - What A stack is one of the most important non- primitive linear data structure in computer science.
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.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 6.6, 11, 16 Stacks 1CSCI 3333 Data Structures.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Lecture 16 Stacks and Queues Richard Gesick. Sample test questions 1.Write a definition for a Node class that holds a number. 2.Write a method that sums.
Data Structure By Amee Trivedi.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Chapter 15 Lists Objectives
Data Structures and Algorithms
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Stacks, Queues, and Deques
Data Structures and Database Applications Stacks in C#
Lecture 21 Stacks and Queues Richard Gesick.
Data Structures and Algorithms
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.
Stacks and Queues.
Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop.
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Abstract Data Types Stacks CSCI 240
Stack Implementations
Presentation transcript:

1 Joe Meehean

 Conceptual Picture access only to top item last-in-first-out (LIFO) item 1 item 2 item 3 Values in Values out 2

 empty is the stack empty  size  push add item to top of stack  pop remove and return item from top of stack  top return (do not remove) top of stack 3

 Similar to List ADT with restricted operations implemented using arrays or linked nodes 4

 Fields T *items_; // dynamically allocated array int size_; int capacity_ 0 0 size_: items_: 1 1 X X //Empty //After s.push(X) capacity_: size_: items_: 4 4 capacity_:

2 2 size_: items: X X Y Y //After s.push(Y) 2 2 size_: items: Y Y X X //After s.push(Y) OR  Add to end or insert at the front? capacity_: 4 4

MethodTop == FrontTop == Back push?? pop?? peek?? 7

MethodTop == FrontTop == Back pushO(N)O(N) avg: O(1) popO(N)O(1) peekO(1) 8

 Fields Node *p_head_; int size_; 0 0 size_: p_head_: 1 1 //Empty //After s.push(A) A A 9 size_: p_head_:

2 2 //After s.push(B) A A B B 2 2 //After s.push(B) B B A A OR  Add to end or insert at the front? 10 size_: p_head_: size_: p_head_:

FrontSingle TailPtr Double TailPtr push???? pop???? peek???? BackSingle TailPtr Double TailPtr push???? pop???? peek???? 11

FrontSingle TailPtr Double TailPtr pushO(1) popO(1) peekO(1) BackSingle TailPtr Double TailPtr pushO(N)O(1)O(N)O(1) popO(N) O(1) peekO(N)O(1)O(N)O(1) 12

FrontSingle TailPtr Double TailPtr pushO(1) popO(1) peekO(1) BackSingle TailPtr Double TailPtr pushO(N)O(1)O(N)O(1) popO(N) O(1) peekO(N)O(1) O(N) 13  Least space  Easiest to implement

 Restricted list  Best implemented with linked nodes singly-linked, no tail pointer unlike array implementation, no O(N) case 14

 stack class in  methods empty size push pop top  Does not provide iterators 15

16