Linked List and Queue Flashcards

1
Q

What is the List ADT?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can an array be used to implement the List ADT?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a linked list?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the advantages of using a linked list over an array for implementing the List ADT?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the disadvantages of using a linked list over an array for implementing the List ADT?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a linked list data structure?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How is a linked list different from an array?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the advantages of using a linked list?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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:

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a node in a linked list?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is a linked list represented in memory?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How is navigation in a linked list different from navigation in an array?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How are objects stored in a linked list?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How is an element added to a linked list?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How is an element removed from a linked list?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are some advantages of using a linked list over an array?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do we look up an element in a linked list?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Why are linked lists slower for lookups in the general case?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

In what ways are linked lists better than arrays for inserts and deletes?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How does the size of a linked list differ from the size of an array?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is the main advantage of a linked list over an array?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the advantage of linked list over array when it comes to insertion?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is deletion in a linked list?

A

Deletion in a linked list is the removal of a node from the list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Why is deletion in a linked list considered low cost?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What happens in an array implementation when the list gets small?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What are some advantages of linked lists over arrays?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What are some disadvantages of linked lists?

A

Linked lists have slower accesses in the general case, since we need to follow the chain of references to find a specific element. This can be less efficient than the direct access to elements in an array.

28
Q

What are some advantages of arrays over linked lists?

A

Arrays have fast accesses in the general case, since we can directly access elements by their index. Arrays can also sometimes require less storage than linked lists, since they don’t need extra references for the links.

29
Q

What are some disadvantages of arrays?

A

Arrays have slower insertion or deletion in the general case, since we need to move all the elements over to create or fill gaps. This can be less efficient than just modifying a few references like in a linked list. Arrays can also sometimes require more storage than we need, since they allocate a fixed-size block of memory regardless of the actual number of elements.

30
Q

What is a Queue ADT?

A

A Queue ADT (Abstract Data Type) is a collection of elements that supports adding elements to the back of the queue and removing elements from the front of the queue, following the “first in, first out” (FIFO) principle.

31
Q

What are some common use cases for a Queue?

A

Queues are commonly used in computer science and software development for tasks such as processing jobs, handling user requests, and managing network traffic.

32
Q

How is a Queue different from a Stack?

A

While both data structures involve adding and removing elements, a Queue operates on a “first in, first out” basis, whereas a Stack operates on a “last in, first out” (LIFO) basis.

33
Q

What are some common operations associated with a Queue ADT?

A

Common operations associated with a Queue ADT include enqueue (adding an element to the back of the queue), dequeue (removing an element from the front of the queue), peek (viewing the element at the front of the queue without removing it), and isEmpty (checking whether the queue is empty).

34
Q

How can a Queue be implemented?

A

A Queue can be implemented using an array or a linked list. In the case of an array implementation, a circular buffer can be used to allow for efficient use of memory and better performance. In the case of a linked list implementation, each node can contain a reference to the next node in the queue.

35
Q

What is the time complexity of enqueue and dequeue operations in a Queue implemented using an array?

A

The time complexity of enqueue and dequeue operations in an array implementation of a Queue is O(1), assuming a circular buffer is used. However, if the array becomes full, additional memory must be allocated and the existing elements must be copied over, resulting in a time complexity of O(n) in the worst case.

36
Q

What is the time complexity of enqueue and dequeue operations in a Queue implemented using a linked list?

A

The time complexity of enqueue and dequeue operations in a linked list implementation of a Queue is O(1), since adding or removing an element from the front or back of a linked list only involves updating a few pointers.

37
Q

What is the purpose of the isEmpty method in a Queue ADT?

A

The purpose of the isEmpty method in a Queue ADT is to check whether there are any elements in the queue or not. If there are no elements, the method returns true, otherwise it returns false.

38
Q

What does the isEmpty method return if there are no elements in the queue?

A

If there are no elements in the queue, the isEmpty method returns true.

39
Q

What does the isEmpty method return if there are elements in the queue?

A

If there are elements in the queue, the isEmpty method returns false.

40
Q

Why is the isEmpty method useful in a Queue ADT?

A

The isEmpty method is useful in a Queue ADT because it allows us to check whether the queue is empty or not before attempting to retrieve or remove an element. This can help avoid errors and ensure that the queue is being used correctly.

41
Q

How is the isEmpty method implemented in a Queue ADT?

A

The isEmpty method is typically implemented by checking whether the front and rear pointers in the queue are equal. If they are equal, then there are no elements in the queue and the method returns true. If they are not equal, then there are elements in the queue and the method returns false.

42
Q

What does the enqueue operation do in a queue data structure?

A

The enqueue operation adds an item to the back of the queue, also known as the tail of the queue.

43
Q

What is the purpose of the enqueue operation in a queue?

A

The purpose of the enqueue operation is to add an element to the end of the queue, so that it can be processed later in the order it was added.

44
Q

What happens when you enqueue an element to a queue that is already full?

A

