CPSC 3200 Exam 1
provide pseudocode for the removeFirst() method of a singly linked list
if head == null then list is empty head = head.next size--;
provide pseudocode for the dequeue() method of a queue
if isEmpty() return null else o = Q[f] f = (f+1) mod N size -- return o
provide pseudocode for the removeLast() method of a doubly linked list
if size == 0 error v = trailer.getPrev() u = v.getPrev() trailer.setPrev(u) u.setNext(trailer) v.setPrev(null) v.setNext(null) size--;
provide pseudocode for the enqueue(o) method of a queue
if size == N throw Exception else r = (f + sz) mod N Q[r] = o size++
What distinguishes a doubly linked list from a singly linked list?
A doubly linked list has a prev field, a header and a trailer
provide pseudocode for the isEmpty() method of a queue
return (sz == 0)
provide pseudocode for the size() method of a queue
return sz
True or false: A singly linked list contains sentinel nodes
False
Why is removeLast() an inefficient method for singly linked lists?
It is inefficient because you have to walk through the list to reach the second to last node, which you then use to remove the tail.
What is the runtime of all queue methods?
O(1)
What is the runtime of all stack operations?
O(1)
What stack operations match to which singly linked list operations?
The top element of the stack is stored as the first node of the list. From there, addFirst() functions as push() and removeFirst() functions as pop()
True or false: A doubly linked list contains sentinel nodes
True
How do you reverse an array using a stack?
You push all elements in the array onto a stack and pop them off the stack and insert them back into the array in reverse order
provide pseudocode for the addFirst() method of a singly linked list
newest = Node(e) newest.next = head head = newest size++
provide pseudocode for the addLast() method of a singly linked list
newest = Node(e) newest.next = null tail.next = newest tail = newest size++;
provide pseudocode for the removeFirst() method of a doubly linked list
u = header.getNext() v = u.getNext() header.setNext(v) v.setPrev(header) u.setNext(null) u.setPrev(null) size--
provide pseudocode for the addLast() method of a doubly linked list
w = Node(e) u = trailer.getPrev() u.setNext(w) trailer.setPrev(w) w.setPrev(u) w.setNext(trailer) size++
provide pseudocode for the addFirst(v) method of a doubly linked list
w = header.getNext() v.setNext(w) v.setPrev(header) w.setPrev(v) header.setNext(v) size++