Linked Lists Contents 1.LinkedList implements the Interface List 2.Doubly Linked List implementation of common methods a.boolean add( Object x) -- append.

Slides:



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

Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Stacks, Queues, and Linked Lists
Queues and Linked Lists
Linear Lists – Linked List Representation
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Linked Lists Ping Zhang 2010/09/29. 2 Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link.
Double-Linked Lists and Circular Lists
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Linked Lists.
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
Data Structure Lecture-5
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
M180: Data Structures & Algorithms in Java
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.
Data Structures Simple data type –int, char, long Reference data type –Integer, String, Composite data type == Data Structure –Collection of elements.
Linked Lists [AJ 15] 1. 2 Anatomy of a Linked List  a linked list consists of:  a sequence of nodes abcd each node contains a value and a link (pointer.
Chapter 3: Abstract Data Types Lists, Stacks Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Comparison summary Array based (dynamic) Keeps place for up to 4N elements Each element takes 1 memory places Fast accession time Slow removals and insertion.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
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.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Stacks, Queues, and Deques
Week 3 - Wednesday.  What did we talk about last time?  Started linked lists.
Reference: Vinu V Das, Principles of Data Structures using C and C++
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
Linked Lists Revision Based on: v/
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Lab-12 Keerthi Nelaturu. BitList Stores bit values in Linked List Has to be stored in right to left order Example :
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Linked Data Structures
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
UNIT – I Linked Lists.
Big-O notation Linked lists
Doubly Linked List Review - We are writing this code
Revision Based on: Linked Lists Revision Based on:
Java collections library
Prof. Neary Adapted from slides by Dr. Katherine Gibson
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Queues: Implemented using Arrays
LINKED LISTS.
Circular Queues: Implemented using Arrays
Programming II (CS300) Chapter 07: Linked Lists
Queues: Implemented using Linked Lists
Presentation transcript:

Linked Lists Contents 1.LinkedList implements the Interface List 2.Doubly Linked List implementation of common methods a.boolean add( Object x) -- append to end of the list b.boolean add(int i, Object x) – insert at position i c.boolean remove(int i) – remove object at position i 3.Singly Linked List -- outline of methods 4.Circularly Linked List

Linked Lists A LinkedList implements the interface List 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 Circular-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 protected Node pred, succ; public Object data; } class LinkedList implements List { //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 protected attributes: pred and succ, have file scope and are thus directly accessible to the methods in class LinkedList

Linked Lists predsuccdatapredsuccdata p This is the the Node that p points toThis Node is the successor of the Node that p points to 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);

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

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 Inserting an object at position i aList head size tail Insert at front of the list (i == 0) Integer temp = new Integer (3); Node p = new Node(temp); p 3 p.pred = 0; p.succ = head; The successor of Node referenced by p is Node referenced by head if (head != 0) head.pred = p; else (head ==0) -- inserting into an empty list -- set tail = p head = p; size++; 4

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

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

Linked Lists Putting it all together public boolean add(int i, Object x) { if (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; tail = 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 head size tail aList 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--; } p 2 Automatic garbage collection reclaims the unreferenced Node when p goes out of scope.

Linked Lists Removing objects from a List head size tail aList Remove the last node – (i == size – 1) 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--; } p tail.getPrev( ) 2 Automatic garbage collection reclaims memory when p goes out of scope

Linked Lists Removing objects from a List head size tail aList Remove Node in general position – ( i ==1) 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--; } p pos = 0 p pos = 1 p.getPrev( )p.getNext( ) 2 The Node referenced by p is deleted when p goes out of scope

Linked Lists Bringing it all back home public boolean remove(int i) { if (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 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; } //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; } size head tail aList pp p Move two handles in sequence until p reaches position i – (let i = 2) Node p = head, q = head; q for(int pos = 0; pos < i; pos++) { q = p; p = p.getNext( ); } pos = 0 q pos = 1 pos =2 To perform an insert at pos, insert the new Node after the Node that q references and before the Node that p references To remove the Node at index pos, remove the Node that p references. Use the Node that q references to reattach the links. Implementing methods add( ) and remove( ) will be left as an exercise. datasucc

Linked Lists Circularly Linked Lists size head aList A circularly-linked list is a singly-linked list where the last Node refers back to the first Node in the list. A tail reference is not used.

Linked Lists Traversing a circularly-linked list size head aList To traverse a circularly-linked list, move a pair of handles until pos == i (or p.getNext( ) == head) Locate the node containing x = 47 Node p = head, q; while (p.getNext( ) != head && !p.getData( ).equals(x)) { q = p; p = p.getNext( ); } pq p q p An insertion can be performed after the Node that q references, and a removal will remove the Node referenced by p, using q to reattach the list.

Linked Lists A circularly-linked list with a single Node size head aList 21 1 Special case – List contains a single Node The succ field of a single Node contains a handle to itself Constructor for Node public Node(Object obj) { data = obj; succ = this ; } Create a Node referring to itself and overwrite the succ field using setNext( ) when adding to the list if Node is not the first Node to be added. The only special circumstances about adding the first or removing the last Node from a circularly-linked list involve setting the contents of head. The object under construction