Definition
A linked list is a linear collection of data elements, called nodes, each pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes which together represent a sequence.
Fig. 1. The illustration of a linked list with 3 elements.
Linked list is a simple and common data structure. They can be used to implement other data types, such as stacks, queues, associative arrays, S-expression.
Advantages
- Linked lists are a dynamic data structure, which can grow and be pruned while the program is running.
- Insertion and deletion node operations are easily implemented in a linked list
- Linear data structures such as stacks and queues are easily executed with a linked list
- They can reduce access time and may expand in real-time without memory overhead
Disadvantages
- They use more memory than arrays because of the storage used by their pointers
- Nodes in a linked list must be read in order from the beginning as linked lists are inherently sequential access.
- Nodes are stored incontiguously, greatly increasing the time required to access individual elements within the list, especially with a CPU cache
- Difficulties arise in linked lists when it comes to reverse traversing.
Fig. 2. The illustration of a doubly linked list.
Implementation of linked list
In this post, i will present how to implement a singly linked list in C programming language (structure, basic functions and examples).
First, we declare a struct representing a node of linked list:
First, we declare a struct representing a node of linked list:
1
2
3
4
5
6
| /* struct node represent an element of linked list */ struct node_s { int data; struct node_s* next_node; }; typedef struct node_s node_t; |
We have two global variables: the length of the linked list and the head node that store the address of the first node of the linked list.
1
2
| int list_len = 0; node_t *head = NULL; |
Now, we declare some basic function:
1
2
3
4
5
6
7
| /* function name : create_node * Description : create a new node based on passed integer value, return created node*/ node_t *create_node( int num) { node_t *node = (node_t*) malloc ( sizeof (node_t)); node->data = num; return node; } |
__________________________________________________________________________
1
2
3
4
5
6
7
8
| /* function name : is empty * Description : check the linked list is empty or not*/ int is_empty() { if (head == NULL) { return 1; } return 0; } |
___________________________________________________________________________
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| /* function name : get_node_at_index * Description : get a node at a index of the linked list*/ node_t *get_node_at_index( int index) { if (index < 0 || index > list_len - 1) { printf ( "Index must be in range of [0, %d].\n" , list_len - 1); return NULL; } else { int i; node_t *temp = head; for (i = 0; i < index; i++) { temp = temp->next_node; } return temp; } } |
___________________________________________________________________________
1
2
3
4
5
6
7
8
9
10
11
12
| /* function name : insert_at_begin * Description : insert a node at the beginning of the linked list and update linked list length*/ void insert_at_begin(node_t *node) { if (head == NULL) { head = node; } else { node->next_node = head->next_node; head->next_node = node; } list_len++; printf ( "Insert %d at begin!\n" , node->data); } |
____________________________________________________________________________
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| /* function name : insert_at_index * Description : insert a node at a index (in range of [0, list_len]) of the linked list and update linked list length */ void insert_at_index( int index, node_t *node) { if (index == 0) { /*exception at index 0 */ insert_at_begin(node); } else { node_t *node_of_index = get_node_at_index(index - 1); /* adj_node stores the index to be inserted*/ if (node_of_index != NULL) { node->next_node = node_of_index->next_node; node_of_index->next_node = node; printf ( "Insert %d at index %d!\n" , node->data, index); list_len++; } } } |
____________________________________________________________________________
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| /* function name : delete_at_index * Description : delete a node at a index (in range of [0, list_len - 1]) of the linked list and update linked list length */ void delete_at_index( int index) { node_t *deleted_node; if (index == 0) { /*exception at index 0 */ if (list_len == 0) { printf ( "Invalid index" ); } else { deleted_node = head; head = head->next_node; free (deleted_node); printf ( "Delete node at index %d\n" , index); list_len--; } } else { node_t *node_of_index = get_node_at_index(index - 1); if (node_of_index != NULL) { deleted_node = node_of_index->next_node; node_of_index->next_node = deleted_node->next_node; free (deleted_node); printf ( "Delete node at index %d\n" , index); list_len--; } } |
1
2
3
4
5
6
7
8
9
10
11
12
| /* function name : delete_all * Description : deleted all element in linked list */ void delete_all() { node_t *temp; while (!is_empty()) { temp = head; head = temp->next_node; free (temp); } list_len = 0; printf ( "delete all elements!\n" ); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| /* Function name : print_list * Description : print all elements of the linked list */ void print_list() { node_t *temp = head; printf ( "Linked list is: " ); if (is_empty()) { printf ( "empty" ); } else { while (temp != NULL) { printf ( "%d\t" , temp->data); temp = temp->next_node; } } printf ( "\n-------------------------------\n" ); } |
______________________________________________________________
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| int main( void ) { //head = (node_t*) malloc(sizeof(node_t)); int arr[5] = { 1, 3, 6, 4, 7 }; int i; print_list(); /* Insert at begin and print link list */ for (i = 0; i < 5; i++) { node_t *new_node = create_node(arr[i]); insert_at_begin(new_node); } print_list(); /*Insert at a index and print link list*/ node_t *new_node = create_node(10); insert_at_index(3, new_node); print_list(); /* Deleted node at a index and print link list*/ delete_at_index(2); print_list(); /* Deleted all */ delete_all(); print_list(); return EXIT_SUCCESS; } |
- Insert an integer array (1, 3, 6, 4, 7) in turn to the beginning of the linked list.
- Insert a node (data = 10) to index #3 of the linked list
- Delete node #2 of the linked list.
- Clear the linked list.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| Linked list is: empty ------------------------------- Insert 1 at begin! Insert 3 at begin! Insert 6 at begin! Insert 4 at begin! Insert 7 at begin! Linked list is: 1 7 4 6 3 ------------------------------- Insert 10 at index 3! Linked list is: 1 7 4 10 6 3 ------------------------------- delete 4 at index 2! Linked list is: 1 7 10 6 3 ------------------------------- delete all elements! Linked list is: empty ------------------------------- |
Example of the above linked list is available at here.
Reference:
https://en.wikipedia.org/wiki/Linked_list
http://www.tutorialspoint.com/data_structures_algorithms/linked_lists_algorithm.htm
Không có nhận xét nào:
Đăng nhận xét