Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 206 Introduction to Computer Science II 09 / 15 / 2008 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 206 Introduction to Computer Science II 09 / 15 / 2008 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 206 Introduction to Computer Science II 09 / 15 / 2008 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Today’s Topics Questions? Comments? Linked lists

3 A linked list is a data structure where every node contains data and reference(s) to other node(s.)‏ Let's look at drawings on the board. Let's figure out how to – Add to beginning of linked list – Add to end of linked list Need to find last node – Insert after a particular node – Delete a particular node Other options – Storing link to last node – Doubly linked lists (and their operations.)‏ – Circular linked lists (where the “last” node refers to the “head”).

4 Linked lists A doubly linked list is a data structure where every node contains – data – a reference to previous node – a reference to next node The value of the next node for the last element of a linked list is null. The value of the previous node for the first element of a linked list is also null.

5 Linked lists In a singly linked list every node contains data and one reference to another node. –e.g. class Node { public AnyType data; // can have more data than one public Node next; } A Linked List is maintained by keeping a reference to the head of the list. From the head, we are able to get at all the other nodes in the list by following the next reference.

6 Linked lists class Node { public AnyType data; // can have more data than one public Node next; // when constructing a new node, we set its data and // set the next reference to null public Node(AnyType d)‏ { data = d; next = null; }

7 Linked lists // All the code on the rest of the slides is outside of the Node class. Node n = new Node(somedata); Node head; head = n; // sets n to be the head of the linked list Node newnode = new Node(somedata2); // to add a newnode to the beginning of the list: newnode.next = head; head = newnode;

8 Linked lists Node newnode = new Node(somedata2); // to add a newnode to the end of the list: Node currnode; currnode = head; while (currnode != null)‏ { savenode = currnode; currnode = currnode.next; } savenode.next = newnode;

9 Linked lists Insert after a particular node // insert newnode after findnode // head currnode = head; while (!((currnode.data).equals(findnode.data)))‏ { currnode = currnode.next; } newnode.next = currnode.next; currnode.next = newnode;

10 Linked lists // delete findnode // head Node prevnode; Node currnode = head; while (!((currnode.data).equals(findnode.data)))‏ { prevnode = currnode; currnode = currnode.next; } prevnode.next = currnode.next; currnode = null; // the data originally refered-to to be garbage collected // if we didn't have currnode = null; here, it would get garbage // collected once the currnode went out of scope.

11 Linked lists What's deficient in singly linked lists?

12 Linked lists What's deficient in singly linked lists? –can't add to the end of the list efficiently (add tail)‏ what gets easier? How to do it; what needs to change? –can't “back up” if necessary, so we can't say, traverse the linked list from the tail to the head because we only have a next (one way)‏ create a doubly linked list = add links that go both ways (next and prev)‏

13 Linked lists Let's consider operations for a doubly linked list on the board. A doubly linked list is a data structure where every node has – data – a reference to previous node (prev)‏ – a reference to next node (next)‏ We maintain a head node and a tail node. The value of the next node for the last (tail) element of a linked list is null. The value of the previous node for the first (head) element of a linked list is also null. When you construct a new node, set the data and next and prev default to null


Download ppt "CS 206 Introduction to Computer Science II 09 / 15 / 2008 Instructor: Michael Eckmann."

Similar presentations


Ads by Google