HW: modify the stack class, such that when you reach the capacity, it increases in size.
time the pushing of elements.
start by inserting, say 50.
100
200
300
400
500
until 1000.
play around to find a good magnitude
based on this, push is O(what)?
________________________
how do i traverse the linked list?
for (Node current = head; current != null; current = current.next)
;// do something with the current node
function printFirstToLast(Node here):
if here = null:
return
else:
# do something
print here.element
printFirstToLast(here.next)
HW: write a recursive printLastToFirst(Node here):
write an iterative printLastToFirst(Node here):
If you can, figure out what the recursive and iterative implementations are O of.
How do we insert at the start?
function insertAtStart(value x):
n = new Node()
n.element = x
n.next = head
head = n
size++
function insertAtEnd(value x):
# first, create the new node
n = new Node()
n.element = x
n.next = null
# then, find the last node in the list
for(current = head; current.next != null; current = current.next)
; # do nothing
# now, current in the end of the list
current.next = n
size++
function insertAtEnd(value x): # this time, with a tail
# first, create the new node
n = new Node()
n.element = x
n.next = null
# then, add to the end of the list
tail.next = n
# then, update the tail
tail = n
size++
function insertBefore(element x, Node v):
# find the node immediately before v
for (curr=head; curr.next!=v; curr=curr.next)
; # do nothing
# create a node
n = new Node()
n.element = x
# put it in the list in proper place
# recall that curr in the node before v
curr.next = n
n.next = v
function insertBeforeValue(element x, value v):
for (curr=head; curr.element!=v; prev=curr, curr=curr.next):
; # do nothing
# now, curr is the node with element v in it
# new node
n = new Node()
n.element = x
#put n in its proper place
n.next = curr
prev.next = n