CSE 20 – Discrete Mathematics - Stanford...

Post on 02-Jun-2020

6 views 0 download

Transcript of CSE 20 – Discrete Mathematics - Stanford...

CS106X –Programming

Abstractions in C++

Dr. Cynthia Bailey Lee

Today’s Topics:

1. Linked structures

2. (if we have time) Heaps

2

What does this code do?front->next->next = new ListNode;

front->next->next->data = 40;

Before:

A. After:

B. After:

C. Using “next” that is NULL gives error

D. Other/none/more than one

data next

10

data next

20 NULLfront

data next

10

data next

40front

data next

20 NULL

data next

10

data next

20front

data next

40 NULL

Need to add this: front->next->next->next = NULL;

Lets clean this code up a bit Add LinkedNode constructors to help initialize new nodes

struct LinkedNode {

int data;

LinkedNode * next;

LinkedNode() {

data = 0;

next = NULL;

}

LinkedNode(int d, LinkedNode* n) {

data = d;

next = n;

}

};

Lets clean this code up a bit Make a LinkedList class (separate from LinkedNode) to

encapsulate list operations like generalized insert() and

remove() to insert and remove at given indicies

class LinkedList {

public:

LinkedList();

~LinkedList();

void insert(int data, unsigned int index);

void remove(unsigned int index);

int get(unsigned int index);

private:

LinkedNode * head;

}

Add to front

head

head

NULL

NULL

Add to the end

Special cases of add

head

NULL

Add to the middle

NULL

Lets clean this code up a bit

//PREcondition: assume index is valid

void LinkedList::insert(int data, unsigned int index){

if (index == 0){

head = new LinkedNode(data, head);

return;

}

LinkedNode * current = head;

unsigned int i=0;

for (int i=0; i<index-1; i++){//assumes index valid

current = current->next;

}

current->next = new LinkedNode(data, current->next);

}

Remove from the front

NULL

head

Special cases of remove

Remove in the middle

NULL

head

NULL

head Remove from the end

NULL

Suppose we have a pointer (current) to the node

containing the item to be removed.

What additional information do we need to

successfully remove the node?

A) Nothing additional.

B) A pointer to the node immediately prior to the to-be-deleted

node.

C) A pointer to the node immediately after the to-be-deleted

node.

D) Both B and C.

NULL

head current (we want to remove this one)

Remove

Suppose we have a pointer (current) to the node

containing the item to be removed.

NULL

head current (we want to remove this one)

NINJA CODER EDITION

Remove (bug)

void LinkedList::remove(unsigned int index){

if (index == 0){

head = head->next;

return;

}

LinkedNode * current = head;

unsigned int i=0;

for (i=0; i<index-1; i++){

current = current->next;

}

current->next = current->next->next;

}

Classic interview questions

How can you remove a node of a linked list

given a pointer to the node itself (and not the

previous one)?

Write code to reverse a linked list in place.

How can you detect if there is a loop in a

linked list (i.e. list is improperly formed), using

constant space (O(1) amount of space)?

Priority QueueEmergency Department waiting room operates as a

priority queue: patients are sorted according to

seriousness, NOT how long they have waited.

Some priority queue

implementation options

Unsorted linked list

Insert new element in front

Remove by searching list for highest-

priority item

Sorted linked list

Always insert new elements where they

go in priority-sorted order

Remove from front

Unsorted linked list

Add is FAST

Just throw it in the list

at the front

O(1)

Remove/peek is SLOW

Hard to find item the

highest priority item—

could be anywhere

O(N)

Priority queue implementations

This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license. Keyah Cheatum http://commons.wikimedia.org/wiki/File:Messy_Room.JPG

Sorted linked list

Add is SLOW

Need to step through the list to find where item goes in priority-sorted order

O(N)

Remove/peek is FAST

Easy to find item you are looking for (first in list)

O(1)

Priority queue implementations

We want the best of both

Fast add AND fast remove/peek

We will investigate trees as a way to get

the best of both worlds

Priority queue implementations