Lugsole.net long logo

09 01 02 Linked List

Author: Lugsole

Linked List

A linked list contains a few diferent parts. The first is the item. The second would be the a pointer to the next item.

Advantage

This data structure allows for any number of items be able to be added to a collection without copying a bun ch of data like would be the case in a vector.

Features

  • Get item
  • Insert item
  • Delete item
  • Length
  • Search
  • Get next item
  • Get last item
  • Last item in list
  • First item in list

Code

 1struct linked_node {
 2	void *data;
 3	struct linked_node *next;
 4	struct linked_node *previous;
 5}
 6
 7/** @brief This gets the data from the linked list item
 8 * 
 9 * 
10 * @param node The linked list node
11 * @return Address to the item at the linked list node
12 */
13void * linked_data(struct linked_node *node) {
14	return node->data;
15}
16
17/** @brief This gets the next node in the linked list
18 * 
19 * 
20 * @param node The linked list node
21 * @return Address to the next linked list item
22 */
23struct linked_node * linked_next(struct linked_node *node) {
24	return node->next;
25}
26
27/** @brief This gets the next node in the linked list
28 * 
29 * 
30 * @param node The linked list node
31 * @return Address to the next linked list item
32 */
33struct linked_node * linked_length(struct linked_node *node) {
34	int length = 0;
35	while(node)
36	{
37		++length;
38		node = node->next;
39	}
40	return length;
41}