cs-2852 data structures lecture 5 andrew j. wozniewicz image copyright 2010

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

Upload: martin-kelly

Post on 18-Jan-2018

221 views

Category:

Documents


0 download

DESCRIPTION

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”.

TRANSCRIPT

Page 1: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

CS-2852Data StructuresLECTURE 5

Andrew J. Wozniewicz

Image copyright © 2010 andyjphoto.com

Page 2: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

CS-2852 Data Structures, Andrew J. Wozniewicz

Agenda• Linked Lists Introduction– Singly-Linked List– Doubly-Linked List

Page 3: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

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”.

head

payload next

Page 4: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

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!

Page 5: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

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

Page 6: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

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.

head

payload nextprev

Page 7: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

CS-2852 Data Structures, Andrew J. Wozniewicz

Singly-Linked List Variation 1

• “head”, as before• “tail”, for ease of insertion

head

payload next

tail

Page 8: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

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”.

payload next

tailhead

payload next

tail

Page 9: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

CS-2852 Data Structures, Andrew J. Wozniewicz

Tiger-By-The-Tail…

http://www.flickr.com/photos/gavinbell/35378445/. Licensed under the Creative Commons license.

Page 10: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

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.

head

payload nextprev

tail

• “Tail”variable - in addition to the “head”.

• Why?

Page 11: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

CS-2852 Data Structures, Andrew J. Wozniewicz

Singly-Linked List Variation 2

• “head”, as before• “tail”, for ease of insertion

head

payload next

tail3

size

Page 12: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

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.

3sizehead

payload next

Page 13: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

CS-2852 Data Structures, Andrew J. Wozniewicz

List NodeA 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

Page 14: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

CS-2852 Data Structures, Andrew J. Wozniewicz

Node Classprivate static class Node<E> { private E data; private Node<E> next;

private Node(E data) { this.data = data; next = null; }

private Node(E data, Node<E> ref) { this.data = data; next = ref; }}

A nested (inner) class – does not reference its outer class.

Page 15: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

CS-2852 Data Structures, Andrew J. Wozniewicz

Singly-Linked List Class

• “head”, “tail”, and “size” are fields of this SinglyLinkedList class.

payload next

3sizetailhead

Page 16: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

CS-2852 Data Structures, Andrew J. Wozniewicz

SLL – Insert at the Head

public void addFirst(E value) { Node<E> newNode = new Node<E>(value); newNode.next = head; head = newNode;}

You Try It First!

Page 17: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

CS-2852 Data Structures, Andrew J. Wozniewicz

SLL – Insert at the Tailpublic void addLast(E value) {Node<E> newNode = new Node<E>(value);

Node<E> nodeRef = head;while (nodeRef != null)if (nodeRef.next != null)nodeRef = nodeRef.next;elsebreak;if (nodeRef == null)head = newNode;elsenodeRef.next = newNode;}

You Try It First!

Page 18: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

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

Page 19: CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright  2010

Questions?

Image copyright © 2010 andyjphoto.com