Top Banner
CS 2430 Day 35
24

CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Jan 03, 2016

Download

Documents

Lorena Baldwin
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 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

CS 2430

Day 35

Page 2: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Agenda

• Introduction to linked lists

• Bag as linked list

• Stack as linked list

Page 3: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Growing slowly

• Do you remember the grow() method?

• What is its big O complexity?

• O(N), where N is the number of elements in the Bag, Queue, etc.

• Therefore, add(), enqueue(), etc., had worst case big O of O(N)

Page 4: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Growing faster

• Can we make a growable Bag class with O(1) worst case add() method?

• Yes, we can!

• We cannot use an array

• We will use a “linked list”

Page 5: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Introduction to linked lists

Page 6: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Linked lists

• Arrays are random access, i.e., access to array element is O(1)

• Linked lists can have data scattered all over memory

• Therefore, linked lists are (mostly) sequential access

Page 7: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Nodes

• Each element of a linked list is a Node

• Nodes have “info” and “next” fields

• The “next” field points to the next node in the list

Page 8: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

The Node classclass Node

{

public Object info;

public Node next; // Note the type!

public Node(Object theInfo, Node theNext)

{

info = theInfo;

next = theNext;

}

}

Page 9: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

An empty list

Node list; // points to null. . .

►►

Ølist

Page 10: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Insert x at the back

list = new Node(x, null);. . .

What is the big O?O(1)

►►

xlist ØØ

Page 11: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Insert y at the back

list.next = new Node(y, null);. . .

►►

list x ØyØ

Page 12: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Insert z at the back

list.next.next = new Node(z, null);. . .

►►

list x Øy Øz

Page 13: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Delete from the front

list = list.next;. . .

What is the big O?O(1)

►►

list x y z Ø

Page 14: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Insert at front

list = new Node(w, list);. . .

What is the big O?O(1)

►►

list

w

y z Ø

Page 15: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Print everything out

Node p = list;while (p != null){ System.out.println(p.info); p = p.next;}

►►►►►►

list

p

x y z Ø

Page 16: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Always draw pictures when working linked list problems

Page 17: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Any questions?

Page 18: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Go to THE THING:

https://xray.ion.uwplatt.edu/summerss

Page 19: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Growable Bag as linked listpublic class Bag

{

private Node list;

public Bag() { . . . }

public boolean add(Object obj) { . . . } // insert at front

public boolean isEmpty() { . . . }

public int size() { . . . }

public void reset() { . . . }

public boolean contains(Object target) { . . . }

public boolean remove(Object obj) { . . . }

}

We will do some of the methods

Page 20: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

public class Bag

{

private Node list;

public boolean add(Object obj)

{

list = new Node(obj, list);

return true;

}

}

Page 21: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

public class Bag

{

private Node list;

. . .

public boolean isEmpty()

{

return list == null;

}

public void reset()

{

list = null;

}

}

Page 22: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

public class Bag

{

private Node list;

. . .

public int size()

{

int count = 0;

Node p = list;

while (p != null)

{

count++;

p = p.next;

}

return count;

}

}

What is the big O?

O(N)

I think we can do better than this!

Page 23: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

public class Bag

{

private Node list;

private int count = 0;

public void reset()

{

count = 0;

list = null;

}

. . .

}

Page 24: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

public class Bag

{

private Node list;

private int count;

. . .

public boolean add(Object obj)

{

list = new Node(obj, list);

count++;

return true;

}

public int size()

{

return count;

}

}

Now size(), add(), reset() and isEmpty() are all O(1)