Review: programmer-defined data type that specifies a set of data values and a collection of well defined operations Review: A way to store and organize data in order to facilitate access and modification Definition Array: Contiguous array of memory Definition Array: Contiguous array of memory consisting of equal-size elements Definition Array: Contiguous array of memory consisting of equal-size elements indexed by contiguous integers. 1 2 3 4 5 6 7 8 Constant-time access array_addr + elem_size x (i – first_index) 1 2 3 4 5 6 7 8 (1,1) (3,4) (3,4) (3-1) x 6 (3,4) (3-1) x 6 + (4-1) (3,4) elem_size * ((3-1) x 6 + (4-1)) (3,4) array_addr + elem_size * ((3-1) x 6 + (4-1)) Add Remove Beginning End Middle 5 8 3 12 Add Remove Beginning End Middle 5 8 3 12 4 Add Remove Beginning End O(1) Middle 5 8 3 12 4 Add Remove Beginning End O(1) Middle 5 8 3 12 Add Remove Beginning End O(1) O(1) Middle 5 8 3 12 Add Remove Beginning End O(1) O(1) Middle 8 3 12 Add Remove Beginning End O(1) O(1) Middle 8 3 12 Add Remove Beginning End O(1) O(1) Middle 8 3 12 Add Beginning Remove O(n) End O(1) O(1) Middle 8 3 12 Add Remove Beginning O(n) O(n) End O(1) O(1) Middle 8 3 12 Add Remove Beginning O(n) O(n) End O(1) O(1) Middle O(n) 8 O(n) 3 12 Stores a set of elements in a particular order Stack principle: LAST IN FIRST OUT = LIFO It means: the last element inserted is the first one to be removed Example Which is the first element to pick up? A top B A top C B A top D C B A top E D C B A top D C B A top ▪push(key): inserts key to collection ▪Key pop(): removes and returns the last inserted element ▪top(): returns most recently added element without removing it ▪size(): returns the number of elements stored ▪isEmpty(): indicates whether no elements are stored ▪Delimiter matching () {} [] <> ▪Undo sequence in a text editor ▪Web browser history ▪A simple way of implementing the Stack ADT uses an array ▪We add elements from left to right … S 0 1 2 t ▪A variable keeps track of the index of the top element ▪The array storing the stack elements may become full … S 0 1 2 t numElements: 0 Push(a) numElements: 1 a Push(a) numElements: 1 a Push(b) numElements: 2 a b Push(b) numElements: 2 a b Top() → b numElements: 2 a b Push(c) numElements: 3 a b c Push(c) numElements: 3 a b c Pop() numElements: 2 a b Pop() → c numElements: 3 a b d Push(d) numElements: 4 a b d Push(e) e numElements: 5 a b d Push(f) e f numElements: 5 a b d e f Push(g) → error numElements: 5 a b d e f Empty() → False numElements: 4 a b d e Pop() → f numElements: 3 a b d Pop() → e numElements: 2 a b Pop() → d numElements: 1 a Pop() → b numElements: 0 Pop() → a numElements: 0 Empty() → True ▪ Stores a set of elements in a particular order in which Insertions and deletions follow the first-in first-out(FIFO) scheme ▪ Insertions are at the rear of the queue and removals are at the front of the queue ▪enqueue(object): inserts an element at the end of the queue ▪object dequeue(): removes and returns the element at the front of the queue ▪ object front(): returns the element at the front without removing it ▪ integer size(): returns the number of elements stored ▪ boolean isEmpty(): indicates whether no elements are stored ▪ Naïve way ▪ When enqueuing, the front index is always fixed and the rear index moves forward in the array. rear rear 3 3 front front Enqueue(3) 6 Enqueue(6) ▪ Naïve way ▪ When dequeuing, the element at the front the queue is removed. Move all the elements after it by one position. (Inefficient!!!) rear rear 3 3 front front Enqueue(3) 6 Enqueue(6) rear 6 front Dequeue() 0 0 read write Enqueue(a) 0 1 read write a Enqueue(b) 0 2 read write a b isEmpty() → False 0 3 read write a b c Enqueue(c) 1 3 read write b c Dequeue() → a 2 3 read write c Dequeue() → b 4 2 write read c d Enqueue(d) 0 2 write read c d e Enqueue(e) 1 2 write read f c d e Enqueue(f) 1 2 write read f c d e Enqueue(g) Error!!! 1 3 write read f d e Dequeue → c 1 4 write read f e Dequeue → d 1 0 write read f Dequeue → e 1 read 1 write isEmpty() → True ▪ Running times Running time Enqueue(obj) O(1) Dequeue() O(1) isEmpty() O(1) ▪ Waiting lists, bureaucracy ▪ Access to shared resources (e.g., printer) ▪ Multiprogramming ▪ Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException