Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

Similar presentations


Presentation on theme: "CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com."— Presentation transcript:

1 CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

2 CS-2852 Data Structures, Andrew J. Wozniewicz Agenda Linked Lists Introduction – Singly-Linked List – Doubly-Linked List

3 CS-2852 Data Structures, Andrew J. Wozniewicz Singly-Linked List Consists of “nodes”. Each node is linked to the next node in the list via the “next”, or “link”, etc. reference. Each node contains data “payload”.

4 CS-2852 Data Structures, Andrew J. Wozniewicz Singly Linked List – Class Exercise repeat until name of 1 person obtained – Ask someone for a name If the name given… – If there is no way to traverse the chain of people back to you… » Remember the name and the person (point your finger at them at all times) – If asked for a name… If you have given your name to someone already: refuse Else, give your name!

5 CS-2852 Data Structures, Andrew J. Wozniewicz Singly-Linked List: Operations Insert element at the head Add element at the end Insert element in the middle Delete element Find an element Get an element at n-th position Clear the list

6 CS-2852 Data Structures, Andrew J. Wozniewicz Doubly-Linked List Consists of “nodes”. Each node is linked to the next node in the list via the “next”, or “link”, or … reference. Each node is linked to th previous one via the “prev” reference.

7 CS-2852 Data Structures, Andrew J. Wozniewicz Singly-Linked List Variation 1 “head”, as before “tail”, for ease of insertion

8 CS-2852 Data Structures, Andrew J. Wozniewicz Singly L.L. Variation 1A – Valid? Something must hold on to the head of the list, or else there is no way to access most of the elements! Must have a “head”.

9 CS-2852 Data Structures, Andrew J. Wozniewicz Tiger-By-The-Tail… http://www.flickr.com/photos/gavinbell/35378445/. Licensed under the Creative Commons license.

10 CS-2852 Data Structures, Andrew J. Wozniewicz Dbl.L.L. Variation 1 “Tail”variable - in addition to the “head”. Why? To make additions at the end easier. “Tail”variable - in addition to the “head”. Why?

11 CS-2852 Data Structures, Andrew J. Wozniewicz Singly-Linked List Variation 2 “head”, as before “tail”, for ease of insertion

12 CS-2852 Data Structures, Andrew J. Wozniewicz SLL Variation 3 - Circular “head” – “tail” does not make much sense here… Could have an arbitrary number of variables pointing at different elements in the list, for ease of access.

13 CS-2852 Data Structures, Andrew J. Wozniewicz List Node A node is a data structure that consists of a data item (“payload”) and one or more links, where a link is a reference to a node. DEFINITION

14 CS-2852 Data Structures, Andrew J. Wozniewicz Node Class private static class Node { private E data; private Node next; private Node(E data) { this.data = data; next = null; } private Node(E data, Node ref) { this.data = data; next = ref; } A nested (inner) class – does not reference its outer class.

15 CS-2852 Data Structures, Andrew J. Wozniewicz Singly-Linked List Class “head”, “tail”, and “size” are fields of this SinglyLinkedList class.

16 CS-2852 Data Structures, Andrew J. Wozniewicz SLL – Insert at the Head public void addFirst(E value) { Node newNode = new Node (value); newNode.next = head; head = newNode; } You Try It First!

17 CS-2852 Data Structures, Andrew J. Wozniewicz SLL – Insert at the Tail public void addLast(E value) { Node newNode = new Node (value); Node nodeRef = head; while (nodeRef != null) if (nodeRef.next != null) nodeRef = nodeRef.next; else break; if (nodeRef == null) head = newNode; else nodeRef.next = newNode; } You Try It First!

18 CS-2852 Data Structures, Andrew J. Wozniewicz Summary Linked Lists Nodes – Payload – Links Singly-Linked List – Adding elements – Removing elements – Traversing Think about time-efficiency of operations! Doubly-Linked List

19 Questions? Image copyright © 2010 andyjphoto.com


Download ppt "CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com."

Similar presentations


Ads by Google