Ch 6 - Stacks, Queues and Deques

Ace your homework & exams now with Quizwiz!

What are 8 deque methods?

1. add_first(e) add element e to the front of the deque 2. add_last(e) add element e to the back of the deque 3. delete_first( ) remove and return the first element from deque D; an error will occur if deque is empty 4. delete_last( ) remove and return the last element from deque D; an error will occur if deque is empty 5. first( ) Return but not remove the first element of deque; an error will occur if deque is empty 6. last( ) Return but not remove the last element of deque; an error will occur if deque is empty 7. is_empty( ) return true if deque D does not contain any elements 8. len(D) return the number of elements in deque

What is the equivalent of the following in a list class: 1. push() 2. pop() 3. top() 4. is_empty 5. len

1. append(e) 2. pop( ) 3. lst[-1] 4. len(lst) == 0 5. len(lst)

What is the time space complexity for the follow methods? : 1. enqueue 2. dequeue 3. first 4. is_empty 5. len(q)

1. enqueue * 2. dequeue * 3. first 4. is_empty 5. len(q) They are all O(1) * means amortized (The amortized analysis considers both the costly and less costly operations together over the whole sequence of operations.) are all O(1)

What are some methods for a queue?

1. enqueue(e) : Add element e to the back of queue 2. dequeue( ) : remove and return the first element from queue ; an error will occur if queue is empty 3. first( ): returns a reference to the element at the front of the queue without removing it an error will occur if queue is empty 4. is_empty( ) Return True if queue Q does not contain any elements 5. len(Q) return the number of elements in queue Q

time complexity of stack operations 1. push( ) 2. pop ( ) 3. top ( ) 4. is_empty( ) 5. len(S)

1. push - O(1) 2. pop - O(1) 3, top - O(1) 4. is_empty - O(1) 5. len( ) - O(1)

What are the top 5 methods for manipulating a stack?

1. push() Add element e to the top of the stack 2. pop() remove and return the top element from the stack S an error will occur if the stack is empty 3. top() return a reference to the top element of stack S , without removing it an error will occur if the stack is empty 4. is_empty() Return True if stack does not contain any element 5. len() return the number of element in stack, In python, we implement this with the special method __len__

What is a queue?

A collection of objects that are inserted and removed according to the First-In-First-Out Principle That is, elements can be inserted at any time but only the element that has been in the queue the longest can be next removed

What is a stack? And their characteristics and give 3 examples of stack.

A collection of objects that are inserted/removed according to LIFO (last-in, first-out) principle You can insert objects into stacks at any given time but may access or remove the most recently inserted object that remains at the top of the stack. Some examples of stacks are : 1. a pile of books, plates ... 2. Internet browers storage of urls 3. text editor undo functionality.

How do you implement a Stack?

See notebook

Implelement a queue based on Array

See notebook

What is a deque? Not dequeue

a queue-like data structure that supports insertion and deletion at both the front and back of the queue . This is used in restaurant reservation system where a customer was kicked off the queue because there were not enough tables , but needs to be added to the front once there was

What are some methods that are the same and different in the collections.deque library?

same len(d) -- number of elements add_first ( ) == .appendleft( ) -- add to the beginning add_last( ) == .append( ) -- add to the end delete_first( ) == popleft( ) -- remove from beginning delete_last( ) == pop( ) --remove from end first( ) == D[0] -- access first element last( ) == D[-1] ==access last element dlifferent D[j] --> access arbitrary entry by index D[j] = val --> modify arbitrary entry by index clear( ) --> clear all contents rotate(k) --> circularly shift rightward k steps remove(e) --> remove first matching element count(e) --> count number of matches for e


Related study sets

Building construction materials 2

View Set

Program Constructs: Iteration & Recursion

View Set

Троцкий Дм. "Я - не Я"

View Set

Chapter 3: Starting a Small Business

View Set