If the queue is already full, attempting to enqueue a new element will result in an overflow condition, and the enqueue operation will fail.

45
Q

Is the enqueue operation fast or slow in a queue data structure?

A

The enqueue operation can be fast in a queue data structure, as it only involves adding an element to the end of the queue. However, the speed of the operation may depend on the implementation of the queue and whether or not there is available space in the queue.

46
Q

What does the dequeue operation do in a queue data structure?

A

The dequeue operation removes an item from the front of the queue, also known as the head of the queue.

47
Q

What is the purpose of the dequeue operation in a queue?

A

The purpose of the dequeue operation is to remove and retrieve the first element that was added to the queue, allowing the next element in the queue to become the new front.

48
Q

What happens when you dequeue an element from an empty queue?

A

If the queue is empty and you attempt to dequeue an element, it will result in an underflow condition, indicating that there are no elements in the queue to remove.

49
Q

Is the dequeue operation fast or slow in a queue data structure?

A

The dequeue operation can be fast in a queue data structure, especially if it is implemented using a linked list or a circular array. However, the speed of the operation may depend on the size of the queue and the implementation details.

50
Q

What is the purpose of the peek operation in a queue data structure?

A

The purpose of the peek operation is to retrieve the front element of the queue without removing it.

51
Q

How does the peek operation behave in a queue?

A

The peek operation returns the element at the front of the queue without modifying the queue’s contents. It allows you to inspect the next element that will be dequeued.

52
Q

What happens when you peek an element from an empty queue?

A

If the queue is empty and you attempt to peek an element, it will return a special value (such as null or an exception, depending on the programming language) to indicate that the queue is empty.

53
Q

Is the peek operation fast or slow in a queue data structure?

A

The peek operation is generally fast in a queue data structure because it only involves accessing the front element without any modifications. The time complexity of the peek operation is typically O(1) or constant time.

54
Q

What are the two common implementations of a Queue data structure?

A

The two common implementations of a Queue data structure are using an array and using a linked list.

55
Q

Why is a linked list a natural implementation for a Queue?

A

A linked list is a natural implementation for a Queue because it allows for efficient insertion and deletion at both ends of the list, which corresponds to enqueue and dequeue operations in a Queue. The dynamic nature of linked lists makes it easy to add elements to the back of the list and remove elements from the front.

56
Q

What are the advantages of using a linked list for implementing a Queue?

A

The advantages of using a linked list for implementing a Queue include efficient enqueue and dequeue operations, flexibility in adding or removing elements, and the ability to handle arbitrary number of elements without the need for resizing.

57
Q

Can an array be used to implement a Queue?

A

Yes, an array can be used to implement a Queue. However, using an array for a Queue requires additional considerations, such as managing the front and rear indices, and handling resizing when the array becomes full. The circular array implementation is commonly used to address these challenges.

58
Q

Which implementation, linked list or array, is typically considered the most natural choice for implementing a Queue?

A

The linked list implementation is generally considered the most natural choice for implementing a Queue due to its inherent properties that align with the enqueue and dequeue operations of a Queue.

59
Q

What are the main attributes of a Queue implemented with a linked list?

A

The main attributes of a Queue implemented with a linked list include the head (or front) pointer, the tail (or rear) pointer, and the size of the queue

60
Q

What is the purpose of the head pointer in a linked list implementation of a Queue?

A

The head pointer points to the first element (front) of the queue. It is used to keep track of the element that will be dequeued next.

61
Q

What is the purpose of the tail pointer in a linked list implementation of a Queue?

A

The tail pointer points to the last element (rear) of the queue. It is used to efficiently enqueue elements at the back of the queue

62
Q

How is the size of the queue determined in a linked list implementation?

A

The size of the queue is determined by maintaining a count variable that keeps track of the number of elements in the queue. It is incremented when an element is enqueued and decremented when an element is dequeued.

63
Q

What is the advantage of using a linked list for implementing a Queue’s attributes?

A

Using a linked list allows for dynamic allocation of memory, which means the Queue can grow or shrink as elements are enqueued or dequeued. This provides flexibility and efficient memory utilization compared to a fixed-size array implementation

64
Q

How does the linked list implementation handle enqueue and dequeue operations?

A

Enqueue operations involve adding a new element at the tail of the linked list, updating the tail pointer, and incrementing the size. Dequeue operations involve removing the element at the head of the linked list, updating the head pointer, and decrementing the size.

65
Q

What is the time complexity of enqueue and dequeue operations in a linked list implementation?

A

Enqueue and dequeue operations in a linked list implementation have a time complexity of O(1) (constant time) since they involve updating the head or tail pointer, which can be done in constant time regardless of the size of the queue.

66
Q

Can a linked list implementation of a Queue handle an arbitrary number of elements?

A

Yes, a linked list implementation can handle an arbitrary number of elements in a Queue. It can dynamically allocate memory for new elements as needed, allowing for efficient handling of any number of elements without the need for resizing or fixed capacity limitations.