Linked List
Linked lists are the best and simplest example of a dynamic data structure that uses pointers for its implementation. However, understanding pointers is crucial to understanding how linked lists work. We must also be familiar with dynamic memory allocation and structures.
Essentially, linked lists function as an array that can grow and shrink as needed, from any point in the array.
Linked lists have a few advantages over arrays:
- Items can be added or removed from the middle of the list
- There is no need to define an initial size
However, linked lists also have a few disadvantages:
- There is no "random" access - it is impossible to reach the nth item in the array without first iterating over all items up until that item. This means we have to start from the beginning of the list and count how many times we advance in the list until we get to the desired item.
- Dynamic memory allocation and pointers are required, which complicates the code and increases the risk of memory leaks and segment faults.
- Linked lists have a much larger overhead over arrays, since linked list items are dynamically allocated (which is less efficient in memory usage) and each item in the list also must store an additional pointer.
What is a linked list?
A linked list is a set of dynamically allocated nodes, arranged in such a way that each node contains one value and one pointer. The pointer always points to the next member of the list. If the pointer is NULL, then it is the last node in the list.
A linked list is held using a local pointer variable which points to the first item of the list. If that pointer is also NULL, then the list is considered to be empty.
Data | Next -> Data | Next
Sample Code:
typedef struct node
{
int val;
struct node * next;
} node_t;
Examples of Linked List using C Programming Language
1. Single Linked List
2. Doubly Linked List
3. Circular Linked List
4. Single Linked List with Header Node
5. Sorted Linked List
6. Concatenate Two Single Linked List
7. Merging Two Sorted Linked Lists
8. Linked List and Recursion
9. Concatenate Two Circular Linked Lists
10. Stack using Linked List
11. Queue using Linked List
12. Queue using Circular Linked List
14. Priority Queue using Linked List
15. Polynomial Addition using Linked List
16. Polynomial Subtraction using Linked List
17. Polynomial Multiplication using Linked List
18. Polynomial Addition and Multiplication using Linked List
0 Comments