Download presentation
Presentation is loading. Please wait.
1
CS 206 Introduction to Computer Science II 02 / 06 / 2009 Instructor: Michael Eckmann
2
Michael Eckmann - Skidmore College - CS 206 - Spring 2009 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 (tail) – 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 referred-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 (chap. 4) What's deficient in singly linked lists?
12
Linked lists (chap. 4) 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'll also 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 construct a new node, set the data and next and prev default to null
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.