Recall: stacks and queues

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

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.
CS Data Structures II Review COSC 2006 April 14, 2017
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Data Structures & Algorithms
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
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.
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
CSE 143 Lecture 10 Linked List Basics reading: slides created by Marty Stepp
1 Chapter 17 – Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
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.
Understanding Data Types and Collections Lesson 2.
STACKS & QUEUES for CLASS XII ( C++).
Stack: a Linked Implementation
Building Java Programs Chapter 16
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Building Java Programs
Dr. Bernard Chen Ph.D. University of Central Arkansas
Cinda Heeren / Geoffrey Tien
slides created by Marty Stepp
Wednesday Questions How do I debug? We love to teach this at IPL/OH!
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Road Map CS Concepts Data Structures Java Language Java Collections
Linked node problem 3 What set of statements turns this picture:
Pointers and Linked Lists
Linked node problem 3 What set of statements turns this picture:
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Stacks, Queues, and Deques
Building Java Programs
Lecture 21 Stacks and Queues Richard Gesick.
CSE 143 Lecture 9 References and Linked Nodes reading: 16.1
Stacks, Queues, and Deques
Popping Items Off a Stack Lesson xx
slides created by Marty Stepp and Ethan Apter
Linked List (Part I) Data structure.
Building Java Programs
Further Data Structures
CSE 143 Lecture 6 References and Linked Nodes reading: 16.1
Lecture 7: Linked List Basics reading: 16.2
CSE 214 – Computer Science II Stacks
slides created by Marty Stepp and Hélène Martin
Stack Implementations
CSE 143 Lecture 5 References and Linked Nodes
Road Map CS Concepts Data Structures Java Language Java Collections
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.
Recall: stacks and queues
slides adapted from Marty Stepp
Building Java Programs
Stacks, Queues, and Deques
Lecture 7: Linked List Basics reading: 16.2
Lecture 6: References and linked nodes reading: 16.1
CSE 143 Lecture 7 References and Linked Nodes reading: 16.1
Wednesday Questions How do I debug? We love to teach this at IPL/OH!
slides created by Marty Stepp
Lecture 16 Stacks and Queues CSE /26/2018.
Abstract Data Types Stacks CSCI 240
Lecture 6: References and linked nodes reading: 16.1
LINEAR DATA STRUCTURES
Stack Implementations
Presentation transcript:

Recall: stacks and queues stack: retrieves elements in reverse order as added queue: retrieves elements in same order as added push pop, peek top 3 2 bottom 1 front back 1 2 3 remove, peek add queue stack

Memory for a List Array (contiguous in memory) Spread in memory 42 -3 17 9 42 9 -3 17

A list node class Each list node object stores: public class ListNode { public int data; public ListNode next; } Each list node object stores: one piece of integer data a reference to another list node ListNodes can be "linked" into chains to store a list of values: data next 42 data next -3 data next 17 data next 9 end

Linked node problem 1 What set of statements turns this picture: Into this? data next 10 data next 20 list data next 10 data next 20 data next 30 list

Linked node problem 2 What set of statements turns this picture: Into this? data next 10 data next 20 list data next 30 data next 10 data next 20 list

Reassigning references when you say: a.next = b.next; you are saying: "Make variable a.next refer to the same value as b.next." Or, "Make a.next point to the same place that b.next points." data next 10 data next 20 a data next 30 data next 40 b

Linked node problem 3 What set of statements turns this picture: Into this? data next 10 data next 20 list1 data next 30 data next 40 list2 data next 10 data next 20 data next 30 list1 data next 40 list2

Linked node problem 3 How many ListNode variables? Which variables change? B C data next 10 data next 20 list1 A E F data next 30 data next 40 list2 D C E data next 10 data next 20 data next 30 list1 data next 40 list2 D

Linked node problem 3 How many ListNode variables? Which variables change? B C data next 10 data next 20 list1 list1.next.next = list2 A E F data next 30 data next 40 list2 D C E data next 10 data next 20 data next 30 list1 data next 40 list2 F

Linked node problem 3 How many ListNode variables? Which variables change? B C data next 10 data next 20 list1 list1.next.next = list2 A list2 = list2.next E F data next 30 data next 40 list2 D C E data next 10 data next 20 data next 30 list1 data next 40 list2 D

Linked node problem 3 How many ListNode variables? Which variables change? B C data next 10 data next 20 list1 list1.next.next = list2 A list2 = list2.next list1.next.next.next = null E F data next 30 data next 40 list2 D C E data next 10 data next 20 data next 30 list1 data next 40 list2 D

Linked node problem 4 What set of statements turns this picture: Into this? data next 1 data next 2 data next 3 list1 list2 data next 1 data next 3 list1 data next 2 list2

References vs. objects variable = value; For the list at right: a variable (left side of = ) is an arrow (the base of an arrow) a value (right side of = ) is an object (a box; what an arrow points at) For the list at right: a.next = value; means to adjust where points variable = a.next; means to make variable point at 2 data next 10 data next 20 a 1 1 2