Chapter 22 Implementing lists: linked implementations.

Slides:



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

Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Chapter 21 Implementing lists: array implementation.
Linked Lists Geletaw S..
COMP171 Fall 2005 Lists.
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
Linked Lists Linear collections.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Linked Lists Linked Lists Representation Traversing a Linked List
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
CHP-5 LinkedList.
M180: Data Structures & Algorithms in Java
Data Structures: A Pseudocode Approach with C
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
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.
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
(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.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
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.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
Linked Lists Revision Based on: v/
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Lecture 6 of Computer Science II
Chapter 4 Linked Structures.
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 5 (continued)
Lists CS 3358.
Stacks.
Chapter 4 Linked Lists
LINKED LISTS CSCD Linked Lists.
CS212D: Data Structures Week 5-6 Linked List.
Chapter 4 Unordered List.
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Chapter 9 Linked Lists.
Presentation transcript:

Chapter 22 Implementing lists: linked implementations

This chapter discusses n Linked lists u lists built through object references. u dynamic lists. u linked implementations of structures more complex than a simple sequence.

A linked list implementation n If the list is not empty u there is a first element u there is a last element u every element (except the last) has a successor u every element (except the first) has a predecessor

A linked list implementation (cont.) n An array structure is built by creating a reference to the collection of list elements which are stored sequentially in memory. n A linked list is built by creating a reference to a node of the list that, in turn contains a reference to another node of the list, etc.

LinkedList class public abstract class LinkedList implements Cloneable { … private class Node { /** * Create a Node containing specified element. * / public Node (Object element) { this.element = element; this.next = null; } Object element; Node next; }

LinkedList class (cont.) public abstract class LinkedList implements Cloneable { /** * Create a Node containing specified element. * / protected LinkedList () { size = 0; first = null; } … private int size; private Node first; }

LinkedList class (cont.)

The component next of the last Node always has a value of null. For the empty list, the LinkedList component first is null.

LinkedList methods: get public Object get (int i) { Node p = first; int pos = 0; while (pos < i) { p = p.next; pos = pos + 1; } return p.element; }

LinkedList methods: get (cont.) The variable p is initialized with a reference to the 0-th Node of the list.

LinkedList methods: get (cont.) Each iteration of the loop assigns p a reference to the next element of the list and increments pos.

LinkedList methods: get (Cont). Loop invariant: p references the Node containing the element with index pos. n At loop termination pos == i p references the Node we are looking for.

LinkedList methods: append n First, find the last element of list

LinkedList methods: append (cont.) n Create a new Node containing the element to be appended n Set the old last Nodes next component to reference the new Node. public void append (Object obj) { if (this.isEmpty()) first = new Node(obj); else { Node p = first; while (p.next != null); p = p.next; p.next = new Node(obj); } size = size + 1; }

LinkedList methods: remove n First, find the Node in front of the one we want to delete.

LinkedList methods: remove (cont.) Take next of the Node before the one to be deleted and reference it the same as the next field of the Node to be deleted.

LinkedList methods: remove (cont.) Since p.next is the Node to be deleted, p.next cannot be null. n Removing the first element of the list is a special case.

LinkedList methods: remove (cont.) public void remove (int i) { if (i == 0) { first = first.next; else { Node p = first; int pos = 0; while (pos < i-1) { p = p.next; pos = pos + 1; } p.next = p.next.next; } size = size - 1; }

LinkedList methods: add

LinkedList methods: add (cont.) public add (int i, Object obj) { Node newElement = new Node(obj); if (i == 0) { newElement.next = first; first = newElement; } else { Node p = first; int pos = 0; while (pos < i-1) { p = p.next; pos = pos + 1; } newElement.next = p.next; p.next = newElement; } size = size + 1; }

LinkedList methods: get, append, remove, and add all require linear time on average. n Deleting the first element and inserting a new first element, are constant time operations. n We must be particularly careful of boundary cases: cases involving u the empty list u a list with one element u the first or last element of a list These may well require explicit handling.

LinkedList variations A simple change will make append a constant time operation. We keep a reference to both the first and last elements in the list.

LinkedList variations (cont.) public append (Object obj) { Node newElement = new Node(obj); if (this.isEmpty()) first = newElement; else last.next = newElement; last = newElement; size = size + 1; }

LinkedList variations (cont.) remove must now check explicitly for the case in which the last element is deleted. public void remove (int i) { if (size == 1) { // remove the only element first = null; last = null; } else if (i == 0) { // remove the first element first = first.next; }

LinkedList variations (cont.) else { Node p = first; int pos = 0; while (pos < i-1) { p = p.next pos = pos + 1; } p.next = p.next.next; if (i == size-1) //last element removed last = p; } size = size - 1; }

Header nodes n One way to eliminate special cases is to employ a header node. It contains no element, but is always present at the front of the list. i.e. it is referenced by first.

Header nodes (cont.) private class Header extends Node { public Header () { this.element = null; this.next = null; } The LinkedList constructor creates the header. protected LinkedList () { size = 0; first = new Header(); last = first; }

Header nodes (cont.) n The method append, need not check explicitly for the empty list. public append (Object obj) { Node newElement = new Node(obj); last.next = newElement; last = newElement; size = size + 1; }

Circular lists n In a circular list, the last node references the first. n A circular list may or may not have a header. n We can traverse the entire list starting from any node. n Care must be taken to avoid infinite iterations or recursions.

Doubly-linked lists n Each node contains references to the preceding as well as to the following node.

Doubly-linked lists (cont.) n Three components: u the list elements u references to its two neighboring nodes. public abstract class DoublyLinkedList implements Cloneable {… private int size; private Node header; private class Node { public Node (Object element) { this.element = element; this.next = null; this.previous = null; } Object element; Node next; Node previous; }

Doubly-linked lists (cont.) protected DoublyLinkedList () { size = 0; header = new Header(); header.next = header; header.previous = header; }

Doubly-linked lists (cont.) n Operations are a bit more complicated since we have two references in each node, but the combination of a circular structure and a header eliminates the need for handling most boundary cases explicitly.

DoublyLinkedList : append n Set previous of the new node to reference the old last node. n Set next of the new node to reference the header. n Set previous of the header to reference the new node. n Set next component of the old last node to reference the new node.

DoublyLinkedList : append (cont.) public void append (Object obj) { Node newElement = new Node(obj); Node last = header.previous; newElement.next = header; newElement.previous = last; last.next = newElement; header.previous = newElement; size = size + 1; }

Linked list limitations n Accessing elements by index is a linear time operation. The get operation is linear. Therefore, any operation using the get method will be slower than in an array-based implementation. public boolean contains (List list, Object obj) { int n = list.size(); int i = 0; while (i < n && !obj.equals(list.get(i)) i = i + 1; } return i < n; }

Linked list limitations (cont.) This method can be implemented without get. public boolean contains (LinkedList list, Object obj) { Node p = list.first; while (p != null && !obj.equals(p.element)) p = p.next; } return p != null; }

Dynamic storage allocation n automatic allocation: memory space for automatic variables is allocated when the method is invoked, and reclaimed (deallocated) when the method completes. n Memory space for array elements is allocated when the array is created. n For a linked implementation, space required for a node is allocated when the node is created.

Garbage collection n Dynamically allocated space that can no longer be accessed is termed garbage. n If we create an object and then loose all references to the object, the memory space occupied by the object becomes garbage.

Garbage collection (cont.) n The Java run-time system or interpreter continuously looks for garbage and reclaims the space (garbage collection). n Many programming languages do not include garbage collection as part of their run-time systems. n They require programs to deallocate explicitly dynamically allocated space. n In an object-oriented environment, it is often difficult to know when an object no longer is accessible.

Garbage collection (cont.) n Dangling references are references to space that has been reclaimed mistakenly. n Such references result in errors that often are extremely difficult to track down.

Weve covered n Linked structures u nodes u headers n Doubly-linked lists n Circular lists n Time complexities n Dynamic storage allocation u garbage collection

Glossary

Glossary (cont.)