Top Banner
CS 307 Fundamentals of Computer Scienc e 1 Linked Lists many slides taken from Mike Scott, UT Austin
21

CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

1

Linked Lists

many slides taken from Mike Scott, UT Austin

Page 2: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

2

Recursive Data Structures Linked Lists are dynamic data structures

– They grow and shrink one element at a time, normally without some of the inefficiencies of arrays

Big O of Array Manipulations– Access the kth element– Add or delete an element in the middle of the array while maintaining

relative order– adding element at the end of array? space avail? no space avail? – add element at beginning of an array

If accesses are all at beginning or end of list, a linked structure offers improvements

Linked Lists could be used as the underlying storage container for higher level ADTs (stack, queue)

Page 3: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

3

Nodes and Lists A different way of implementing a list Each element of a Linked List is a separate

Node object. Each Node tracks a single piece of data plus

a reference (pointer) to the next Create a new Node every time we add

something to the List Remove nodes when item removed from list

and allow garbage collector to reclaim that memory

Page 4: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

4

Elements of Linked Listspublic class Node{ private Object myData;

private Node myNext;

public Node(){ myData = null; myNext = null; }

public Node(Object data, Node next){ myData = data; myNext = next; }

public Object getData(){ return myData; }

public Node getNext(){ return myNext; }

public void setData(Object data){ myData = data; }

public void setNext(Node next){ myNext = next; }

}

Page 5: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

5

One Implementation of a Linked List The Nodes shown on the previous slide are

singly linked– a node refers only to the next node in the

structure– it is also possible to have doubly linked nodes. – The node has a reference to the next node in the

structure and the previous node in the structure as well

How is the end of the list indicated– myNext = null for last node– a separate dummy node class / object

Page 6: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

6

A Simple List Interfacepublic interface IList{

void add(Object item);

void add(Object item, int pos);

Object set(Object item, int pos);

void add(List other);

Object get(int pos);

Object remove(int pos);

int size();}

Page 7: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

7

A Linked List Implementationpublic class LinkedList implements Ilist{ private Node myHead;

private Node myTail;private int iMySize;

public LinkedList(){ myHead = null;

myTail = null;iMySize = 0;

}}LinkedList list = new LinkedList();

LinkedList

myHead iMySize

myTail

null

null

0

Page 8: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

8

Add Element - List Empty (Before)

myHead myTail iMySize

null null 0

Object

item

Page 9: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

9

Add Element - List Empty (After)

myHead myTail iMySize

1

ObjectNode

myData myNext

null

Page 10: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

10

Add Element - List Not Empty (Before)

1

Object

Node

myData myNext

null

myHead myTail iMySize

Objectitem

Page 11: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

11

Add Element - List Not Empty (After)

2

Object

Node

myData myNext

myHead myTail iMySize

Object

Node

myData myNext

null

Page 12: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

12

Code for default add public void add(Object item)

Page 13: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

13

Code for arbitrary add public void add(Object item, int pos)

Page 14: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

14

Code for get public Object get(int pos)

Page 15: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

15

Code for remove public Object remove(int pos)

Page 16: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

16

Why Linked List Are any operations with a Linked List faster

than the version from ArrayList?

What about this: public void addFront(Object item)

Big O? Array version Big O? Fast performance for removeFront as well?

Page 17: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

17

Remove Back Method public Object removeBack()

Big O?

Page 18: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

18

Other Possible Features of Linked Lists

Doubly Linked

Circular Dummy Nodes for first and last node in list

public class DLNode{ private Object myData;

private DLNode myNext;private DLNode myPrevious;

}

Page 19: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

19

Default add for Doubly Linked List public void add(Object item)

Page 20: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

20

Code for arbitrary add - Doubly Linked List

public void remove(Object item, int pos)

Page 21: CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

CS 307 Fundamentals of Computer Science

21

Code for removeDoubly Linked List

public Object remove(int pos)