Singly Linked Lists Representation Space Analysis Creation and Insertion Traversal Search Deletion.

Slides:



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

1 - Recursion on linked lists Lecture 7 ADS2 Lecture 7.
Singly Linked Lists What is a singly-linked list? Why linked lists?
Stacks, Queues, and Linked Lists
Linear Lists – Linked List Representation
Section 5 Lists again. Double linked lists – insertion, deletion. Trees.
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.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
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.
The Singleton Pattern II Recursive Linked Structures.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
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.
LISTS & TREES Lecture 8 CS2110 – Fall List Overview 2  Purpose  Maintain an ordered set of elements (with possible duplication)  Common operations.
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 Singly Linked Lists: Introduction. Singly Linked Lists: Implementation. Singly Linked Lists: Analysis. Singly Linked Lists: Creation.
Singly Linked Lists Representation Space Analysis Creation and Insertion Traversal Search Deletion.
Linked Lists CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
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,
Linked Lists CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10.
Queues What is a Queue? Queue Implementations: Queue As Array
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
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 
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Linked Lists © John Urrutia 2013, All Rights Reserved1.
Introduction to Data Structures and Algorithms
12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
Chapter 5 Linked Lists II
Subject Name : Data Structure Using C Title : Linked Lists
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
Algorithms and Data Structures
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.
2/21/20161 List Operations Advanced Programming Ananda Gunawardena.
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
Java linked list.
CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
CS1020 L AB 3 (L INKED L IST ) Problem 1: Balls Problem 2: Josephine Problem 3: Keyboard.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Linked Data Structures
Section 2.6 Linked List StringLog ADT Implementation
Tirgul 12 Data Structures (2) 1.
Singly Linked Lists.
Recap: Solution of Last Lecture Activity
Activity Write the code fragment to reverse a singly linked list
8-1.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
8-1.
MyList<T> It’s a generic type, <T> is a type parameter
Data Structures ADT List
Data Structures ADT List
ADT list.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
COSC 1030 Section 8 List.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Data Structures ADT List
Singly Linked Lists Representation Space Analysis
Activity Write the code fragment to reverse a singly linked list
Doubly Linked Lists Representation Space Analysis
Presentation transcript:

Singly Linked Lists Representation Space Analysis Creation and Insertion Traversal Search Deletion

Representation We are using a representation in which a linked list has both head and tail references. public class MyLinkedList{ protected Element head; protected Element tail; //... public final class Element{ Object datum; Element next; Element(Object obj, Element element){ datum = obj; next = element; } public Object getDatum(){ return datum; } public Element getNext(){ return next; } //... } listhead tail b)

Representation: 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)

List Creation and Insertion An empty list is created as follows: MyLinkedList list = new MyLinkedList(); 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. b) head tail

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

Insertion at the beginning (Prepend) public void prepend(Object obj) { Element element = new Element(obj, head); if(head == null) tail = element; head = element; } Complexity is O(1)

Insertion before and after an element public void insertBefore(Object obj) { Element element = new Element(obj, this); if(this == head) { head = element; return; } Element lastElement = head; while (lastElement != null && lastElement.next != this) { lastElement = lastElement.next; } lastElement.next = element; } public void insertAfter(Object obj) { next = new Element(obj, next); if(this == tail) tail = next; } Complexity is O(n) Complexity is O(1)

Traversal To move a reference e from one node to the next: e = e.next; Example: Count the number of nodes in a linked list. public int countNodes(){ int count = 0; Element e = head; while(e != null){ count++; e = e.next; } return count; } Complexity is O(n)

Searching To search for an element, we traverse from head until we locate the object. Example: Count the number of nodes with datum field equal to a given object. public int countNodes(Object obj){ int count = 0; Element e = head; while(e != null){ if(e.datum.equals(obj)) count++; e = e.next; } return count; } Complexity is O(n)

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

Deletion - Difference between the MyLinkedList and the Element extracts To delete an element, we use either the extract method of MyLinkedList or that of the Element inner class. try{ list.extract(obj1); } catch(IllegalArgumentException e){ System.out.println("Element not found"); } MyLinkedList.Element e = list.find(obj1); if(e != null) e.extract(); else System.out.println("Element not found");

Deletion – Deleting First and Last Element Deleting First element public void extractFirst(){ if(head == null) throw new IllegalArgumentException("item not found"); head = head.next; if(head == null) tail = null; } Deleting Last element: - Left as Lab task Complexity is O(1)

Exercises For the MyLinkedList class, Implement each of the following methods: –String toString() –Element find(Object obj) –void ExtractLast() –void insertAt(int n) //counting the nodes from 1. State the complexity of each method. Which methods are affected if we do not use the tail reference in MyLinkedList class.