Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 14 Linked Lists CSE 1322 4/26/2018.

Similar presentations


Presentation on theme: "Lecture 14 Linked Lists CSE 1322 4/26/2018."— Presentation transcript:

1 Lecture 14 Linked Lists CSE 1322 4/26/2018

2 Linked Lists Dynamic data structures can grow and shrink at execution time. A linked list is a linear collection (i.e., a sequence) of nodes, connected by reference links (“chained together”). It can be singly or doubly linked A linked list is appropriate when the number of data elements is unpredictable. Linked lists become full only when the system has insufficient memory. 4/26/2018

3 Nodes A self-referential class contains a member that refers to an object of the same class type. In the class declaration Next references an object of type Node, an object of the same type being declared. Next is referred to as a link. class Node { public string data; public Node next ; } 4/26/2018

4 A Linked List A linked list can be thought of as a chain of linked nodes. A node is an object with two attributes: data the location of the next node in the chain A backslash indicates a null link. Not setting the link in the last node of a list to null is a logic error. 4/26/2018

5 Common tasks include: Inserting to the front Inserting to the end
Inserting in between the front/end Deleting Searching Sorting Each of these tasks takes different amounts of time depending upon how the linked list is implemented 4/26/2018

6 The insert Method The insert method performs the following steps: 1. Instantiate a new node containing the int to be inserted. 2. Attach that node at the beginning of the list, that is, make that node point to the previous head node. If the list originally was empty and the previous head node has the value null, then the next field of the new node is given the value null. 3. Indicate that the new node is now the head of the list, that is, make head point to the new node. 4/26/2018

7 The insert Method The original list: The new node is instantiated: attached to the beginning of the list: head now points to the new node: 4/26/2018

8 The delete Method To find the node to delete, we traverse the list. Traversing a list means to loop through the list, visiting each node in order. Three outcomes are possible: The item is found and is not the head of the list. The item is found and is the head of the list. The item is not found, and therefore, cannot be deleted. As we traverse the list, we maintain a reference to the previous node, because we will need to connect that node to the node following the deleted node. 4/26/2018

9 Deleting an Item That is Not the Head
The list before deleting the item with the value 8: The previous node is connected to the node after current: 4/26/2018

10 Deleting an Item That is the Head
The list before deleting the item whose value is 7: The link from the first node becomes the new head: 4/26/2018

11 Testing a Linked List Class
Be sure to test all possibilities. Insert: inserting into an empty list inserting into a nonempty list Delete: attempting to delete from an empty list deleting the first item in the list deleting the last item in the list deleting an item in the middle of the list. attempting to delete an item not in the list 4/26/2018

12 A linked list of ints -- the node and constructor
public class LinkListClass { private Node first; class Node { public int num; public Node next; } public LinkListClass() { first=null; } 4/26/2018

13 A linked list of ints -- add front
public void addNode(int n) { Node newNode = new Node(); newNode.num=n; newNode.next=first; first=newNode; } 4/26/2018

14 A linked list of ints -- add back
public void addBack(int n) { Node temp= new Node(); temp.num=n; Node current =first; while(current.next!=null) current=current.next; current.next=temp; } 4/26/2018

15 A linked list of ints -- remove
public void remove(int n) { Node curr=first; Node prev=curr; if(curr.num==(n)) { first=curr.next; return; } while(curr.num!=(n)) { prev=curr; curr=curr.next; if(curr==null) return; } prev.next=curr.next; } 4/26/2018

16 A linked list of ints -- display
public String display() { Node current= first; String data= ""; while(current.next!=null) { data+=" "+current.num+" --> " ; current=current.next; } data+=" "+current.num+" --> " ; return data; } 4/26/2018

17 A linked list of ints -- display
public String display() { Node current= first; String data= ""; while(current.next!=null) { data+=" "+current.num+" --> " ; current=current.next; } data+=" "+current.num+" --> " ; return data; } 4/26/2018

18 Java Client LinkListClass LL = new LinkListClass(); System.out.println(LL); LL.addNode(5); LL.addNode(7); LL.addNode(1); LL.addBack(4); System.out.println("display1 "+ LL+ " "+LL.display()); LL.remove(5); System.out.println("display2 "+ LL+ " "+LL.display()); LL.remove(9); System.out.println("display3 "+ LL+ " "+LL.display()); LL.addNode(8); System.out.println("display4 "+ LL+ " "+LL.display()); LL.addNode(3); System.out.println("display5 "+ LL+ " "+LL.display()); 4/26/2018

19 C# Client LinkListClass LL = new LinkListClass(); Console.WriteLine(LL); LL.addNode(5); LL.addNode(7); LL.addNode(1); LL.addBack(4); Console.WriteLine("display1 " + LL.display()); LL.remove(5); Console.WriteLine("display2 " + LL.display()); LL.remove(9); Console.WriteLine("display3 " + LL.display()); LL.addNode(8); Console.WriteLine("display4 " + LL.display()); LL.addNode(3); Console.WriteLine("display5 " + LL.display()); 4/26/2018


Download ppt "Lecture 14 Linked Lists CSE 1322 4/26/2018."

Similar presentations


Ads by Google