CS146
Chris Pollett
Apr 2, 2014
Let's look at some dynamic sets now...
STACK-EMPTY(S) 1 if S.top == 0 2 return true 3 else return false PUSH(S,x) 1 S.top = S.top + 1 2 S[S.top] = x POP(S) 1 if STACK-EMPTY(S) 2 error "underflow" 3 else S.top = S.top - 1 4 return S[S.top + 1]
ENQUEUE(Q, x) 1 Q[Q.tail] = x 2 if Q.tail == Q.length 3 Q.tail = 1 4 else Q.tail = Q.tail + 1 DEQUEUE(Q) 1 x = Q[Q.head] 2 if Q.head == Q.length 3 Q.head = 1 4 else Q.head = Q.head + 1 5 return x
LIST-SEARCH(L,k) //Theta(n) where n=L.length worst case 1 x = L.head 2 while x != NIL and x.key != k 3 x = x.next 4 return x LIST-INSERT(L,x) //Theta(1) 1 x.next = L.head 2 if L.head != NIL 3 L.head.prev = x 4 L.head = x 5 x.prev = NIL LIST-DELETE(L,x) //Theta(n) worst case 1 if x.prev != NIL 2 x.prev.next = x.next 3 else L.head = x.next 4 if x.next != NIL 5 x.next.prev = x.prev