linked list.ppt

21
CS 307 Fundamentals of Computer Scienc e 1 Linked Lists many slides taken from Mike Scott, UT Austin

Upload: taharazvi

Post on 18-Nov-2015

1 views

Category:

Documents


0 download

TRANSCRIPT

  • Linked Listsmany slides taken from Mike Scott, UT Austin

  • Recursive Data StructuresLinked Lists are dynamic data structures

    They grow and shrink one element at a time, normally without some of the inefficiencies of arraysBig O of Array Manipulations

    Access the kth elementAdd or delete an element in the middle of the array while maintaining relative orderadding element at the end of array? space avail? no space avail? add element at beginning of an arrayIf accesses are all at beginning or end of list, a linked structure offers improvementsLinked Lists could be used as the underlying storage container for higher level ADTs (stack, queue)

  • Nodes and ListsA different way of implementing a listEach element of a Linked List is a separate Node object.Each Node tracks a single piece of data plus a reference (pointer) to the next Create a new Node every time we add something to the ListRemove nodes when item removed from list and allow garbage collector to reclaim that memory

  • Elements of Linked Lists

    public class Node{private Object myData;private Node myNext;public Node(){myData = null; myNext = null;}public Node(Object data, Node next){myData = data;myNext = next; }public Object getData(){return myData;}public Node getNext(){return myNext;}public void setData(Object data){myData = data;}public void setNext(Node next){myNext = next;}}

  • One Implementation of a Linked ListThe Nodes shown on the previous slide are singly linked

    a node refers only to the next node in the structureit is also possible to have doubly linked nodes. The node has a reference to the next node in the structure and the previous node in the structure as wellHow is the end of the list indicated

    myNext = null for last nodea separate dummy node class / object

  • A Simple List Interface

    public interface IList{void add(Object item);void add(Object item, int pos);Object set(Object item, int pos);void add(List other);Object get(int pos);Object remove(int pos);int size();}

  • A Linked List Implementation

    public class LinkedList implements Ilist{private Node myHead;private Node myTail;private int iMySize;

    public LinkedList(){myHead = null;myTail = null;iMySize = 0;}}LinkedList list = new LinkedList();LinkedListmyHead iMySize

    myTailnullnull0

  • Add Element - List Empty (Before)

    myHeadmyTailiMySizenullnull0

  • Add Element - List Empty (After)

    myHeadmyTailiMySize 1

    Object

  • Add Element - List Not Empty (Before)

    1

    Object

    NodemyDatamyNext

    null myHeadmyTailiMySize

  • Add Element - List Not Empty (After)

    2Object

    NodemyDatamyNext

    myHeadmyTailiMySizeObjectnull

  • Code for default addpublic void add(Object item)

  • Code for arbitrary addpublic void add(Object item, int pos)

  • Code for getpublic Object get(int pos)

  • Code for removepublic Object remove(int pos)

  • Why Linked ListAre any operations with a Linked List faster than the version from ArrayList?

    What about this:public void addFront(Object item)

    Big O? Array version Big O?Fast performance for removeFront as well?

  • Remove Back Methodpublic Object removeBack()

    Big O?

  • Other Possible Features of Linked ListsDoubly Linked

    CircularDummy Nodes for first and last node in list

    public class DLNode{private Object myData;private DLNode myNext;private DLNode myPrevious;

    }

  • Default add for Doubly Linked Listpublic void add(Object item)

  • Code for arbitrary add - Doubly Linked Listpublic void remove(Object item, int pos)

  • Code for removeDoubly Linked Listpublic Object remove(int pos)