Preeja Unit1 Linked List (1)

download Preeja Unit1 Linked List (1)

of 26

Transcript of Preeja Unit1 Linked List (1)

  • 8/11/2019 Preeja Unit1 Linked List (1)

    1/26

    Preeja

  • 8/11/2019 Preeja Unit1 Linked List (1)

    2/26

    Suppose I have an array: 1,4,10,19,6 I want to insert a 7 between the 4 and the 10

    What do I need to do?

  • 8/11/2019 Preeja Unit1 Linked List (1)

    3/26

    A linked list is fundamentally different way of storing collections each element stores a reference to the element after it

    Arrays

  • 8/11/2019 Preeja Unit1 Linked List (1)

    4/26

    Linked Lists vs Arrays Arrays have a pre -determined fixed size easy access to any element a[i] in constant time

    no space overhead Size = n x sizeof(element)Linked lists no fixed size; grow one element at a time space overhead each element must store an additional reference Size = n x sizeof (element) + n x sizeof(reference) no easy access to i -th element wrt the head of the list need to hop through all previous elements

  • 8/11/2019 Preeja Unit1 Linked List (1)

    5/26

    Linked list List of items, called nodes

    The order of the nodes is determined by the address,called the link, stored in each nodeEvery node (except the last node) contains the addressof the next node

    Components of a nodeData: stores the relevant information Link: stores the address of the next node

  • 8/11/2019 Preeja Unit1 Linked List (1)

    6/26

    Head or firstHolds the address of the first node in the listThe info part of the node can be either a value of aprimitive type or a reference to an object

  • 8/11/2019 Preeja Unit1 Linked List (1)

    7/26

    Class Node Represents nodes on a list

    It has two instance variablesinfo (of type int, but it can be any other type) link (of type Node)

    public class Node {public int info;public Node link;

    }

  • 8/11/2019 Preeja Unit1 Linked List (1)

    8/26

    Linked List: Some Properties

  • 8/11/2019 Preeja Unit1 Linked List (1)

    9/26

    Now consider the statement

    current = head;

  • 8/11/2019 Preeja Unit1 Linked List (1)

    10/26

    Now consider the statement

    current = current. Link;

  • 8/11/2019 Preeja Unit1 Linked List (1)

    11/26

  • 8/11/2019 Preeja Unit1 Linked List (1)

    12/26

    Traversing a Linked ListBasic operations of a linked list that require the link

    to be traversed

    Search the list for an itemInsert an item in the listDelete an item from the list

    You cannot use head to traverse the list You would lose the nodes of the list Use another reference variable of the same type as

    head: current

  • 8/11/2019 Preeja Unit1 Linked List (1)

    13/26

  • 8/11/2019 Preeja Unit1 Linked List (1)

    14/26

    Write code to print out the data stored in each nodein a linked list

    current = head; while (current != null){System.out.println (current.info + ); current = current.link;}

  • 8/11/2019 Preeja Unit1 Linked List (1)

    15/26

    InsertionConsider the following linked list

    You want to create a new node with info 50 and insertit after p

  • 8/11/2019 Preeja Unit1 Linked List (1)

    16/26

    The following statements create and store 50 in theinfo field of a new nodeNode newNode = new Node(); //create newNodenewNode.info = 50; //store 50 in the new node

  • 8/11/2019 Preeja Unit1 Linked List (1)

    17/26

    The following statements insert the node in the linkedlist at the required placenewNode.link = p.link;p.link = newNode;

  • 8/11/2019 Preeja Unit1 Linked List (1)

    18/26

  • 8/11/2019 Preeja Unit1 Linked List (1)

    19/26

    DeletionConsider the following linked list

  • 8/11/2019 Preeja Unit1 Linked List (1)

    20/26

    Linked Lists 20

    Inserting at the Head1. Allocate a new node2. update new element

    3. Have new node pointto old head

    4. Update head to pointto new node

  • 8/11/2019 Preeja Unit1 Linked List (1)

    21/26

    Linked Lists 21

    Inserting at the Tail1. Allocate a new node2. Insert new element3. Have new node

    point to null4. Have old last node

    point to new node

    5. Update tail to pointto new node

  • 8/11/2019 Preeja Unit1 Linked List (1)

    22/26

    Linked Lists 22

    Removing at the Head

    1. Update head topoint to next nodein the list

    2. Allow garbagecollector to reclaimthe former first

    node

  • 8/11/2019 Preeja Unit1 Linked List (1)

    23/26

    Linked Lists 23

    Removing at the Tail

    Removing at the tailof a singly linked listis not efficient!There is no constant-time way to updatethe tail to point to the

    previous node

  • 8/11/2019 Preeja Unit1 Linked List (1)

    24/26

  • 8/11/2019 Preeja Unit1 Linked List (1)

    25/26

  • 8/11/2019 Preeja Unit1 Linked List (1)

    26/26

    Write code to set the data in the 5th node to be 10current = head;cnt = 0; while (cnt < 4 && current != null) {current = current.link;}

    if (current != null) {current.info = 10;}