Singly Linked Lists Singly Linked Lists: Introduction. Singly Linked Lists: Implementation. Singly Linked Lists: Analysis. Singly Linked Lists: Creation.

Slides:



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

Chapter 3 Lists Dr Zeinab Eid.
Singly Linked Lists What is a singly-linked list? Why linked lists?
Linked Lists Chapter 4.
Queues and Linked Lists
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Data Structure Lecture-5
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.
3 May Linked Lists CSE 2011 Winter Linked Lists2 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
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.
LISTS & TREES Lecture 8 CS2110 – Fall List Overview 2  Purpose  Maintain an ordered set of elements (with possible duplication)  Common operations.
Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
Singly Linked Lists Representation Space Analysis Creation and Insertion Traversal Search Deletion.
Singly Linked Lists Representation Space Analysis Creation and Insertion Traversal Search Deletion.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
Using Linked Lists in java.util Package Linked Lists in java.util package: Introduction. LinkedList class of java.util package: Introduction. LinkedList.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
Singly Linked Lists Representation Space Analysis Creation and Insertion Traversal Search Deletion.
Queues What is a Queue? Queue Implementations: Queue As Array
13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
LISTS Slides of K. Birman, Cornell University. List Overview 2  Purpose  Maintain an ordered collection of elements (with possible duplication)  Common.
CMSC 341 Linked Lists, Stacks and Queues. 8/3/2007 UMBC CMSC 341 LSQ 2 Implementing Your Own Linked List To create a doubly linked list as seen below.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
CMSC 341 Skip Lists. 8/3/2007 UMBC CMSC 341 SkipList 2 Looking Back at Sorted Lists Sorted Linked List What is the worst case performance of find( ),
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.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
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 Java linked list. Java linked list - definition ▪ Often in programming we are required to systematically store some type of information. A prime example.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Java linked list.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Linked Lists CS 367 – Introduction to Data Structures.
LINKED LISTS.
Linked Data Structures
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
Singly Linked Lists.
Recap: Solution of Last Lecture Activity
Activity Write the code fragment to reverse a singly linked list
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Java collections library
Priority Queue.
Linked list insertion Head. Linked list insertion Head.
Notes on Assignment 1 Your code will have several classes, most notably the class that represents the entire list data structure, and the class.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists, Stacks and Queues Textbook Sections
Foundational Data Structures
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
ADT list.
Mutators for compound data Stack Queue
Queues What is a Queue? Queue Implementations: As Array
Singly Linked Lists Representation Space Analysis
Introduction to Stacks
CSC 143 Java Linked Lists.
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Introduction to Stacks
Activity Write the code fragment to reverse a singly linked list
Doubly Linked Lists Representation Space Analysis
Presentation transcript:

Singly Linked Lists Singly Linked Lists: Introduction. Singly Linked Lists: Implementation. Singly Linked Lists: Analysis. Singly Linked Lists: Creation and Destruction. Singly Linked Lists: Accessor Methods. Singly Linked Lists: Mutator Methods.

Introduction The singly-linked list is the most basic of all the linked data structures. datum next slisthead a) slisthead tail b) sentinel slisthead c) slisttail d) Circular Singly Linked Lists

Empty Singly Linked Lists head a) b) head tail d) tail sentinelhead c)

Singly Linked Lists: Implementation The Figure below illustrates the Singly Linked List scheme we have chosen to implement. slist head tail datum next datum next datum next Integer

