Linked Structures. See Section 3.2 of the text.

Similar documents
8. Fundamental Data Structures

CS171 Midterm Exam. October 29, Name:

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Data Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types

Stacks and Queues

CSC212:Data Structure

Data Structures and Algorithms. Roberto Sebastiani

CS S-04 Stacks and Queues 1. An Abstract Data Type is a definition of a type based on the operations that can be performed on it.

Data Structures and Algorithms

Stack and Queue. Stack:

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CS350 - Exam 1 (100 Points)

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Introduction to Data Structures

Laboratorio di Algoritmi e Strutture Dati

PA3 Design Specification

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Linked Lists, Stacks, and Queues

CIS Fall Data Structures Midterm exam 10/16/2012

Linked List Implementation of Queues

CSCA48 Winter 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

CS2012 Programming Techniques II

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough

csci 210: Data Structures Stacks and Queues

CS 112 Midterm Exam Fall 2016

THE UNIVERSITY OF WESTERN AUSTRALIA

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

DNHI Homework 3 Solutions List, Stacs and Queues

Abstract Data Types. Abstract Data Types

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

ITI Introduction to Computing II

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

Lecture 3 Linked List

Introduction to Data Structures. Introduction to Data Structures. Introduction to Data Structures. Data Structures

.:: UNIT 4 ::. STACK AND QUEUE

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 8, February 4, 2016

Queue. COMP 250 Fall queues Sept. 23, 2016

Problem One: Loops and ASCII Graphics

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 8, February 4, 2016

DS L9: Queues

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists

More Data Structures (Part 1) Stacks

Fun facts about recursion

CMSC 132: Object-Oriented Programming II. Stack and Queue

Interfaces & Generics

CMSC Introduction to Algorithms Spring 2012 Lecture 7

Apply to be a Meiklejohn! tinyurl.com/meikapply

CS 331 Midterm Exam 2

Linked lists (6.5, 16)

Basic Data Structures

UNIVERSITY REGULATIONS

CS350: Data Structures Linked Lists

Name CPTR246 Spring '17 (100 total points) Exam 3

Dartmouth College Computer Science 10, Winter 2012 Solution to Midterm Exam

Basic Data Structures 1 / 24

CSE 143 SAMPLE MIDTERM

Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

Procedural Programming & Fundamentals of Programming

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

CS 61B Spring 2017 Guerrilla Section 4 Worksheet. 11 March 2017


Stacks and queues. CSCI 135: Fundamentals of Computer Science Keith Vertanen.

16. Dynamic Data Structures

Thread-Local. Lecture 27: Concurrency 3. Dealing with the Rest. Immutable. Whenever possible, don t share resources

CS-141 Exam 2 Review November 10, 2017 Presented by the RIT Computer Science Community

CS 151 Name Final Exam December 17, 2014

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Tutorial 2: Linked Lists

Stacks and Queues. Fundamentals of Computer Science.

You must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF.

3. Fundamental Data Structures

CSC 1052 Algorithms & Data Structures II: Linked Queues

table1 = [ [1, 2, 3, 4], [4, 5, 6, 7], [8, 9,10,11] ] we get: >>> sumcol(table1,0) 13 >>> sumcol(table1,1) 16

Stack as an ADT. COMP 182: Data Structures and Program Design Lecture Slides 8. Stack as an Idea

Data Structure. Recitation VII

Information Science 2

Introduction to Computer Science II (ITI 1121) Final Examination

Queues. Queue ADT Queue Implementation Priority Queues

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Lauren Milne Summer 2015

Notes on the Exam. Question 1. Today. Comp 104:Operating Systems Concepts 11/05/2015. Revision Lectures (separate questions and answers)

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

CSE1030 Lecture #20. CSE1030 Introduction to Computer Science II. Theory: Definition of Recursion. Recursion Review

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016

Queues 4/11/18. Many of these slides based on ones by Cynthia Lee

data structures and algorithms lecture 6

Comp 204: Computer Systems and Their Implementation. Lecture 25a: Revision Lectures (separate questions and answers)

Transcription:

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: 25 37 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 head p, q If we now say q = new Node(); q.data = 49; the picture becomes

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

The statement p = q; doesn't alter the nodes at all, but it makes variable p refer to the same box as variable q: head 25 37 49 p, 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: 3 p, head q We now set some links. First p.next = q; p = q; head 3 p,q

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

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

3 head 5 p, q

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 3 5 7 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 head 17 43 tail 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 data data data 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: head data data data last How would you construct an empty Queue, and write Enqueue(), Dequeue(), Front() and IsEmpty() to go with this picture?