Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Similar presentations


Presentation on theme: "Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,"— Presentation transcript:

1 Linked List

2 p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element, a second element……and a last element. p Frequently, we want to add items to or delete items from a list Introduction

3 A Shopping List Milk Eggs Butter Tomatoes Apples Oranges Bread Milk Eggs Butter Tomatoes Apples Oranges Bread Milk Eggs Butter Tomatoes Apples Oranges Bread Chicken Corn Lettuce Milk Eggs Butter Tomatoes Apples Oranges Bread Chicken Corn Lettuce

4 p A linked lists, or one-way list, is a linear collection of data elements, called nodes, where the linear order is given by means of pointers. p Each node is divided into 2 parts: 1. the first part contains the information of the element, and 2. the second part, called the link field or nextpointer field, contains the address of the next node in the list. Linked Lists

5 Linked lists p A linked list consists of a sequence of nodes connected by links, plus a header. p Each node (except the last) has a successor, and each node (except the first) has a predecessor. p Each node contains a single element (object or value), plus links to its successor and/or predecessor. antbatcat header null link node element link

6 Linked lists (2) p The length of a linked list is the number of nodes. p An empty linked list has no nodes. p In a linked list: p We can manipulate the individual elements. p We can manipulate the links, thus changing the linked lists very structure! (This is impossible in an array.)

7 Singly-linked lists (1) p A singly-linked list (SLL) consists of a sequence of nodes, connected by links in one direction only. p Each SLL node contains a single element, plus a link to the nodes successor (or a null link if the node has no successor). p An SLL header contains a link to the SLLs first node (or a null link if the SLL is empty). pigdogratcatdog

