Stacks and Queues
- 84Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What is the main trade-off between C and higher-level languages such as Python?
Higher-level languages can let humans solve problems more quickly, but their programs may run more slowly than equivalent programs written in a lower-level language such as C.
Or swipe the card
84 cards
What is the main trade-off between C and higher-level languages such as Python?
Higher-level languages can let humans solve problems more quickly, but their programs may run more slowly than equivalent programs written in a lower-level language such as C.
What is an abstract data type?
A specification of the characteristics, operations, and functionality of a data structure independent of its implementation details.
How can the same abstract data type have different implementations?
It can be implemented using different concrete data structures, such as arrays or linked lists.
What is a concrete data structure?
A specific organization of data in computer memory used to implement an abstract data type.
What does a dictionary abstract data type represent?
A collection of key-value pairs that associates each key with a corresponding value.
How does a computer scientist generalize a word-definition dictionary?
The word is called the key, and the definition is called the value.
Why can different implementations of a dictionary have different performance?
Different implementations organize memory differently, which affects how quickly entries can be inserted, searched, or deleted.
What is a queue's defining fairness property?
FIFO: first in, first out.
What are the two fundamental operations on a queue?
Enqueue, written
enqueueornq, adds an item to the back; dequeue, writtendequeueordq, removes an item from the front.How does the clothing example illustrate a stack?
Clothes are placed on top of one another and the top item is removed first.
What is a stack's defining property?
LIFO: last in, first out.
What are the two fundamental operations on a stack?
Push adds an item to the top, and pop removes an item from the top.
Why are stacks of cafeteria trays a natural real-world stack?
The newest tray is placed on top, and people normally take the top tray first.
Why is a queue implemented with a fixed-size array limited?
It can hold only the number of elements specified when the array is allocated.
What is the difference between an array's capacity and a queue's size?
Capacity is the maximum number of items the allocated storage can hold; size is the number of items currently in the queue.
What trade-off results from choosing a large fixed capacity for a queue?
A large capacity reduces the risk of running out of space but may waste memory when few items are stored.
What additional information must an array-based queue track when items are removed?
It must track which item is currently at the front or head of the queue.
What is an array?
A contiguous chunk of memory containing values stored back-to-back.
Are C arrays zero-indexed?
Yes. The first element is at index 0, the second at index 1, and so on.
What is the main limitation of a statically allocated array?
Its size must be determined in advance, typically when the program is compiled.
Why cannot a program simply place a fourth array element immediately after a three-element array?
The memory immediately afterward may belong to another object or may not be part of the array's allocated region.
How can an array be resized manually in C?
Allocate a larger block, copy the old elements into it, add the new elements, free the old block, and update the pointer.
What does
mallocreturn?The address of the first byte of a newly allocated block of memory, or
NULLif allocation fails.How can
mallocallocate space for three integers without hard-coding the integer size?Use
malloc(3 * sizeof(int)).Why can dynamically allocated memory still be accessed with array notation?
A pointer to a contiguous allocated block can be indexed using pointer arithmetic, and C treats
list[i]equivalently to*(list + i).What is a memory leak?
Allocated memory that the program can no longer access or release because its address was lost.
Why is a temporary pointer useful when resizing dynamically allocated memory?
It preserves the address of the new allocation while the original pointer still identifies the old allocation.
What two practices should accompany every use of
malloc?Check whether it returned
NULL, and eventually callfreeon the allocated memory.What does
reallocdo?It attempts to resize an existing allocated memory block and returns a pointer to the resized block.
What is the first argument to
realloc?The pointer to the existing allocated memory block that should be resized.
Why should the result of
reallocbe stored in a temporary pointer first?If
reallocfails and returnsNULL, overwriting the original pointer would lose the address of the original block.What is a linked list?
A sequence of nodes connected by pointers, where the nodes need not be contiguous in memory.
What metadata does a basic singly linked-list node contain?
The data value and a pointer to the next node.
How does a linked list represent its end?
The final node's next pointer is set to
NULL.What pointer represents a linked list as a whole?
A pointer to the first node, commonly called
listor the head pointer.How is an empty linked list represented?
Its head pointer is set to
NULL.What is the purpose of metadata in a linked list?
Metadata stores information that helps organize or maintain the data, such as the address of the next node.
What does the C arrow operator
->mean when used with a structure pointer?It dereferences the pointer and accesses a field of the pointed-to structure.
Why must a self-referential node structure use a structure tag such as
struct node?The compiler must know the structure's type name before declaring a pointer to another node within the structure.
What does prepending mean in a linked list?
Inserting a new node at the beginning of the list.
How can a node be prepended without losing the existing list?
Set
new->nextto the current list head, then set the list head tonew.What is the running time of prepending to a linked list?
O(1).
What is the running time of appending to a linked list when no tail pointer is maintained?
O(n).
What is the running time of searching a linked list?
O(n) in the worst case.
What is the running time of traversing or printing every element of a linked list?
O(n).
What is the running time of deleting an arbitrary item from a linked list?
O(n) in the worst case.
Why does a linked list use more memory than an array storing the same values?
Each node requires extra metadata, such as one or more pointers, in addition to the stored data.
What is a doubly linked list?
A linked list in which each node contains pointers to both the next node and the previous node.
Why does a linked list not support binary search efficiently?
It cannot jump directly to the middle because its nodes are not contiguous; reaching the middle requires traversing from the beginning.
What is a tree data structure?
A hierarchical, multidimensional structure consisting of nodes connected through parent-child relationships.
What is the root of a tree?
The topmost node in the tree.
What is a leaf node?
A node with no children.
What property defines a binary search tree?
For each node, values in the left subtree are smaller and values in the right subtree are larger, assuming no duplicate values.
Why is a binary search tree described as recursive?
Each node's left and right subtrees are themselves binary search trees.
What information does a binary search tree node typically contain?
A data value, a pointer to its left child, and a pointer to its right child.
How does searching a binary search tree eliminate half of the search space?
At each node, comparison with the target determines whether to follow the left or right subtree, allowing the other subtree to be ignored.
What are the search, insertion, and deletion times in a balanced binary search tree?
They are approximately O(logn).
What happens to a binary search tree if values are inserted in an unfortunate sorted order?
It can become unbalanced and degenerate into a structure resembling a linked list.
What is the worst-case running time of operations on an unbalanced binary search tree?
O(n).
What is the trade-off of using a binary search tree instead of an array?
It gains dynamic growth and potentially logarithmic operations but uses more memory because each node stores two child pointers.
What are the base cases in recursive binary-search-tree search?
Return false if the current tree pointer is
NULL; otherwise return true if the current node contains the target.How does recursive binary-search-tree search proceed when the target is smaller than the current value?
It recursively searches the current node's left subtree.
How does recursive binary-search-tree search proceed when the target is larger than the current value?
It recursively searches the current node's right subtree.
What does hashing do?
It maps values from a potentially infinite domain to a finite range of bucket locations.
What is a hash function?
A function that takes an input key and produces the bucket or index where the key should be stored.
How could a simple hash function map English names to array indices?
Convert the first character to uppercase and subtract the character code for
A, producing a value from 0 through 25.What is a collision in a hash table?
A situation in which two or more keys map to the same bucket.
How can collisions be handled using an array of linked lists?
Store a linked list at each array bucket and place all keys that hash to that bucket in the corresponding list.
What is a hash table?
An array of buckets, often containing linked lists, used to associate keys with values.
Why can a hash table be faster than one linked list for searching?
Hashing jumps directly to a bucket, so the search examines only the linked list associated with that bucket rather than the entire collection.
Under uniform distribution, what is the approximate chain length in a hash table with k buckets and n keys?
rac{n}{k}.
Why is a hash table with a fixed number of buckets still technically O(n) in the worst case?
Collisions can place many keys in the same bucket, requiring a linear search through that bucket's linked list.
What is the trade-off of using a hash function based on more characters?
It can reduce collisions but requires more buckets and therefore more memory.
What is a trie?
A tree made from arrays, commonly used to store strings by following one pointer per character.
How does a trie represent a word such as
TOAD?It follows the pointer for
T, thenO, thenA, thenD, and marks the final node to indicate that a word ends there.What does a non-
NULLcharacter pointer mean in a trie?That the corresponding character continues to a child node in the stored path.
Why does a trie need a marker at the end of a word?
To distinguish a complete stored word from a prefix of a longer stored word.
How does a trie support words that share prefixes?
They reuse the same nodes for their shared initial characters and branch only when their characters differ.
What is the search time for a word in a trie?
O(1) with respect to the number of stored words, assuming a bounded maximum word length.
What is the major disadvantage of a trie?
It can use a very large amount of memory because each node may contain an array of many pointers, most of which may be
NULL.How can a trie store a key-value pair such as a name and phone number?
Use the character path for the key and store the associated value, such as the phone number, at the terminal node.
Why are hash tables often preferred over tries despite tries' constant-time lookup?
Hash tables usually achieve good lookup performance with substantially less memory.
What real-world system illustrates hashing into buckets?
A restaurant pickup shelf can hash customer names into alphabetical ranges and place each order in the corresponding bucket.
What can happen if one bucket in a real-world hash-based system receives too many items?
That bucket can overflow even when other buckets have unused capacity.

Ask anything
We've read this whole study set, so questions about it get the best answers.