MOD 3_ZYBOOKS 4_LISTS Flashcards
(100 cards)
What is a list?
A list is a common ADT for holding ordered data, having operations like append a data item, remove a data item, search whether a data item exists, and print the list.
For a given list item, after “Append 7”, “Append 9”, and “Append 5”, then “Print” will print…?
And “Search 8” would…?
Print (7, 9, 5) in that order
Would indicate item not found.
VIEW: Some common operations for a list ADT.
For lists, what do the following operations do?
1) Append(list, x)
2) Prepend(list, x)
3) InsertAfter(list, w, x)
4) Remove(list, x)
5) Search(list, x)
For lists, what do the following operations do?
1) Print(list)
2) PrintReverse(list)
3) Sort(list)
4) IsEmpty(list)
5) GetLength(list)
What is an array-based list?
An array-based list is a list ADT implemented using an array.
T/F: An array-based list implementation will dynamically allocate the array as needed as the number of elements changes.
True.
How does an array-based list allocate size initially?
Initially, the array-based list implementation allocates a fixed size array and uses a length variable to keep track of how many array elements are in use. The list starts with a default allocation size, greater than or equal to 1. A default size of 1 to 10 is common.
For an array-based list, what happens if an item is added when the allocation size equals the list length?
An array-based list must be resized if an item is added when the allocation size equals the list length. A new array is allocated with a length greater than the existing array. Allocating the new array with twice the current length is a common approach. The existing array elements are then copied to the new array, which becomes the list’s storage array.
Because all existing elements must be copied from 1 array to another, the resize operation has a runtime complexity of O(N).
What does the Prepend operation do for an array-based list?
The Prepend operation for an array-based list inserts a new item at the start of the list. First, if the allocation size equals the list length, the array is resized. Then all existing array elements are moved up by 1 position, and the new item is inserted at the list start, or index 0. Because all existing array elements must be moved up by 1, the prepend operation has a runtime complexity of O(N).
What does the InsertAfter operation do for an array-based list?
The InsertAfter operation for an array-based list inserts a new item after a specified index. Ex: If the contents of numbersList is: 5, 8, 2, ArrayListInsertAfter(numbersList, 1, 7) produces: 5, 8, 7, 2. First, if the allocation size equals the list length, the array is resized. Next, all elements in the array residing after the specified index are moved up by 1 position. Then, the new item is inserted at index (specified index + 1) in the list’s array. The InsertAfter operation has a best case runtime complexity of O(1) and a worst case runtime complexity of O(N).
What does the Search operation do for an array-based list?
Given a key, the search operation returns the index for the first element whose data matches that key, or -1 if not found.
Both the search and remove operations have a worst case runtime complexity of O(N).
What does the Remove-at operation do for an array-based list?
Given the index of an item in an array-based list, the remove-at operation removes the item at that index. When removing an item at index X, each item after index X is moved down by 1 position.
Both the search and remove operations have a worst case runtime complexity of O(N).
What syntax is used for a class’s destructor function?
How is it implemented?
The syntax for a class’s destructor function is similar to a class’s constructor function, but with a “~” (called a “tilde” character) prepended to the function name.
A destructor has no parameters and no return value. So the LinkedListNode and LinkedList class destructors are declared as ~LinkedListNode(); and ~LinkedList();, respectively.
The LinkedList class destructor is implemented to free each node in the list. The LinkedListNode destructor is not required, but is implemented below to display a message when a node’s destructor is called. Using delete to free a dynamically allocated LinkedListNode or LinkedList will call the object’s destructor.
C++’s vector type is implemented using an ____-based data structure
array
VIEW: ArrayList class: declaration of private members and constructor.
VIEW: Append() and Resize() functions.
For array-based lists (classes), what do Append() and Resize() do?
Append() is implemented by inserting the new item at the index equal to the array’s current length. If the array is filled all the way to the allocation size, then the array is doubled in size first.
Resize() works by first creating a new array of the indicated size and then copying all the items in the current array to the new array. The arrayData data member is then reassigned with the new array and the allocation size is updated.
For array-based lists (classes), what do the Prepend() and InsertAfter() functions do?
The Prepend() function shifts the entire contents of the array one index to the right. Shifting is accomplished by copying the last item to the next index, then copying the second-to-last item to the index previously occupied by the last index, and so on until the first item is copied to index 1.
To do the shifting, the loop must use the indices in reverse order, from the last item down to the first item. Ex: If a list’s length is 5, then the loop shifts by accessing indices 5, 4, 3, 2, 1, in that order. Since a new item is added to the list, the array is first checked to see if space is available, and resized if not.
The InsertAfter() function operates similarly to Prepend(). Items are shifted one space to the right, but only down to the provided index plus 1. All items from the given index down to 0 are not shifted, creating an empty space for the new item to be inserted into.
VIEW: Prepend() and InsertAfter() functions.
For array-based lists (classes), what do the Search() and RemoveAt() functions do?
The Search() function takes a target item as a parameter and then tests each item in the list, from index zero up to index arrayListLength - 1, for equality with the target item. The function returns the index where the first matching item is found, or -1 if no matching item exists in the array. The Search() function does not change the list’s length or the internal array’s allocation size.
RemoveAt() removes the item at the specified index by shifting the array items. The items from the parameter index plus 1 are shifted to the left by one index, up to the final item in the array. The RemoveAt() function decreases the list’s length (assuming the specified index is valid in the current list), but does not change the internal array’s allocation size.
VIEW: Search() and RemoveAt() functions.
What is a singly-linked list?
A singly-linked list is a data structure for implementing a list ADT, where each node has data and a pointer to the next node. The list structure typically has pointers to the list’s first node and last node.
A singly-linked list’s first node is called the ____, and the last node the ____.
Head, tail