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

Preview:

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

Citation preview

CS-2852Data StructuresLECTURE 5

Andrew J. Wozniewicz

Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

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

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

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!

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

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

CS-2852 Data Structures, Andrew J. Wozniewicz

Singly-Linked List Variation 1

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

head

payload next

tail

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

CS-2852 Data Structures, Andrew J. Wozniewicz

Tiger-By-The-Tail…

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

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?

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

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

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

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.

CS-2852 Data Structures, Andrew J. Wozniewicz

Singly-Linked List Class

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

payload next

3sizetailhead

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!

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!

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

Questions?

Image copyright © 2010 andyjphoto.com

Recommended