Doubly Linked Lists Representation Space Analysis

Slides:



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

Singly Linked Lists What is a singly-linked list? Why linked lists?
Linear Lists – Linked List Representation
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.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Doubly-linked list library.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
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.
Doubly Linked Lists Representation Space Analysis Creation and Insertion Traversal Deletion.
Doubly Linked Lists Doubly Linked Lists: Introduction. Doubly Linked Lists: Implementation Doubly Linked Lists: Analysis Doubly Linked Lists: Creation.
Doubly-Linked Lists Cmput Lecture 16 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
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.
Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Singly Linked Lists Representation Space Analysis Creation and Insertion Traversal Search Deletion.
Singly Linked Lists Singly Linked Lists: Introduction. Singly Linked Lists: Implementation. Singly Linked Lists: Analysis. Singly Linked Lists: Creation.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Singly Linked Lists Representation Space Analysis Creation and Insertion Traversal Search Deletion.
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.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Singly Linked Lists Representation Space Analysis Creation and Insertion Traversal Search Deletion.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Lists and Iterators Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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,
Algorithms and Data Structures
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
2/21/20161 List Operations Advanced Programming Ananda Gunawardena.
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
Linked List. LINKED LIST Link is collection of similar type of elements. There are two ways of maintaining a list in memory. The first way is store the.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Linked Data Structures
Lecture 6 of Computer Science II
Java Methods Lists and Iterators Object-Oriented Programming
4. Linked Lists.
Singly Linked Lists.
Lectures linked lists Chapter 6 of textbook
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
Activity Write the code fragment to reverse a singly linked list
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
Algorithm for deleting a node from a singly linked list
Doubly linked lists.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
MyList<T> It’s a generic type, <T> is a type parameter
Linked Lists.
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Doubly Linked Lists or Two-way Linked Lists
CS212D: Data Structures Week 5-6 Linked List.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Chapter 17: Linked Lists.
LINKED LISTS.
Computer Science and Engineering
Linked Lists.
Singly Linked Lists Representation Space Analysis
Activity Write the code fragment to reverse a singly linked list
Programming II (CS300) Chapter 07: Linked Lists
Linked Lists.
Chapter 9 Linked Lists.
Presentation transcript:

Doubly Linked Lists Representation Space Analysis Creation and Insertion Traversal Deletion COSC 301: Data Structures

Representation public class DoublyLinkedList{ protected Element head, tail; //. . . public class Element { Object data; Element next, previous; Element(Object obj, Element next, Element previous){ data = obj; this.next = next; this.previous = previous; } public Object getData(){return data;} public Element getNext(){return next;} public Element getPrevious(){return previous;} // . . . list head tail COSC 301: Data Structures

Doubly Linked Lists : Space Analysis The space requirements of our representation of the doubly linked lists is as follows: S(n) = sizeof(DoublyLinkedList) + n sizeof(DoublyLinkedList.Element) = 2 sizeof(DoublyLinkedList.Element ref) + n [sizeof(Object ref) + 2 sizeof(DoublyLinkedList.Element ref)] = (2n + 2) sizeof(DoublyLinkedList.Element ref) + n sizeof(Object ref) Explanation Required space The list reference has two fields: head (type: Element) and tail (type: Element) = 2 sizeof(DoublyLinkedList.Element ref) sizeof(DoublyLinkedList) The list has n elements of type Element. Each element has three fields-- previous (type Element), data (type Object), and next (type Element) n sizeof(DoublyLinkedList. Element) COSC 301: Data Structures

List Creation and Insertion b) head tail An empty doubly linked list is created as follows: DoublyLinkedList list = new DoublyLinkedList(); Like singly link list, once Created, elements can be inserted into the list using either the append or prepend methods for (int k = 0; k < 10; k++) list.append(new Int(k)); Also if we have reference to a node (an element), we can use insertAfter or InsertBefore of the Element class.. COSC 301: Data Structures

Insertion at the end (append) public void append(Object obj){ Element element = new Element(obj, null, tail); if(head == null) head = tail = element; else { tail.next = element; tail = element; } Complexity is O(1) COSC 301: Data Structures

Insertion at the beginning (prepend) public void prepend(Object obj){ Element element = new Element(obj, head, null); if(head == null) head = tail = element; else { head.previous = element; head = element; } Complexity is O(1) COSC 301: Data Structures

Insertion before an element Inserting before the current node (this) that is neither the first nor the last node: Element element = new Element(obj, this, this.previous); this.previous.next = element; this.previous = element; Complexity is O(1) COSC 301: Data Structures

Traversal Example: Count the number of nodes in a linked list. For DoublyLinked list, traversal can be done in either direction. Forward, starting from head, or backward starting from tail. Example: Count the number of nodes in a linked list. Element e = head; while (e != null) { //do something e = e.next; } Element e = tail; while (e != null) { //do something e = e.previous; } public int countNodes(){ int count = 0; Element e = head; while(e != null){ count++; e = e.next; } return count; Complexity is O(n) COSC 301: Data Structures

Traversal Example: The following computes the sum of the last n nodes: public int sumLastNnodes(int n){ if(n <= 0) throw new IllegalArgumentException("Wrong: " + n); if(head == null) throw new ListEmptyException(); int count = 0, sum = 0; Element e = tail; while(e != null && count < n){ sum += ((Integer)e.data).intValue(); count++; e = e.previous; } if(count < n) throw new IllegalArgumentException(“No. of nodes < "+n); return sum; Complexity is O(n) COSC 301: Data Structures

Deletion To delete an element, we use either the extract method of DoublyLinkedList or that of the Element inner class. public void extract(Object obj){ Element element = head; while((element != null) && (!element.data.equals(obj))) element = element.next; if(element == null) throw new IllegalArgumentException("item not found"); if(element == head) { head = element.next; if(element.next != null) element.next.previous = null; }else{ element.previous.next = element.next; element.next.previous = element.previous; } if(element == tail) tail = element.previous; Complexity is O(n) COSC 301: Data Structures

Exercises For the DoublyLinkedList class, Implement each of the following methods and state its complexity. String toString() Element find(Object obj) void ExtractLast() void ExtractFirst() void ExtractLastN(int n) For the DoublyLinkedList.Element inner class, implement each of the following methods and state its complexity. void insertBefore() void insertAfter() void extract() What are the methods of DoublyLinkedList and its Element inner class are more efficient than those of MyLinkedList class? COSC 301: Data Structures