Linked Lists Flashcards
is an arrays capacity fixed or variable?
fixed
When adding elements to a linked list and taking away from a linked list what must happen?
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.
what is the head of a linked list?
head stores the link to the first node
what is in the node in the linked list?
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.
what are the pros to linked lists?
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
what are the cons of linked lists
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 do you create a linked list containing a single node
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);
what does it mean when the head of the list is equal to NULL
it means that the linked list has no nodes.
What is a linked list in computer science?
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 are elements stored in a linked list compared to arrays?
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.
What terminology is used to describe the structure of a linked list where each node is linked to one other node?
This type of linked list is referred to as a singly-linked list.
What are the pros of using linked lists?
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.
What are some cons associated with linked lists?
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 are nodes typically declared in C for implementing linked lists?
Nodes are typically declared using a struct, where each node contains a payload (data) and a pointer to the next node in the list.
What is the purpose of the assert function in C programming, especially concerning memory allocation?
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.