COP3503 Programming Quiz 3 - Lists, Stacks, and Queues
What is an ADT?
Abstract Data Type, a class of objects whose logical behavior is defined by data and operations
Performance for an array-based list
Adding an element to the beginning: O(N) Removing an element from the beginning: O(N) Removing an element from the end: O(1) Adding an element to the end: O(1) Adding in the middle: O(N) Removing in the middle: O(N)
Benefits of a singly linked list (no tail)
Adding/Removing in front is easier, O(1)
Runtime error
An error that does not occur until the program has started to execute but that prevents the program from continuing.
Benefits of an array-based list
Constant access time, arr[i] = arr + (i*sizeOf(type))
true or false: doubly linked lists allow random access in the container in constant time
FALSE
true or false: linked lists are more efficient than arrays if you have to access elements with O(1) time.
FALSE
True or false: Doubly linked lists do not require more memory
False
Characteristics of a list implementation for an array
Finite size, stores similar elements, contiguous, allows for random access
Which of the following container(s) are list ADT implementations in C++?
Forward list, array, vector
For singly linked list, these operations are O(1)
Front(), isEmpty(), and AddAfter
Is stack[i] allowed?
NO. ABSOLUTELY NOT. There is NO RANDOM ACCESS (we access through top index)
What is big o of deleting an element from a doubly linked list w/ tail in the worst case in terms of big o notation?
O(n)
For doubly linked list with tail, these operations become O(1)
PopBack() and AddBefore(Node, data)
For singly linked list with a tail, these operations become O(1)
PushBack() and TopBack()
Performance of a singly linked list (w/ tail)
PushFront: O(1) PopFront: O(1), TopFront: O(1) Find: O(n) *PushBack: O(1) -PopBack: O(n) *TopBack: O(1) Erase: O(n) Empty: O(1) -AddBefore(Node, Key): O(n) AddAfter(Node, Key): O(1)
Performance of a singly linked list (no tail)
PushFront: O(1) PopFront: O(1), TopFront: O(1) Find: O(n) PushBack: O(n) PopBack: O(n) TopBack: O(n) Erase: O(n) Empty: O(1) AddBefore(Node, Key): O(n) AddAfter(Node, Key): O(1)
Characteristics of singly linked list (w/ tail)
Stores similar elements, elements are linked in memory but stored non-contiguously, doesn't allow for random access
Drawbacks of a singly linked list (no tail)
TopBack, PushBack, PopBack and AddBefore are expensive
How do we go through lists?
Using iterators for( list<int>::iterator it = myList.begin(); it != myList.end(); it++) {cout << *it << endl;} // accesses element iterator is pointing to
What is a data structure?
a specialized format for organizing and storing data ex. Lists, stacks, and queues
a SINGLE stack can be used to handle which of the following? A. undo feature B. Balancing parenthesis C. Task scheduling D. Evaluating expressions
a, b, d
PushBack(data)
adds a new node to the end of the linked list
WHICH OF THE FOLLOWING STATEMENTS ABOUT LINKED LISTS AND ARRAYS ARE TRUE? A) both data structures store elements sequentially (contiguously) in memory B) both data structures use iterators C) Both are linear data types D) Linked lists are more efficient if you need random access
b, c
Performance of array-based queue
enqueue(element): O(1) - dequeue(): O(1) - size(): O(1) - isEmpty(): O(1)
How do we solve the "rightward shift" problem?
implement a queue using a "circular array"
TopBack()
peeks at the node at the end of the linked list (if no tail, we need to traverse the whole list)
Characteristics of a stack implementation
push(element) - pop() - top() - size() - isEmpty()
Performance of array-based stack and list-based stack
push(element): O(1) - pop(): O(1) - top(): O(1) - size(): O(1) - isEmpty(): O(1)
Operations of a list ADT
read/write element, add/remove element, find element, count, traverse list
Characteristics of a singly linked list (no tail)
stored non-contiguously in memory, does not allow random access
Compiler error
syntax error
PopBack()
takes off the node at the end of the linked list