Linked Structures See Section 3.2 of the text.. First, notice that Java allows classes to be recursive, in the sense that a class can have an element.

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Chapter 24 Lists, Stacks, and Queues
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
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.
1 Queues (Continued) Applications for a queue Linked queue implementation Array queue implementation Circular array queue implementation Reading L&C 3.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
CS Data Structures II Review COSC 2006 April 14, 2017
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Linked Lists. Outline Why linked lists? Linked lists basics Implementation Basic primitives ­Searching ­Inserting ­Deleting.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Circular List Next field in the last node contains a pointer back to the first node rather than null pointer From any point in such a list it is possible.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
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
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
Stack and Queue.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
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’
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Stacks And Queues Chapter 18.
QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue.
1 Lecture 14: Queues Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CS 367 Introduction to Data Structures Lecture 5.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Winter 2006CISC121 - Prof. McLeod1 Stuff Solution to midterm is posted. Marking has just started… Lab for this week is not posted (yet?). Final exam (full.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Stacks – Cont’d Nour El-Kadri ITI Summary We have seen that arrays are efficient data structures. Accessing any element of an array necessitates.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
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.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Linked Data Structures
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Data Structure Interview Question and Answers
Linked List Stacks, Linked List Queues, Dequeues
Chapter 15 Lists Objectives
CS 1114: Implementing Search
Doubly Linked List Review - We are writing this code
Week 15 – Monday CS221.
Introduction to Data Structure
Chapter 17 Object-Oriented Data Structures
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Lesson Objectives Aims
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Data Structures and Algorithms for Information Processing
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Presentation transcript:

Linked Structures See Section 3.2 of the text.

First, notice that Java allows classes to be recursive, in the sense that a class can have an element which is itself an object of that class: class Person { String name; Person spouse;.... }

We can use this to build linked structures of almost any shape or complexity. Each element of the structure is a node. A node can hold data, and it also has links to other nodes.

For example, for a linear structure we might use a node class like this: public class Node { int data; Node next; public Node() {... } We can think of a Node as a box with 2 fields: data next

The code p = new Node(); p.data = 25; q = new Node(); q.data = 37; p.next = q; creates the following structure: p q

The code head = new Node(); p = head; q = new Node(); q.data = 25; p.next = q; p = q; q = new Node(); q.data = 37; p.next = q; p = q; creates the following structure:

25 37 p, qhead If we now say q = new Node(); q.data = 49; the picture becomes

25 37 p head q The statement p.next = q; links p's box to q's box p head q 49

25 37 head p, q 49 The statement p = q; doesn't alter the nodes at all, but it makes variable p refer to the same box as variable q: Of course, we can keep going; there is no limit to the number of nodes you can construct and link together.

Here is an example of a circular structure using the same Node class. We start off head = new Node(); p = head; p, head

The code q = new Node(); q.data = 3; makes this: p, head q 3 We now set some links. First p.next = q; p = q; head p,q 3

We now say q.next = head.next; Since head.next is currently the same node as q, this makes q point to itself: head p,q 3 Now do these four statements again (with new data): q = new Node(); q.data = 5; p.next = q; q.next = head.next;

head p 3 q 5 Two more statements complete the cycle: p = q; q.next = head.next;

head 3 p, q 5

Once more. First we make a new node, put data in it, and connect our structure to it: q = new Node(); q.data = 7; p.next = q

q is now the last box in the list; we make it point back around at the first: q.next = head.next; p = q; head p, q

Note the role of the "head" node in these examples. It is a node with no data whose only role is to point at a feature of the structure, which in these examples is the first node of the structure. Such a node is called a "sentinel".

Good use of sentinels is one of the keys to successful programming with linked structures. Among other things, they can help you avoid special cases, a likely source of bugs.

For example, suppose you are making a simple linked list. Here are pictures of an empty list and a list with two data values: head, tail headtail 1743 In either case the code to add a node with datum 25 at the end of the list is q = new Node(); q.data = 25; tail.next = q; tail = q;

If we want to add a node with datum 16 at the start of the list there is a special case to worry about, because if the list is initially empty we will need to update the variable tail: q = new Node(); q.data = 16; if (head==tail) { /* the list was empty */ tail = q; head.next = q; } else { q.next = head.next; head.next = q; }

You should form the habit of always drawing pictures of your linked structures and using those pictures as a guide to your coding. If the picture works your code will work.

Now, why do we do this (other than it is great fun)??

For one thing, this gives us natural implementations of trees and graphs and other non-linear structures that aren't easy to model in other ways.

Sometimes, when we are dealing with large amounts of data, linked structures can be implemented more efficiently than array-based or ArrayList-based structures.

For example, suppose we have a queue with n elements based on an ArrayList. How would you estimate the worst-case running time of the enqueue() and dequeue() operations? With a good linked structure they are both O(1).

Here is a picture for a Stack structure data top bottom How would you initialize or construct a Stack? How would you write Push(), Pop(), Top() and IsEmpty()??

Here is a picture of a Queue structure: data headlast How would you construct an empty Queue, and write Enqueue(), Dequeue(), Front() and IsEmpty() to go with this picture?