8 antbatcat first Example: SLL traversal Instance method (in class SLL ) to traverse an SLL: Instance method (in class SLL ) to traverse an SLL: public void printFirstToLast () { // Print all elements in this SLL, in first-to-last order. for (SLLNode curr = this.first; curr != null; curr = curr.succ) System.out.println(curr.element); } p Animation: antbatcat first curr antbatcat first curr antbatcat first curr antbatcat first curr

9 Bed Number Patient 1Kirk 2 3Dean 4Maxwell 5Adams 6 7Lane 8Green 9Samuels 10 11Fields 12NelsonsNext A hospital ward contains 12 beds, of which 9 are occupied. Suppose we want an alphabetical listing of the patients. Questions5START

10 Bed Number Patient 1Kirk 2 3Dean 4Maxwell 5Adams 6 7Lane 8Green 9Samuels 10 11Fields 12NelsonsNext Answer! PatientBed Adam5 Dean3 Fields11 Green8 Kirk1 Lane7 Maxwell4 Nelson12 Samuels9

11 INFO 1 2 3O 4T 5 6SPACE 7X 8 9N 10I 11E 12LINK What is the actual word based on the list? Questions9START

12 Answer! START = 9, so INFO[9]=N LINK[9]=3, so INFO[3]=O LINK[3]=6, so INFO[6]=SPACE LINK[6]=11, so INFO[11]=E LINK[11]=7, so INFO[7]=X LINK[7]=10, so INFO[10]=I LINK[10]=4, so INFO[4]=T LINK[4]=0 – a NULL Value INFO 1 2 3O 4T 5 6SPACE 7X 8 9N 10I 11E 12LINK START

13 TEST LINK What is the score of both SCIENCE & ART? Questions3SCIENCE8ART

14 TEST LINK SCIENCE 84, 62, 74, 100, ART 88, 74, 93, 82 Answer:3SCIENCE8ART

15 Customer 1Vito 2Hunter 3Katz 4Evans 5Rogers 6Tellers 7Jones 8Grant 9McBride 10Weston 11Scott 12AdamsLINK Suppose a brokerage firm has 4 broker and each broker QuestionsBrokerPointer1Bond12 2Kelly3 3Hall0 4Nelson9

16 Customer 1Vito 2Hunter 3Katz 4Evans 5Rogers 6Tellers 7Jones 8Grant 9McBride 10Weston 11Scott 12AdamsLINK Bonds list of Customer would be:Grant, Scott, Vito, Katz 2. Kellys list of customer would be: Hunter, McBridge, Evans 3. Nelsons list of customer would be: Tellers, Jones, Adams, Rogers, Weston AnswerBrokerPointer1Bond8 2Kelly2 3Hall0 4Nelson6

17 NameSSNSEXSALARY 1DAVIS Female22,800 2KELLY Male19,000 3GREEN Male27,200 4BROWN Female14,700 5LEWIS Female16,400 6COHEN Male19,000 7RUBIN Female15,500 8EVAN Male34,200 9HARRIS Female22, LINK Question4Start

18 p p Each node in the linked list is a class, as shown here. data link 10 data link 15 data link 7 null Declarations for Linked Lists

19 data link 7 p p The data portion of each node is an int. link null public class IntNode { private int data;... } data link 15 Declarations for Linked Lists data 10

20 p p Each IntNode also contains a link which refers to another IntNode. data 15 data 7 public class IntNode { private int data; private IntNode link;... } Declarations for Linked Lists data 10 link null link

21 Declarations for Linked Lists p p A program can keep track of the front node by using a variable such as head in this example. p p Notice that head is not an IntNode -- it is a reference to an IntNode. data link 10 data link 15 data link 7 null head

22 Declarations for Linked Lists p p A program can keep track of the front node by using an IntNode reference variable such as head. p p Notice that head is not an IntNode -- it is a reference to an IntNode. We represent the empty list by storing null in the head reference. head null

23 Inserting an IntNode at the Front We want to add a new entry, 13, to the front of the linked list shown here null head

24 Inserting an IntNode at the Front ¶ ¶Create a new node null head

25 Inserting an IntNode at the Front ¶ ¶Create a new node... · ·Place the data in the new node's data field null head 13

26 Inserting an IntNode at the Front null head 13 ¶ ¶Create a new node... · ·Place the data in the new node's data field.... Ž ŽConnect the new node to the front of the list.

27 Inserting an IntNode at the Front null head 13 ¶Create a new node... ·Place the data in the new node's data field.... Ž Connect the new node to the front of the list. ¹Make the head refer to the new head of the linked list.

28 Inserting an IntNode at the Front null head 13 ¶Create a new node... ·Place the data in the new node's data field.... Ž Connect the new node to the front of the list. ¹Make the head refer to the new head of the linked list.

29 Pseudocode for Inserting IntNodes p p IntNodes are often inserted at places other than the front of a linked list. p p There is a general pseudocode that you can follow for any insertion function...

30 Pseudocode for Inserting IntNodes ¶ ¶Determine whether the new node will be the first node in the linked list. If so, then there is only one step: head = new IntNode(newEntry, head);

31 Pseudocode for Inserting IntNodes ·Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the node which is just before the new node's position.

32 Pseudocode for Inserting IntNodes null head ·Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the node which is just before the new node's position. In this example, the new node will be the second node In this example, the new node will be the second node previous

33 Pseudocode for Inserting IntNodes null head ·Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the node which is just before the new node's position What is the name of this link? Look at the link which is in the node previous Look at the link which is in the node previous

34 Pseudocode for Inserting IntNodes null head ·Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the node which is just before the new node's position This link is called previous.link This link is called previous.link What is the name of this link ? previous

35 Pseudocode for Inserting IntNodes null head ËOtherwise (if the new node will not be first): Start by setting a reference named previous to refer to the node which is just before the new node's position previous.link refers to the head of a small linked list, with 10 and 7 previous.link refers to the head of a small linked list, with 10 and 7 previous

36 Pseudocode for Inserting IntNodes null head ·Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the node which is just before the new node's position. The new node must be inserted at the front of this small linked list. The new node must be inserted at the front of this small linked list. 13 Write one Java statement which will do the insertion. previous

37 Pseudocode for Inserting IntNodes null head ·Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the node which is just before the new node's position. 13 Write one Java statement which will do the insertion. previous previous.link = new IntNode(newEntry, previous.link);

38 Pseudocode for Removing IntNodes p p IntNodes often need to be removed from a linked list. p p As with insertion, there is a technique for removing a node from the front of a list, and a technique for removing a node from elsewhere. p p Well look at the technique for removing a node from the front of a linked list.

39 Removing the Head IntNode null head 13 Draw the change that this statement will make to the linked list.

40 Removing the Head IntNode null head 13

41 Removing the Head IntNode null head 13

42 Removing the Head IntNode Heres what the linked list looks like after the removal finishes null head

43 p p It is easy to insert or remove a node at the front of a list. p p You also need a technique for inserting or removing a node elsewhere Summary


Download ppt "Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,"

Similar presentations


Ads by Google