Singly Linked Lists: Implementation (Contd.) The classes MyLinkedList and MyLinkedList.Element are shown in the code below. Element class defines the list node. 1 public class MyLinkedList { 2 protected Element head; 3 protected Element tail; 4 public final class Element { 5 Object datum; 6 Element next; 7 Element(Object datum, Element next) { 8 this.datum = datum; 9 this.next = next; 10 } // End of Element() constructor

Singly Linked Lists: Implementation (Contd.) The inner class MyLinkedList.Element has some accessor methods such as: 11 public Object getDatum() \{ 12 return datum; 13 } // End of getDatum() method public Element getNext() \{ 16 return next; 17 } // End of getNext() method 18 // Other Element Methods } // End of inner class Element 20 // Other MyLinkedList Methods } // End of class MyLinkedList

Singly Linked Lists: Space Analysis Now, we can take a look at the space requirements: S(n) = sizeof(MyLinkedList) + n sizeof(MyLinkedList.Element) = 2 sizeof(MyLinkedList.Element ref) + n [sizeof(Object ref) + sizeof(MyLinkedList.Element ref)] = (n + 2) sizeof(MyLinkedList.Element ref) + n sizeof(Object ref) ExplanationSpace Require The list reference has two fields: head (type: Element) and tail (type: Element) = 2 sizeof(MyLinkedList.Element ref) sizeof(MyLinkedList) The list has n elements of type Element. Each element has two fields-- datum (type Object) and next (type Element). n sizeof(MyLinkedList.Element)

Singly Linked Lists: Complexity Analysis The constructor of the inner class MyLinkedList.Element performs 2 Operations. It has then a constant complexity of O(1). To access the class fields, each of the two methods: getDatum() and getNext() performs 1 Operation. In this case, both methods, getDatum() and getNext(), have a constant complexity of O(1). The constructor of MyLinkedList class has an empty implementation! Regardless of this, its running time is clearly constant, i.e., O(1).

Singly Linked Lists: Creation and Destruction The following code fragment creates a new singly linked lists. MyLinkedList slist = new MyLinkedList(); Destroying (or purging) an existing list is quite easy! slist.purge(); head tail slist 1 public void purge(){ 2 head = null; 3 tail = null; 4 } // End of purge()method slist head tail head tail head tail 3 11 slist head tail head tail head tail 3 11 slist head tail head tail head tail ) Linked List [slist] 2)slist.Purge() 3) Finally! It is easy to assume that the running time of the method purge() is O(1). However, the exact running time cannot be estimated!!! Garbage!

Accessor Methods of MyLinkedList Class The accessor methods of MyLinkedList class are defined below: 1 public Element getHead() { 2 return head; 3 } // End of getHead() method 4 5 public Element getTail() { 6 return tail; 7 } // End of getTail() method 8 9 public boolean isEmpty() { 10 return head == null; 11} // End of isEmpty() method 12 public Object getFirst() throws ListEmptyException { 13 if(head == null) 14 throw new ListEmptyException(); 15 return head.datum; 16 } // End of getFirst() method public Object getLast() throws ListEmptyException { 19 if(head == null) 20 throw new ListEmptyException(); 21 return tail.datum; 22 } // End of getLast() method complexity# of OperationMethod Name O(1)1 OperationgetHead() O(1)1 OperationgetTail() O(1)1 OperationisEmpty() O(1)1 OperationgetFirst() O(1)1 OperationgetLast()

Mutator Methods of MyLinkedList Class The mutator methods of MyLinkedList class are defined below: 1 public void prepend(Object item) { 2 Element tmp = new Element(item, head); 3 if(head == null) 4 tail = tmp; 5 head = tmp; 6 } // End of prepend() method 7 public void append(Object item) { 8 Element tmp = new Element(item, null); 9 if(head == null) 10 head = tmp; 11 else 12 tail.next = tmp; 13 tail = tmp; 14 } // End of append() method 15 public void assign(MyLinkedList slist) { 16 if(slist != this) { 17 purge(); 18 for(Element tmp = slist.head; tmp != null; tmp = tmp.next) 19 append(tmp.datum); 20 } // End of the if block 21 } // End of assign() method Question: What will happen if we don’t check the list’s refrences befor performing the list assignment?

Mutator Methods of MyLinkedList Class (Contd.) 22 public void extract(Object item) throws IllegalArgumentException { 23 Element ptr = head; 24 Element prevPtr = null; 25 while(ptr != null && ptr.datum != item) { 26 prevPtr = ptr; 27 ptr = ptr.next; 28 } // End of while block 29 if(ptr == null) 30 throw new IllegalArgumentException (“Item not in the list! "); 31 if(ptr == head) 32 head = ptr.next; 33 else 34 prevPtr.next = ptr.next; 35 if(ptr == tail) 36 tail = prevPtr; 37 } // End of extract() method ComplexityMutator Name O(1)prepend() O(1)append() O(n)assign() O(n)extract()

Mutator Methods of Element Class Now, let’s consider the methods insertAfter() and insertBefore() of MyLinkedList.Element class shown below. 1 public void insertAfter(Object item) { 2 next = new Element(item, next); 3 if(tail == this) 4 tail = next; 5 } // End of insertAfter() method 6 public void insertBefore(Object item) { 7 Element tmp = new Element(item, this); 8 if(this == head) 9 head = tmp; 10 else { 11 Element prevPtr = head; 12 while(prevPtr != null && prevPtr.next != this) 13 prevPtr = prevPtr.next; 14 prevPtr.next = tmp; 15 } // End of else block 16 } // End of insertBefore() method ComplexityMutator Name O(1)insertAfter() O(n)insertBefor()

Drill Questions Let's consider the redefinition of the assign() method shown below: 1 public void assign(MyLinkedList slist) { 2 purge(); 4 for(Element tmp = slist.head; tmp != null; tmp = tmp.next) 5 append(tmp.datum); 6 } // End of assign() What are the possible problems with such an implementations? The method extract() as defined in this session needs some modifications to work correctly in all situations. Provide your own implementation for this method. Which methods are affected if we do not use the tail reference in MyLinkedList class.