Linked lists memory view -...

12
Linked lists memory view lst = Linked_list("abc") name space memory space lst next value next “a” value next “b” value next “c” None type: Node type: Node type: Node type: linked_list

Transcript of Linked lists memory view -...

Page 1: Linked lists memory view - tau-cs1001-py.wdfiles.comtau-cs1001-py.wdfiles.com/local--files/recitation-logs-2017b/m_09... · Guidance value next value next value next 1.Think about

Linked lists memory view

lst = Linked_list("abc")

name space

memory space

lst

next

value

next

“a”

value

next

“b”

value

next

“c”

None

type: Node type: Node type: Node type:

linked_list

Page 2: Linked lists memory view - tau-cs1001-py.wdfiles.comtau-cs1001-py.wdfiles.com/local--files/recitation-logs-2017b/m_09... · Guidance value next value next value next 1.Think about

next

next

lst.reverse()

value

next

“a”

value

next

“b”

value

next

“c”

None

value

next

“a”

value

next

“b”

value

next

“c”

None

Exercise: implement list reverse with O(1) additional memory

Page 3: Linked lists memory view - tau-cs1001-py.wdfiles.comtau-cs1001-py.wdfiles.com/local--files/recitation-logs-2017b/m_09... · Guidance value next value next value next 1.Think about

Guidance

value

next

value

next

value

next

1. Think about “the middle” of the process.

You have 2 consecutive elements prev, curr.

All elements up to prev were reversed.

Write the loop’s body.

2. Think about the initialization of prev, curr

3. Think about termination of the loop and

extra actions after it

value

next

prev curr

Page 4: Linked lists memory view - tau-cs1001-py.wdfiles.comtau-cs1001-py.wdfiles.com/local--files/recitation-logs-2017b/m_09... · Guidance value next value next value next 1.Think about

next

Solution

value

next

“a”

value

next

“b”

value

next

“c”

None

def reverse(self):

prev = None

curr = self.next

while curr !=None:

tmp = curr.next

curr.next = prev

prev = curr

curr = tmp

self.next = prev

>>> lst = Linked_list("abcde")

>>> lst

[a,36067440] [b,36067376] [c,36067024] [d,36067312] [e,36066864]

>>> lst.reverse()

>>> lst

[e,36066864] [d,36067312] [c,36067024] [b,36067376] [a,36067440]

Page 5: Linked lists memory view - tau-cs1001-py.wdfiles.comtau-cs1001-py.wdfiles.com/local--files/recitation-logs-2017b/m_09... · Guidance value next value next value next 1.Think about

Demo

next

value

next

“a”

value

next

“b”

value

next

“c”

None

prev curr

Initialization

def reverse(self):

prev = None

curr = self.next

while curr !=None:

tmp = curr.next

curr.next = prev

prev = curr

curr = tmp

self.next = prev

None

Page 6: Linked lists memory view - tau-cs1001-py.wdfiles.comtau-cs1001-py.wdfiles.com/local--files/recitation-logs-2017b/m_09... · Guidance value next value next value next 1.Think about

Demo

next

value

next

“a”

value

next

“b”

value

next

“c”

None

prev curr

Step 1

def reverse(self):

prev = None

curr = self.next

while curr !=None:

tmp = curr.next

curr.next = prev

prev = curr

curr = tmp

self.next = prev

None

tmp

Page 7: Linked lists memory view - tau-cs1001-py.wdfiles.comtau-cs1001-py.wdfiles.com/local--files/recitation-logs-2017b/m_09... · Guidance value next value next value next 1.Think about

Demo

next

value

next

“a”

value

next

“b”

value

next

“c”

None

prev curr

Step 1

def reverse(self):

prev = None

curr = self.next

while curr !=None:

tmp = curr.next

curr.next = prev

prev = curr

curr = tmp

self.next = prev

None

tmp

Page 8: Linked lists memory view - tau-cs1001-py.wdfiles.comtau-cs1001-py.wdfiles.com/local--files/recitation-logs-2017b/m_09... · Guidance value next value next value next 1.Think about

Demo

next

value

next

“a”

value

next

“b”

value

next

“c”

None

prev curr

Step 1

def reverse(self):

prev = None

curr = self.next

while curr !=None:

tmp = curr.next

curr.next = prev

prev = curr

curr = tmp

self.next = prev

None

tmp

Page 9: Linked lists memory view - tau-cs1001-py.wdfiles.comtau-cs1001-py.wdfiles.com/local--files/recitation-logs-2017b/m_09... · Guidance value next value next value next 1.Think about

Demo

next

value

next

“a”

value

next

“b”

value

next

“c”

None

prev curr

Step 1

def reverse(self):

prev = None

curr = self.next

while curr !=None:

tmp = curr.next

curr.next = prev

prev = curr

curr = tmp

self.next = prev

None

tmp

Page 10: Linked lists memory view - tau-cs1001-py.wdfiles.comtau-cs1001-py.wdfiles.com/local--files/recitation-logs-2017b/m_09... · Guidance value next value next value next 1.Think about

Demo

next

value

next

“a”

value

next

“b”

value

next

“c”

None

prev curr

Step 2

def reverse(self):

prev = None

curr = self.next

while curr !=None:

tmp = curr.next

curr.next = prev

prev = curr

curr = tmp

self.next = prev

None

tmp

Page 11: Linked lists memory view - tau-cs1001-py.wdfiles.comtau-cs1001-py.wdfiles.com/local--files/recitation-logs-2017b/m_09... · Guidance value next value next value next 1.Think about

Demo

next

value

next

“a”

value

next

“b”

value

next

“c”

None

prev curr

Step 3

def reverse(self):

prev = None

curr = self.next

while curr !=None:

tmp = curr.next

curr.next = prev

prev = curr

curr = tmp

self.next = prev

None

tmp

Page 12: Linked lists memory view - tau-cs1001-py.wdfiles.comtau-cs1001-py.wdfiles.com/local--files/recitation-logs-2017b/m_09... · Guidance value next value next value next 1.Think about

Demo

next

value

next

“a”

value

next

“b”

value

next

“c”

None

prev curr

Termination

def reverse(self):

prev = None

curr = self.next

while curr !=None:

tmp = curr.next

curr.next = prev

prev = curr

curr = tmp

self.next = prev

None

tmp