CSE 220 Week 4
CSE 220 Week 4
Mushtari Sadia
Linked List
Different Types of Linked Lists
10 → 20 → 30 → 40
DH → 10 → 20 → 30 → 40
10 → 20 → 30 → 40
1
class Node:
def __init__(self, e, n, p):
self.elem = e
self.next = n prev elem next
self.prev = p
Linear vs Circular
Linear: 10 → 20 → 30 → 40
Circular:
10 → 20 → 30 → 40
Dummy-headed doubly circular linked list
DH⇄ 10 ⇄ 20 ⇄ 30 ⇄ 40
Dummy-headed doubly circular linked list
class DoublyNode:
self.elem = elem
Some of you know about the famous mathematician and computer scientist Alan Turing. He investigated and proved many important properties
about computers. One of his most fascinating contributions is the Turing Machine that can simulate any computer. By doing so, the Turing
machine allows us to investigate the fundamental limitations and capabilities of all computers ever being built. The machine is surprisingly
simple. It has an infinite tape of symbols and a read-write head that can make one-step forward or backward on that tape in each step or
change the content of the tape cell where the head is located at that moment. The read-write head, also called the finite control, keeps track of
its current state and decides what to do based-on the symbol on the tape in its position and its state.
prev = None
current = linked_list .head
# Traverse to the last node and keep track of the previous node
while current.next:
prev = current
current = current.next
intersection_list = LinkedList ()
current1 = list1.head
current2 = list2.head
return intersection_list
def get_last_node(head):
current = head
while current.next:
current = current.next
return current
def is_palindrome(head):
left = head
right = get_last_node (head)
return True
def reverse(head):
current = head
while current:
temp = current.prev
current.prev = current.next
current.next = temp
current = current.prev
head = temp.prev
return head
Next Class: Quiz on linked list
Wednesday 8:00 AM
Stack
Introduction
Basic Operations
● Push(obj): adds obj to the top of the stack ("overflow" error if the stack has fixed capacity,
and is full)
● Pop: removes and returns the item from the top of the stack ("underflow" error if the stack
is empty)
● Peek: returns the item that is on the top of the stack, but does not remove it ("underflow"
error if the stack is empty)
Stack
Fixed capacity - implemented with array Unlimited capacity - implemented with linked list
Fixed Capacity Stack Example
Stack of
Size 4
Push(1)
1
Push(2)
1
Push(3)
1
Push(4)
4
1
Push(5) Stack Overflow Error!
1
Peek
4
1
Pop
4
1
Pop
1
Pop
1
Pop
1
Pop Stack Underflow Error!
import numpy as np
class Stack:
def __init__(self):
self.stack = np.zeros(4, dtype=int) Code
Implementing The
self.top = -1
Push(1) top = 0
stack.push(1)
Code running
behind the scene:
Push(2) top = 1
stack.push(2)
Code running
behind the scene:
Push(3) top = 2
stack.push(3)
Code running
behind the scene:
3
Push(4) top = 3
stack.push(4)
4
Code running
behind the scene:
3
Push(5) top = 3
stack.push(5)
4
Stack Overflow Error!
Code running
behind the scene:
3
4 print(stack.peek())
4 3 Code running
behind the scene:
def peek(self):
Will return 4, but
2 if self.top == -1:
print(stack.pop())
4 3 Code running
behind the scene:
def pop(self):
stack.pop()
Code running
behind the scene:
def pop(self):
Will remove 3
2 if self.top == -1:
print("Stack underflow")
from the stack else:
item = self.stack[self.top]
1 self.stack[self.top] = None
self.top -= 1
return item
Pop top = 0 Driver code:
stack.pop()
Code running
behind the scene:
def pop(self):
stack.pop()
Code running
behind the scene:
def pop(self):
stack.pop()
Stack Underflow
Error! Code running
behind the scene:
def pop(self):
if self.top == -1:
print("Stack underflow")
else:
item = self.stack[self.top]
self.stack[self.top] = None
self.top -= 1
return item
Stack Applications
1. Call Stacks
def main:
A()
def A:
B()
def B:
print("hello world")
main()
Stack Applications Hello
2. Reverse a word
def reverse_word(word):
word = "Hello"
stack = Stack()
reversed_word = reverse_word(word)
print(reversed_word)
# Push each character of the word onto the stack
for char in word:
stack.push(char)
reversed_word = ""
# Pop each character from the stack to get the reversed word olleH
while not stack.is_empty():
reversed_word += stack.pop()
return reversed_word
Stack Applications
3. Undo