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.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks and Queues. Not really data structures – More of an enforcement of policy – Can be implemented using an array or linked list – Can store just about.
Stacks, Queues, and Linked Lists
Data Structure HKOI training /4/2010 So Pak Yeung.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
CS Data Structures II Review COSC 2006 April 14, 2017
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
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.
Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science Chapter.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
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’
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
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.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Computer Engineering Rabie A. Ramadan Lecture 6.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
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.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Queues 1. Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
G64ADS Advanced Data Structures
COSC160: Data Structures: Lists and Queues
QueueStack CS1020.
September 29 – Stacks and queues
Chapter 15 Lists Objectives
Building Java Programs
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Data Structures and Database Applications Queues in C#
Linked node problem 3 What set of statements turns this picture:
Lecture 21 Stacks and Queues Richard Gesick.
Building Java Programs
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Lecture 7: Linked List Basics reading: 16.2
ADT list.
CSE 214 – Computer Science II Stacks
Presented By: Mahmoud Rafeek Alfarra
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.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Recall: stacks and queues
Stacks and Queues.
Lecture 16 Stacks and Queues CSE /26/2018.
Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop.
slides created by Marty Stepp
Fundaments of Game Design
Using a Queue Chapter 8 introduces the queue data type.
Recall: stacks and queues
Using a Queue Chapter 8 introduces the queue data type.
CS210- Lecture 6 Jun 13, 2005 Announcements
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
CSCS-200 Data Structure and Algorithms
slides created by Marty Stepp
Lecture 16 Stacks and Queues CSE /26/2018.
Lecture 9: Stack and Queue
Stack Implementations
slides created by Marty Stepp and Hélène Martin
Data Structures & Programming
Presentation transcript:

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 up all of the numbers held within a linked list and returns the sum 14-2

Question 1 class Node { public int Data; public Node Next; } 14-3

Question 2 int Sum(Node current) { if (current == null) return 0; else return current.Data + Sum(current.Next); } int Sum(Node current) { int s = 0; while (current != null) { s += current.Data; current = current.Next; } return s; } 14-4

Stacks Implement a first-in-last-out behavior Push() to add an element Pop() to remove an element Add and remove from the same side of the internal data structure Easiest to add/remove from the front if implemented as a linked list internally Peek() returns the top element without popping it from the stack 14-5

Stacks Which data structure would you pick to implement a Stack? Why? 14-6

Queues Implement a first-in-first-out behavior Enqueue() to add an element Dequeue() to remove an element Add and remove from the opposite sides of the internal data structure If we maintain a list-tail reference, then we can add to the end and remove from the front, each having constant - O(1) - time 14-7

Queues Which data structure would you pick to implement a Queue? Why? 14-8