Revision Based on: http://www.academic.marist.edu/~jzbv/ Linked Lists Revision Based on: http://www.academic.marist.edu/~jzbv/

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linear Lists – Linked List Representation
Linked Lists Linear collections.
Linked Lists Contents 1.LinkedList implements the Interface List 2.Doubly Linked List implementation of common methods a.boolean add( Object x) -- append.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
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.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Topic 11 Linked Lists -Joel Spolsky
CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
Stacks, Queues, and Deques
Arrays and Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101,
Information and Computer Sciences University of Hawaii, Manoa
CS 307 Fundamentals of Computer ScienceLinked Lists 1 Topic 14 Linked Lists "All the kids who did great in high school writing pong games in BASIC for.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Chapter 5 Linked Lists II
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
Page 1 – Spring 2010Steffen Vissing Andersen Software Development with UML and Java 2 SDJ I2, Spring 2010 Agenda – Week 8 Linked List (a reference based.
Linked Lists Revision Based on: v/
List Interface and Linked List Mrs. Furman March 25, 2010.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Linked Data Structures
Linked List, Stacks Queues
Lecture 6 of Computer Science II
Elementary Data Structures
Linked Lists.
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
Storage Strategies: Dynamic Linking
Doubly Linked List Review - We are writing this code
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Programming Abstractions
Java collections library
LinkedList Class.
public class StrangeObject { String name; StrangeObject other; }
Top Ten Words that Almost Rhyme with “Peas”
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Topic 11 Linked Lists -Joel Spolsky
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Linked Lists.
Linked Lists [AJ 15].
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Programming Abstractions
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Dynamic Data Structures and Generics
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Problem Understanding
LINKED LISTS.
Computer Science and Engineering
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Lecture 14 Linked Lists CSE /26/2018.
Data Structures & Algorithms
Programming II (CS300) Chapter 07: Linked Lists
Stacks, Queues, and Deques
TCSS 143, Autumn 2004 Lecture Notes
Linked Lists Chapter 5 (continued)
Topic 11 Linked Lists -Joel Spolsky
Presentation transcript:

Revision Based on: http://www.academic.marist.edu/~jzbv/ Linked Lists Revision Based on: http://www.academic.marist.edu/~jzbv/

Linked Lists LinkedList is an important data structure in OO languages such as Java and C# What you need to learn is how to write the implementation. Our philosophical principle: in pursuit of absolute simplicity. How: Divide, Conquer, and Glue

What to Do? Interface IList is given Two implementations are discussed: Solution One: Doubly Linked List Class Solution Two: Singly Linked List Class

Interface IList Interface IList has the following methods: boolean add( Object x) -- append to end of the list boolean add(int i, Object x) – insert at position i boolean remove(int i) – remove object at position i

Linked Lists A LinkedList implements the interface IList There are a number of ways in which a LinkedList can be implemented internally, but each implementation should provide the client (code that uses a LinkedList) with a single common set of operations that can be evoked by messages to the LinkedList object. Types of LinkedList implementations include: Doubly linked list Singly linked list We will examine how the main List operations are implemented in each of these alternatives

Linked Lists Doubly-linked List A LinkedList is a connected sequence of Nodes. It is composed of two classes of objects: LinkedLists and Nodes. class Node { //Node methods go here //attributes private Node pred, succ; private Object data; } class LinkedList implements IList { //class Node (see left) is declared here //implementation of List methods goes here //attributes private Node head, tail; private int size; } Node is an internal class visible only to LinkedList objects. The private attributes: pred and succ, have class scope and are thus indirectly accessible via the setter/getter methods in class LinkedList. BUT for the illustration purpose, we treat them as protect, which you can set and get a value for them.

This is the the Node that p points to Linked Lists Useful terminology for referring to Nodes in a LinkedList Nodes are not named objects, but are referred to by a “pointer” Node p = new Node(theObject); p.succ = new Node(nextObject); p pred succ data pred succ data This is the the Node that p points to This Node is the successor of the Node that p points to

Linked Lists LinkedList aList = new LinkedList( ); Integer temp = new Integer(21); Node p = new Node(temp); Integer temp = new Integer(24); Node p = new Node (temp); aList.add( new Integer(24) ); aList.add( new Integer(21) ); if (head == 0) head = p; tail = p; size++; //head != 0 tail.succ = p; head size tail aList p.pred = tail; tail = p; size++; 2 1 p p 24 21

Linked Lists Putting it together public boolean add (Object theObj) { Node p = new Node(theObj); if (head == 0) head = p; else { tail.succ = p; p.pred = tail; } tail = p; size++; return true;

Linked Lists Insert at front of the list (i == 0) Inserting an object at position i Integer temp = new Integer (3); Node p = new Node(temp); aList head size tail p.pred = 0; p.succ = head; if (head != 0) head.pred = p; 3 4 head = p; size++; 10 21 24 p Insert at front of the list (i == 0) 3 The successor of Node referenced by p is Node referenced by head else (head ==0) -- inserting into an empty list -- set tail = p

Linked Lists Inserting at end of non-empty list (i==size) Inserting an object at position i Integer temp = new Integer (3); Node p = new Node(temp); aList head size tail if (i == size) { //insert at end p.pred = tail; p.succ = 0; tail.succ = p; 3 4 tail = p; size++; 10 21 24 p Inserting at end of non-empty list (i==size) 41 p’ s predecessor is the Node tail points to Make Node p points to the successor of Node tail points to The Node p points to is now at the end of the list

Linked Lists Inserting an object at position i Integer temp = new Integer (23); Node p = new Node(temp); Inserting an object at position i aList head size tail int count = 0; Node q = head; while (count < i) { count++; q = q.getNext( ); } 3 4 q q q 10 21 24 p.pred = q.getPrev( )); Inserting at general position – (0 <= i < size) p count = 0 count = 1 p.succ = q; count = 2 23 q.getPrev( ).succ = p; //3 Move a handle to the Node at position i – Let i = 2 in this example The order of these statements is important – statement 3 MUST precede statement 4. Insert new Node before the Node that q points to. q.pred = p; //4 size++;

Linked Lists Putting it all together public boolean add(int i, Object x) { if (i < 0 || i > size) { //range error System.err.println (“error – index out of range”); System.exit(1); //better – throw Exception } Node p = new Node(x); if (i ==0) { //insert at front p.pred = 0; //superfluous p.succ = head; if (head !=0) head.pred = p; else tail = p; head = p; else if (i == size) { //insert at end p.succ = 0; p.pred = tail; tail.succ = p; else { //insert at general position int count = 0; Node q = head; while (count < i) { count++; q = q.getNext( ); } p.pred = q.getPrev( ); p.succ = q; q.getPrev( ).succ = p; q.pred = p; size++; return true; q.getPrev( ) returns a reference to the previous Node – Make the successor of that Node = p.

Linked Lists Removing objects from a List Remove first Node – (i == 0) Node p = head; if (i ==0) { //remove first Node head = p.getNext( ); if (head != 0) head.pred = 0; else //removing last node tail = 0; size--; } head size tail aList 3 2 p 21 24 41 Remove first Node – (i == 0) Automatic garbage collection reclaims the unreferenced Node when p goes out of scope.

Linked Lists Removing objects from a List if (i ==size-1) { //remove last Node p = tail; //Node * p; set previously if (head != tail) { tail.getPrev().succ = 0; tail = tail.getPrev( ); } else { //removing last node tail = 0; head = 0; } size--; } Removing objects from a List head size tail aList 3 2 p 21 24 41 Remove the last node – (i == size – 1) tail.getPrev( ) Automatic garbage collection reclaims memory when p goes out of scope

Linked Lists Removing objects from a List p.getPrev( ) p.getNext( ) Node p = head; int pos = 0; while (pos < i) { //find node to cut p = p.getNext( ); //move handle pos++; } p.getPrev( ).succ = p.succ; p.getNext( ).pred = p.pred; size--; } head size tail aList 3 2 p p 21 24 41 p.getPrev( ) p.getNext( ) The Node referenced by p is deleted when p goes out of scope pos = 1 pos = 0 Remove Node in general position – ( i ==1)

Linked Lists Bringing it all back home else if (i == size-1) { //remove last node p = tail; if (head != tail) { tail.getPrev.succ = 0; tail = tail.getPrev( ); } else { head = 0; tail = 0; } } //end else if else { //remove Node in general position p = head; int pos = 0; while (pos < i ) { //move handle p = p.getNext( ); pos++ p.getPrev.succ = p.succ; p.getNext.pred = p.pred; } //end else p.setPrev(0); p.setNext(0); //for safety size--; return true; public boolean remove(int i) { if (i < 0 || i >= size) { //range error System.err.println (“error – index out of range”); System.exit(1); } Node p; if (i == 0 ) // remove first node p = head; head = p.succ; if (head != 0) head.pred = 0; else tail = 0; } //end if } //end remove

Linked Lists Singly-linked Lists public class Node { public Node(Object x) {…} public void setNext( ) {…} public Node getNext( ) {…} public Object getData( ) {…} private Node succ; private Object data; } Singly-linked Lists size head tail aList 3 data succ 21 24 47 pos = 0 pos =2 pos = 1 q p q p p Implementing methods add( ) and remove( ) will be left as an exercise. To remove the Node at index pos, remove the Node that p references. Use the Node that q references to reattach the links. Move two handles in sequence until p reaches position i – (let i = 2) for(int pos = 0; pos < i; pos++) { q = p; p = p.getNext( ); } To perform an insert at pos, insert the new Node after the Node that q references and before the Node that p references Node p = head, q = head;