Singly Linked Lists.

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
DATA STRUCTURES USING C++ Chapter 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.
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.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
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.
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.
Singly Linked Lists Representation Space Analysis Creation and Insertion Traversal Search Deletion.
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,
Implementation of Linked List For more notes and topics visit: eITnotes.com.
CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
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
Unit – I Lists.
Linked Lists Chapter 5 (continued)
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Big-O notation Linked lists
Review Deleting an Element from a Linked List Deletion involves:
Recap: Solution of Last Lecture Activity
Lists CS 3358.
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Activity Write the code fragment to reverse a singly linked list
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Lists.
Chapter 17 Object-Oriented Data Structures
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Lists.
Arrays and Linked Lists
Data Structures ADT List
Data Structures ADT List
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Linked List Intro CSCE 121 J. Michael Moore.
Linked Lists.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
LINKED LIST.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Problem Understanding
Linked List.
Computer Science and Engineering
Data Structures ADT List
Singly Linked Lists Representation Space Analysis
Activity Write the code fragment to reverse a singly linked list
General List.
Linked List Intro CSCE 121.
Doubly Linked Lists Representation Space Analysis
Linked Lists Chapter 5 (continued)
Chapter 9 Linked Lists.
Problem Understanding
Presentation transcript:

Singly Linked Lists

Outline Introduction Representation Space Analysis Creation and Insertion Traversal Search Deletion

Introduction Storing data items in arrays has at least two limitations The array size is fixed once it is created: Changing the size of the array requires creating a new array ad then copying all data from the old array to the new array The data items in the array are next to each other in memory: Inserting an item inside the array requires shifting other items A linked structure is introduced to overcome limitations of arrays and allow easy insertion and deletion A collection of nodes storing data items and links to other nodes If each node has a data field and a reference field to another node called next or successor, the sequence of nodes is referred to as a singly linked list Nodes can be located anywhere in the memory The first node is called head and the last node is called tail list head tail

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 data; Element next; Element(Object obj, Element element){ data = obj; next = element; } public Object getData(){return data;} public Element getNext(){return next;} //………

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) Explanation Space 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-- data (type Object) and next (type Element). n sizeof(MyLinkedList.Element)

List Creation and Insertion head An empty list is created as follows: Once created, elements can be inserted into the list using either the append or prepend methods MyLinkedList list = new MyLinkedList(); tail for (int k = 0; k < 10; k++) list.append(new Integer(k));

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 we can also define insertAfter or InsertBefore for the Element class to insert a given object after or before a specific a node respectively: public void insertBefore(Object obj) { Element element = new Element(obj, this); if(this == head) { head = element; return; } Element previous = head; while (previous.next != this) { previous = previous.next; previous.next = element; Complexity is O(n) public void insertAfter(Object obj) { next = new Element(obj, next); if(this == tail) tail = next; } Complexity is O(1)

Traversal To move a reference e from one node to the next: Example: Count the number of nodes in a linked list. e = e.next; 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 data field equal to a given object. public int countNodes(Object obj){ int count = 0; Element e = head; while(e != null){ if(e.data.equals(obj)) count++; e = e.next; } return count; Complexity is O(n)

Deletion – Deleting First and Last Element public void extractFirst() { if(head == null) throw new IllegalArgumentException("item not found"); head = head.next; tail = null; } Complexity is O(1) public void extractLast() { if(tail == null) throw new IllegalArgumentException("item not found"); if (head == tail) head = tail = null; else { Element previous = head; while (previous.next != tail) previous = previous.next; previous.next = null; tail = previous; } Complexity is O(n)

Deletion To delete a specific element, we use either the extract method of MyLinkedList or that of the Element inner class. public void extract(Object obj) { Element element = head; Element previous = null; while(element != null && ! element.data.equals(obj)) { previous = element; element = element.next; } if(element == null) throw new IllegalArgumentException("item not found"); if(element == head) head = element.next; else previous.next = element.next; if(element == 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");

Exercises For the MyLinkedList class, Implement each of the following methods: String toString() Element find(Object obj) 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.