Linked List and Queue Flashcards
What is the List ADT?
The List ADT (Abstract Data Type) is a collection of data that supports a set of operations such as adding data to the list, removing elements from the list, and querying the list.
How can an array be used to implement the List ADT?
An array can be used to implement the List ADT by allocating a fixed-size array to store the data and using indexes to access the elements. However, arrays have limitations such as the need to shift elements when an element is removed from the middle of the list and the inability to dynamically adjust the size of the array.
What is a linked list?
A linked list is an alternative to an array for implementing the List ADT. It consists of nodes linked together where each node contains a reference to the rest of the chain and a value at the current link. This forms a chain with a direction
What are the advantages of using a linked list over an array for implementing the List ADT?
The advantages of using a linked list over an array for implementing the List ADT include:
Dynamic size: a linked list can grow and shrink as needed without having to copy elements to a new array.
Easy insertion and removal: adding or removing an element in a linked list only requires updating a few references, unlike an array where shifting elements can be time-consuming.
Flexibility: linked lists can have variable-length nodes with different types of data in each node, making them more flexible than arrays.
What are the disadvantages of using a linked list over an array for implementing the List ADT?
The disadvantages of using a linked list over an array for implementing the List ADT include:
Lack of random access: accessing an element in a linked list requires traversing the list from the beginning, whereas an array can access any element directly using an index.
Extra space: a linked list requires extra memory to store the references between nodes, whereas an array only requires memory for the data elements.
Slower traversal: traversing a linked list can be slower than accessing an array due to the overhead of following the references between nodes.
What is a linked list data structure?
A linked list is a data structure that consists of a sequence of nodes, each containing a reference to the next node in the sequence. It is an alternative to an array for storing and manipulating a collection of elements.
How is a linked list different from an array?
A linked list differs from an array in that the elements are not stored in contiguous memory locations. Instead, each element is stored in a separate node, which contains a reference to the next node in the list. This makes it easier to insert or remove elements in the middle of the list.
What are the advantages of using a linked list?
One advantage of using a linked list is that it allows for efficient insertion and deletion of elements in the middle of the list. Another advantage is that the size of the list can be easily changed by adding or removing nodes, without the need to allocate or deallocate a large block of memory.
What are the basic operations that can be performed on a linked list?
The basic operations that can be performed on a linked list are:
Adding an element to the beginning of the list (prepend)
Adding an element to the end of the list (append)
Removing an element from the beginning of the list (head removal)
Removing an element from the end of the list (tail removal)
Inserting an element at a specific position in the list
Removing an element from a specific position in the list
Searching for an element in the list
What is a node in a linked list?
A node in a linked list is a basic unit that stores an element of the list and a reference to the next node in the sequence. It typically contains two fields: a data field that stores the element and a next field that stores a reference to the next node in the list.
How is a linked list represented in memory?
A linked list is typically represented in memory using a series of nodes, each containing the data element and a reference to the next node in the sequence. The first node is called the head of the list, while the last node is called the tail. The tail node typically contains a null reference, indicating the end of the list.
How is navigation in a linked list different from navigation in an array?
Navigation in a linked list is entirely based on references, while navigation in an array is based on indexes.
In a linked list, the objects in the list only make sense in terms of relative position to each other, while in an array, the objects have a fixed position indicated by their index.
How are objects stored in a linked list?
Objects in a linked list are stored in nodes, which consist of a value and a reference to the next node in the list.
The first node in the list is called the head, and the last node is called the tail.
How is an element added to a linked list?
Adding an element to a linked list involves creating a new node with the value of the element and setting its reference to the next node in the list.
If the element is being added to the beginning of the list, the new node becomes the head of the list.
If the element is being added to the end of the list, the new node becomes the tail of the list.
How is an element removed from a linked list?
Removing an element from a linked list involves finding the node containing the element and adjusting the references of the surrounding nodes to bypass it.
If the element being removed is the head of the list, the next node becomes the new head.
If the element being removed is the tail of the list, the previous node becomes the new tail.
What are some advantages of using a linked list over an array?
Linked lists have a dynamic size, meaning that elements can be added or removed without needing to resize the entire data structure.
Adding or removing elements from a linked list is generally faster than in an array, especially for large data sets, as it only requires adjusting references rather than copying elements to new memory locations.
Linked lists allow for efficient insertion and removal of elements at any position in the list, while arrays are better suited for random access of elements.
How do we look up an element in a linked list?
We follow the chain from the head of the list until we find the desired element. This process is slower than an array for random access because we can’t jump directly to the desired index, but must traverse the list in order.
Why are linked lists slower for lookups in the general case?
Linked lists are slower for lookups in the general case because navigation in a linked list is entirely based on references, and indexes have less meaning. To find a specific element, we need to follow the chain from the head, which can be slower than accessing an element by its index in an array.
In what ways are linked lists better than arrays for inserts and deletes?
Linked lists are better than arrays for inserts and deletes because the position of an element in a linked list only depends on its neighbors, and the position is not absolute. This means that we can insert or delete an element from a linked list without affecting the rest of the list’s elements’ positions or having to shift them over, which can be expensive operations in an array.
How does the size of a linked list differ from the size of an array?
The size of a linked list is not absolute, unlike the size of an array. In an array, the size is determined by the number of elements it can hold, which is fixed. In contrast, the size of a linked list can vary dynamically as we add or remove elements from it.
What is the main advantage of a linked list over an array?
The main advantage of a linked list over an array is its flexibility in terms of inserts and deletes. Because the position of an element in a linked list only depends on its neighbors, we can add or remove elements from a linked list without affecting the rest of the list’s elements’ positions or having to shift them over, which can be expensive operations in an array.
What is the advantage of linked list over array when it comes to insertion?
The advantage of linked list over array when it comes to insertion is that once we have the position, insertion is low cost in linked list. In array implementation, when we want to insert an element at a specific position, we need to move all the elements after that position over by one index, which can be costly in terms of time and memory. In a linked list, we just need to update the reference pointers of the nodes to insert a new node at a specific position.
What is deletion in a linked list?
Deletion in a linked list is the removal of a node from the list.
Why is deletion in a linked list considered low cost?
Deletion in a linked list is considered low cost because once the position of the node to be deleted is found, the node can simply be removed by changing the references of its neighbors to bypass it.
What happens in an array implementation when the list gets small?
In an array implementation, when the list gets small and there are empty spaces in the array, deleting an element becomes expensive because all the remaining elements must be shifted over to fill the empty space, resulting in a high cost operation.
What are some advantages of linked lists over arrays?
Linked lists have cheap insertion and deletion in the general case, since we only need to modify a few references and not move all the elements over like in an array. Linked lists also store only what we need, avoiding any unused memory space.