Linked Lists Flashcards

1
Q

is an arrays capacity fixed or variable?

A

fixed

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

When adding elements to a linked list and taking away from a linked list what must happen?

A

in order to add an element you move everything after the index you are adding to, to the right, and when taking away elements you must moved every element after the index you are removing from to the left to fill the gap.

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

what is the head of a linked list?

A

head stores the link to the first node

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

what is in the node in the linked list?

A

the element is in the node and the node also contains the link to the next stored load or contains the end of the list marker.

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

what are the pros to linked lists?

A

not fixed capacity
we allocate memory only when needed
the size of ta link list is only limited by the memory free in the heap

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

what are the cons of linked lists

A

The code to build a linked list is more complicated than the
code to declare or dynamically allocate an array
Required steps: allocate a node, store the payload in
the node, update the link to the next node
Must ensure that the last node’s link contains the end-
of-list marker

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

how do you create a linked list containing a single node

A

Step 1: Declare the node type
typedef struct node {
int data;
struct node *next;
} node_t;
Step 2: Declare the head (i.e., create a pointer to the head)
node_t *head;
Step 3: Allocate memory for the node (the node is stored in the heap)
head = malloc(sizeof(node_t));
assert(head != NULL);

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

what does it mean when the head of the list is equal to NULL

A

it means that the linked list has no nodes.

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

What is a linked list in computer science?

A

A linked list is a data structure that allows storing a collection of elements where the order of elements matters, and each element’s position is given by its rank or index.

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

How are elements stored in a linked list compared to arrays?

A

Elements in a linked list are not stored in contiguous memory like arrays. Each element in a linked list is grouped with a link to the next element, forming a chain-like structure.

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

What terminology is used to describe the structure of a linked list where each node is linked to one other node?

A

This type of linked list is referred to as a singly-linked list.

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

What are the pros of using linked lists?

A

Linked lists have dynamic memory allocation, allowing for a theoretically unlimited size. They are also flexible in terms of insertion and deletion operations compared to arrays.

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

What are some cons associated with linked lists?

A

Building a linked list requires more complex code compared to declaring or dynamically allocating an array. Additionally, accessing elements in a linked list is less efficient than in arrays since traversal is necessary.

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

How are nodes typically declared in C for implementing linked lists?

A

Nodes are typically declared using a struct, where each node contains a payload (data) and a pointer to the next node in the list.

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

What is the purpose of the assert function in C programming, especially concerning memory allocation?

A

The assert function is used to verify assumptions made during the program’s execution. It’s commonly used after memory allocation to ensure that memory allocation was successful before proceeding with further operations.

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

How can you create a linked list containing one node in C?

A

You can create a linked list containing one node by declaring a node type, allocating memory for the node, initializing its payload and link, and finally setting the head pointer to point to this node.

17
Q

How can you create an empty linked list in C?

A

An empty linked list is represented by a head pointer containing NULL, indicating that it doesn’t point to any node.

18
Q

what are the steps to creating a new node at the beginning of a linked list?

A
  1. initialize the content of the new element
  2. Point new node point to the old head
  3. point the head to the inserted node
19
Q

What is a pushing or push function?

A

function that inserts an integer at the front of a linked list

20
Q

What are the three steps involved in inserting a new node at the front of a linked list?

A

The three steps are: allocating a new node and initializing its payload, setting up the link of the new node to point to the current first node, and updating the head pointer to point to the new node.

21
Q

What is the purpose of the assert function in the context of allocating memory for a new node in a linked list?

A

The assert function is used to verify that memory allocation was successful before proceeding with further operations. It ensures that the pointer returned by malloc is not NULL, indicating successful allocation.

22
Q

How can you apply the 3-step link-in technique to insert a new node at the front of a linked list?

A

You can allocate a new node, set its next pointer to the current head of the list, and then update the head pointer to point to the new node.

23
Q

Why is it important to use memory diagrams when understanding or designing linked list code?

A

Memory diagrams help visualize the state changes that occur during the execution of linked list code, especially since linked lists involve dynamic memory allocation and deallocation.

24
Q

What is the correct approach to designing a new linked list function?

A

When designing a new linked list function, it’s important to draw “before and after” diagrams to visualize the state changes required, considering different cases such as an empty list, a list with one node, and a list with multiple nodes.

25
Q

What are some considerations when designing code for inserting an element at the front of a linked list?

A

: When designing code for inserting an element at the front of a linked list, it’s essential to consider cases such as an empty list, a list with one node, and a list with multiple nodes to ensure the algorithm works correctly in various scenarios

26
Q

What is the main difference between the wrongpush function and the push function?

A

The main difference is that the wrongpush function incorrectly attempts to update the head pointer within the function, while the push function correctly returns the pointer to the new node, leaving the responsibility of updating the head pointer to the caller.

27
Q

Why does the push function return a pointer to the new node instead of updating the head pointer directly?

A

The push function returns a pointer to the new node to ensure that the caller can update the head pointer correctly, especially in cases where the list is initially empty or when the head needs to be updated outside the function’s scope.

28
Q

What cases should be considered when designing code for inserting an element at the front of a linked list?

A

When designing code for inserting an element at the front of a linked list, cases such as an empty list, a list with one node, and a list with multiple nodes should be considered to ensure the algorithm works correctly in all scenarios.

29
Q

How does the push function handle the case of an empty linked list?

A

The push function correctly handles the case of an empty linked list by returning a pointer to the new node, effectively creating a linked list containing one node.

30
Q

How can you write a function that returns the length of a linked list?

A

To write a function that returns the length of a linked list, you can traverse the list while incrementing a counter for each node visited. If the list is empty, return 0; otherwise, return the count of nodes.

31
Q

What are the steps involved in traversing a linked list?

A

The steps involved in traversing a linked list include initializing a pointer to the first node, updating the pointer to point to the next node in each iteration until reaching the end of the list.

32
Q

How can you implement a function to calculate the length of a linked list in C?

A

You can implement a function to calculate the length of a linked list by initializing a count variable to 0, traversing the list while incrementing the count for each node, and returning the count once traversal is complete.

33
Q

What is the purpose of the assert function in the context of implementing a linked list in C?

A

The assert function is used to ensure that memory allocation was successful before proceeding with further operations. It helps catch errors early in the development process.

34
Q

How can you delete a node at the head of a linked list?

A

To delete a node at the head of a linked list, you reserve the address of the head to free it later, move the head to the second node, and then free the memory allocated to the node to be deleted

35
Q

How can you delete a node after a specific pointer in a linked list?

A

To delete a node after a specific pointer in a linked list, you reserve the address of the head, traverse through the list using a temporary pointer to reach the desired position, free the memory allocated to the node to be deleted, and connect the current node with the next node.

36
Q

What is the general form of many recursive linked-list functions?

A

The general form of many recursive linked-list functions includes handling base cases for an empty list and recursive cases where the function is called with the pointer to the rest of the list (head->next).

37
Q

How can you print a linked list in C?

A

To print a linked list in C, you can write a function that traverses the list while printing the payload of each node. If the list is empty, nothing is printed.

38
Q

How can you handle the case of an empty list when writing recursive linked-list functions?

A

When writing recursive linked-list functions, you handle the case of an empty list by checking if the head pointer is NULL. If it’s NULL, you handle the base case for an empty list; otherwise, you handle the recursive cases.

39
Q

What are some problems that can be solved using the recursive linked list pattern?

A

Some problems that can be solved using the recursive linked list pattern include calculating the sum or product of all numeric data, searching for a target value, counting the occurrences of a value, finding the smallest or largest value, and accessing the value stored in the i’th node.