Lecture5: Linked Lists Linked List Singly Linked Lists the Node Class
Total Page:16
File Type:pdf, Size:1020Kb
Linked List • What is the linked list? CSED233: Data Structures (2013F) . Adata structure consisting of a sequence of nodes . Each node is composed of data and link(s) to other nodes. Lecture5: Linked Lists • Properties . Linked lists can be used to implement several other common abstract Bohyung Han data types, such as stacks and queues CSE, POSTECH . The elements can easily be inserted or removed without reallocation [email protected] 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. http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html CSED233: Data Structures 2 by Prof. Bohyung Han, Fall 2013 Singly Linked Lists The Node Class for Singly Linked List • A singly linked list is a concrete data structure consisting of a // Node class sequence of nodes Node public class Node • Each node stores { Defines element and link private Object element; . next Element(s) private Node next; . Link to the next node // Constructors Creates a node with null, which references public Node() to its element and next node element { this(null, null); } public Node(Object e, Node n) { Creates a node with given element element = e; and next node next = n; A B C D } CSED233: Data Structures CSED233: Data Structures 3 by Prof. Bohyung Han, Fall 2013 4 by Prof. Bohyung Han, Fall 2013 The Node Class for Singly Linked List Inserting a Node at the Head public Object getElement() • head { Operation sequence . Allocate a new node. return element; } . Insert new element. Accessor methods public Node getNext() . Have new node point to old B C D { head. return next; . Update head to point to new head } node. public void setElement(Object newElem) { A B C D element = newElem; Node v = new Node(); } v.setElement(“A”); Modifier methods head public void setNext(Node newNext) v.setNext(head); head = v; { next = newNext; } } A B C D CSED233: Data Structures CSED233: Data Structures 5 by Prof. Bohyung Han, Fall 2013 6 by Prof. Bohyung Han, Fall 2013 Removing a Node from the Head Inserting a Node at the Tail • Operation sequence head • Operation sequence head tail . Update head to point . Allocate a new node. to next node in the list. Insert new element. Allow garbage collector . Have new node point to null. B C D to reclaim the former A B C D . Have old last node point to tail first node. head head new node. Update tail to point to new head = head.getNext(); node. B C D A B C D Node v = new Node(); A • Java: garbage collection head v.setElement(“A”); head tail . Unused memory is reclaimed v.setNext(null); automatically for reuse. tail.setNext(v); tail = v; . No need to free or delete B C D B C D A CSED233: Data Structures CSED233: Data Structures 7 by Prof. Bohyung Han, Fall 2013 8 by Prof. Bohyung Han, Fall 2013 Removing a Node at the Tail Stack as a Linked List • Operation sequence head tail • We can implement a stack with a singly linked list. Move tail to the second • The top element is stored at the first node of the list. last node. • The space used is and each operation of the Stack ADT . Have tail node point to null. B C D A takes time. Allow garbage collector head tail to reclaim the former last node. nodes • Issues . There is no constant time B C D t way to make the tail to A point to the previous node. head tail . Removing at the tail of a singly linked list is not efficient! elements B C D CSED233: Data Structures CSED233: Data Structures 9 by Prof. Bohyung Han, Fall 2013 10 by Prof. Bohyung Han, Fall 2013 Queue as a Linked List Node List ADT • We can implement a queue with a singly linked list. • The Node List ADT models a sequence of positions storing . The front element is stored at the first node. arbitrary objects. The rear element is stored at the last node. • It establishes a before/after relation between positions. • The space used is and each operation of the Queue ADT • Methods takes time. Generic methods: size(), isEmpty() r . Accessor methods nodes • first(), last() • prev(p), next(p) f . Modifier methods: • set(p, e) • addBefore(p, e), addAfter(p, e), • addFirst(e), addLast(e) • remove(p) elements CSED233: Data Structures CSED233: Data Structures 11 by Prof. Bohyung Han, Fall 2013 12 by Prof. Bohyung Han, Fall 2013 Doubly Linked List 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 header nodes/positions trailer node . Link to the next node • Special trailer and header nodes prev next elements elem CSED233: Data Structures CSED233: Data Structures 13 by Prof. Bohyung Han, Fall 2013 14 by Prof. Bohyung Han, Fall 2013 Insertion Insertion Algorithm • Insertion operation . We visualize insertAfter(p, X), which returns position q. Algorithm addAfter(p,e): ABC Create a new node v p v.setElement(e) v.setPrev(p) //link v to its predecessor v.setNext(p.getNext()) //link v to its successor ABq C (p.getNext()).setPrev(v) //link p’s old successor to v p.setNext(v) //link p to its new successor, v X return v //the position for the element e pq ABXC CSED233: Data Structures CSED233: Data Structures 15 by Prof. Bohyung Han, Fall 2013 16 by Prof. Bohyung Han, Fall 2013 Deletion Deletion Algorithm • Deletion in doubly linked list . We visualize remove(p), where p = last(). p Algorithm remove(p): ABCD 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 ABC p.setNext(null) p return t D ABC CSED233: Data Structures CSED233: Data Structures 17 by Prof. Bohyung Han, Fall 2013 18 by Prof. Bohyung Han, Fall 2013 19.