Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

Slides:



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

Linked Lists Geletaw S..
Singly linked lists Doubly linked lists
© 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
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
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.
6/7/2014 8:24 AMSequences1 Lists and Sequences. 6/7/2014 8:24 AMSequences2 Outline and Reading Singly linked list Position ADT and List ADT (§5.2.1) Doubly.
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.
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.
CSC 212 – Data Structures Lecture 22: PositionList.
Review Learn about linked lists
Iterators and Sequences1 © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
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.
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
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.
© 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.
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
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.
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.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
CS 221 Analysis of Algorithms Data Structures Vectors, Lists.
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.
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
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”
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Lecture14: Hashing Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Linked List, Stacks Queues
Lecture 6 of Computer Science II
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.
Ch7. List and Iterator ADTs
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,
Data Structure Dr. Mohamed Khafagy.
COMP9024: Data Structures and Algorithms
CS212D: Data Structures Week 5-6 Linked List.
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.
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
LINKED LISTS CSCD Linked Lists.
Arrays and Linked Lists
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
CS212D: Data Structures Week 5-6 Linked List.
CS2013 Lecture 4 John Hurley Cal State LA.
Problem Understanding
Linked Lists & Iterators
LAB#4 Linked List.
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Problem Understanding
Presentation transcript:

Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Linked Lists What is the linked list?  A data structure consisting of a sequence of nodes  Each node is composed of data and link(s) to other nodes. Properties  Linked lists can be used to implement several other common abstract data types, such as stacks and queues  The elements can easily be inserted or removed without reallocation or reorganization of the entire structure  Linked lists allow insertion and removal of nodes at any point in the list in constant time.  Simple linked lists do not allow random access to the data.  2

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Singly Linked Lists A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores  Element(s)  Link to the next node 3 ABCD  next element Node

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 The Node Class for Singly Linked List 4 // Node class public class Node { private Object element; private Node next; // Constructors public Node() { this(null, null); } public Node(Object e, Node n) { element = e; next = n; } Defines element and link Creates a node with null, which references to its element and next node Creates a node with given element and next node

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 The Node Class for Singly Linked List 5 public Object getElement() { return element; } public Node getNext() { return next; } public void setElement(Object newElem) { element = newElem; } public void setNext(Node newNext) { next = newNext; } Accessor methods Modifier methods

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Inserting a Node at the Head Operation sequence  Allocate a new node.  Insert new element.  Have new node point to old head.  Update head to point to new node. 6 Node v = new Node(); v.setElement(“A”); v.setNext(head); head = v; BCD  BCD  BCD  A A head

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Removing a Node from the Head Operation sequence  Update head to point to next node in the list.  Allow garbage collector to reclaim the former first node. Java: garbage collection  Unused memory is reclaimed automatically for reuse.  No need to free or delete 7 head = head.getNext(); BCD  A BCD  A BCD  head

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Inserting a Node at the Tail Operation sequence  Allocate a new node.  Insert new element.  Have new node point to null.  Have old last node point to new node.  Update tail to point to new node. 8 Node v = new Node(); v.setElement(“A”); v.setNext(null); tail.setNext(v); tail = v; BCD  BCD  A  CDA  B head tail

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Removing a Node at the Tail Operation sequence  Move tail to the second last node.  Have tail node point to null.  Allow garbage collector to reclaim the former last node. Issues  There is no constant time way to make the tail to point to the previous node.  Removing at the tail of a singly linked list is not efficient! 9 BCD  A BCD  A  BCD  head tail head tail head

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Stack as a Linked List 10  t nodes elements

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Queue as a Linked List 11 f r  nodes elements

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Node List ADT The Node List ADT models a sequence of positions storing arbitrary objects. It establishes a before/after relation between positions. Methods  Generic methods: size(), isEmpty()  Accessor methods first(), last() prev(p), next(p)  Modifier methods: set(p, e) addBefore(p, e), addAfter(p, e), addFirst(e), addLast(e) remove(p) 12

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Doubly Linked List A doubly linked list provides a natural implementation of the Node List ADT. Nodes implement position and store:  Element  Link to the previous node  Link to the next node Special trailer and header nodes 13 prevnext element Node

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Doubly Linked List 14 trailer header nodes/positions elements

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Insertion operation  We visualize insertAfter(p, X), which returns position q. 15 ABXC ABC ABC p X q pq

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Algorithm 16 Algorithm addAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p) // link v to its predecessor v.setNext(p.getNext()) // link v to its successor (p.getNext()).setPrev(v) // link p’s old successor to v p.setNext(v) // link p to its new successor, v return v // the position for the element e

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Deletion Deletion in doubly linked list  We visualize remove(p), where p = last(). 17 ABCD p ABC D p ABC

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Deletion Algorithm 18 Algorithm remove(p): t = p.element // a temporary variable to hold the return value (p.getPrev()).setNext(p.getNext()) // linking out p (p.getNext()).setPrev(p.getPrev()) p.setPrev(null) // invalidating the position p p.setNext(null) return t

19