+ All Categories
Home > Documents > CSE 20 – Discrete Mathematics - Stanford...

CSE 20 – Discrete Mathematics - Stanford...

Date post: 02-Jun-2020
Category:
Upload: others
View: 6 times
Download: 0 times
Share this document with a friend
17
CS106X – Programming Abstractions in C++ Dr. Cynthia Bailey Lee
Transcript
Page 1: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

CS106X –Programming

Abstractions in C++

Dr. Cynthia Bailey Lee

Page 2: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

Today’s Topics:

1. Linked structures

2. (if we have time) Heaps

2

Page 3: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

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;

Page 4: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

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;

}

};

Page 5: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

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;

}

Page 6: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

Add to front

head

head

NULL

NULL

Add to the end

Special cases of add

head

NULL

Add to the middle

NULL

Page 7: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

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);

}

Page 8: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

Remove from the front

NULL

head

Special cases of remove

Remove in the middle

NULL

head

NULL

head Remove from the end

NULL

Page 9: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

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

Page 10: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

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

Page 11: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

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;

}

Page 12: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

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)?

Page 13: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

Priority QueueEmergency Department waiting room operates as a

priority queue: patients are sorted according to

seriousness, NOT how long they have waited.

Page 14: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

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

Page 15: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

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

Page 16: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

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

Page 17: CSE 20 – Discrete Mathematics - Stanford Universitystanford.edu/class/archive/cs/cs106x/cs106x.1142/lectures/15-linkedlist.pdfUnsorted linked list Add is FAST Just throw it in the

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


Recommended