3 May 20151 Linked Lists CSE 2011 Winter 2011. Linked Lists2 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of.

Slides:



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

Linked Lists Geletaw S..
1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5.
Singly linked lists Doubly linked lists
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Chapter 3 Lists Dr Zeinab Eid.
Queues and Linked Lists
Data Structures ADT List
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
Doubly Linked List This problem can be easily solved by using the double linked list. - Ed. 2 and 3.: Chapter 4 - Ed. 4: Chapter 3.
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Review Learn about linked lists
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.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.
Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
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.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
Doubly Linked Lists1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 3: Arrays, Linked Lists, and Recursion
CSC 212 – Data Structures Lecture 13: Linked Lists.
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
Linear Data Structures
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Linked List, Stacks Queues
COMP9024: Data Structures and Algorithms
Lecture 6 of Computer Science II
Elementary Data Structures
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Linked List Stacks, Linked List Queues, Dequeues
Data Structure Dr. Mohamed Khafagy.
COMP9024: Data Structures and Algorithms
CS212D: Data Structures Week 5-6 Linked List.
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
EEL 4854 IT Data Structures Linked Lists
Data Structures Linked list.
CS212D: Data Structures Week 5-6 Linked List.
LINKED LISTS CSCD Linked Lists.
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
CS212D: Data Structures Week 5-6 Linked List.
Problem Understanding
Computer Science and Engineering
Linked Lists & Iterators
Header and Trailer Sentinels
CS210- Lecture 6 Jun 13, 2005 Announcements
Presentation transcript:

3 May Linked Lists CSE 2011 Winter 2011

Linked Lists2 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores  element  link to the next node next elem node ABCD 

“Node” Class for List Nodes public classNode{ // Instance variables: private Object element; private Node next; /** Creates a node with null references to its element and next node. */ public Node(){ this(null, null); } /** Creates a node with the given element and next node. */ public Node(Object e, Node n) { element = e; next = n; } // Accessor methods: public Object getElement() { return element; } public Node getNext() { return next; } // Modifier methods: public void setElement(Object newElem) { element = newElem; } public void setNext(Node newNext) { next = newNext; } 3

SLinkedList class /** Singly linked list.*/ public class SLinkedList { protected Node head;// head node of the list protected long size;// number of nodes in the list /** Default constructor that creates an empty list */ public SLinkedList() { head = null; size = 0; } //... update and search methods would go here... } 4

Linked Lists5 Inserting at the Head 1.Allocate a new node. 2.Insert new element. 3.Have new node point to old head. 4.Update head to point to new node.

Linked Lists 6 Removing at the Head 1.Update head to point to next node in the list. 2.Allow garbage collector to reclaim the former first node.

Linked Lists7 Inserting at the Tail Assume that we keep a pointer to the last element of the list (“tail”). 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 5. Update tail to point to new node

Linked Lists8 Removing at the Tail Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node.

Lists9 Doubly Linked List (3.3) Nodes store:  element  link to the previous node  link to the next node Special trailer and header nodes prevnext elem trailer header elements node

Header and Trailer Sentinels Dummy nodes which do not store any elements. To simplify programming. 10

“DNode” Class /** Node of a doubly linked list of strings */ public class DNode { protected String element;// String element stored by a node protected DNode next, prev;// Pointers to next and previous nodes /** Constructor that creates a node with given fields */ public DNode(String e, DNode p, DNode n) { element = e; prev = p; next = n; } /** Returns the element of this node */ public String getElement() { return element; } /** Returns the previous node of this node */ public DNode getPrev() { return prev; } /** Returns the next node of this node */ public DNode getNext() { return next; } /** Sets the element of this node */ public void setElement(String newElem) { element = newElem; } /** Sets the previous node of this node */ public void setPrev(DNode newPrev) { prev = newPrev; } /** Sets the next node of this node */ public void setNext(DNode newNext) { next = newNext; } } 11

“DList” Class /** Doubly linked list with nodes of type DNode storing strings. */ public class DList { protected int size; // number of elements protected DNode header, trailer; // sentinels /** Constructor that creates empty list */ public DList() { size = 0; header = new DNode(null, null, null); // create header trailer = new DNode(null, header, null); // create trailer header.setNext(trailer);// make header and trailer point to each other }... // Implementation of methods } Methods: int size() boolean isEmpty() DNode getFirst() DNode getLast() DNode getPrev(DNode v) DNode getNext(DNode v) void addBefore(DNode v, DNode z) void addAfter(DNode v, DNode z) void addFirst(DNode v) void addLast(DNode v) void remove(DNode v) 12

Insert/Remove at Either End Straightforward. Example 1: removing the last node.  Figure 3.15 (next slide) Example 2: inserting a new node at the beginning of the list (head).  Figure

Lists14 Removal at the Tail of the List ABCD p ABC D p ABC

Removal at the Tail:Algorithm Algorithm removeLast() { if size == 0 then Indicate error “empty list”; v = trailer.getPrev(); // last node u = v.getPrev(); // node before last node trailer.setPrev(u) u.setNext(trailer); v.setPrev(null); v.setNext(null); size = size – 1; } 15

Lists16 Insertion in the Middle of the List We visualize operation addAfter(p, q). ABXC ABC p ABC p X q pq

Insertion Algorithm Algorithm addAfter(p, q) { r = p.getNext; // node after p q.setPrev(p); // link q to its predecessor, p q.setNext(r); // link q to its successor, r r.setPrev(q) ); // link r to its new predecessor, q p.setNext(q); // link p to its new successor, q size = size + 1; } 17

Lists18 Removal in the Middle of the List We visualize operation remove(v). ABXC ABC u ABC u X v uv

Removal Algorithm Algorithm remove(v) { u = v.getPrev(); // node before v w = v.getNext(); // node after v w.setPrev(u); // link out v u.setNext(w); v.setPrev(null); // null out the fields of v v.setNext(null); size = size – 1; } 19

Implementation of Doubly Link Lists Section 3.3.3, p.131 (p.125 in 4 th edition). Homework: re-do the implementation without using the header and trailer sentinels. 20

21 Next time … Stacks (5.1) Queues (5.2)