ABSTRACT DATA STRUCTURES | LINKED LISTS
SECTION 1 | DYNAMIC DATA STRUCTURES
A dynamic data structure is a data structure that can grow or shrink in size during the execution of a program, as opposed to a static data structure with a fixed size. This adaptability allows dynamic data structures to efficiently manage memory and handle varying data requirements. Some common examples of dynamic data structures are linked lists, trees, and graphs. Key features and characteristics of dynamic data structures include nodes, pointers, and dynamic memory allocation.
-
Nodes: A node is a fundamental unit of a dynamic data structure, representing an individual element within the structure. Each node typically contains two components: data and a reference (or multiple references) to other nodes. The data component stores the actual value or information, while the reference(s) establish connections between nodes to define the structure's overall organization.
-
Pointers: Pointers are variables that store the memory addresses of other variables or objects. In dynamic data structures, pointers are used to reference or "point to" other nodes within the structure, thereby creating links between them. By updating the pointers, the structure can be modified, such as by adding or deleting nodes, without affecting the actual data.
-
Dynamic Memory Allocation: Dynamic data structures rely on dynamic memory allocation, which allows memory to be allocated or deallocated during the program's execution. This capability enables dynamic data structures to efficiently manage memory by only allocating space when needed and releasing it when no longer in use.
Dynamic data structures are particularly useful when dealing with varying or unpredictable data sizes, as they can easily expand or contract as needed. However, they may require more complex algorithms and management compared to static data structures, which could result in higher overhead and slower performance in certain situations.
SECTION 2 | WHAT ARE LINKED LISTS
Linked lists are a dynamic data structure that link each element (node) in the list to the next via a pointer / address of the next element, by doing this items/elements within the list do not need to be stored consecutively.
myList = ["Dave", "Jill", "Jane", "Bill", "Betty"]
Imagine the list above has been stored in memory, then other files or data has been stored after it. Now imagine you want to change one of the names in the list, for example Bill is now being replaced by Jenny. Jenny has more charters in her name than Bill so the change will not fit in the current memory location that was allocated for bill, the entire list would have to be moved to a new memory location and this would then leave a space in memory where the old list was. This is just one inefficiency of using a list that is not linked.
Array VS List: The terms List and Arrays are often used to describe the same thing, a standard static list, depending on which programming language you learned, whether you say Array or List you are probably referring to the same thing, here we will use the term Array to classify a standard static list/array.
ARRAYS VS LINKED LISTS
There are advantages and disadvantages of each, first we look at Arrays.
ARRAYS
Advantages:
-
Have an index for each element - this means you can go directly to the element you are looking for.
Disadvantage:
-
If the array is frequently changed it can be heavy on the memory
LINKED LISTS
Advantages:
-
Fast to add an item to the start of a list - the rest of the list does not need to change
-
Fast to delete an item to the start of a list - the rest of the list does not need to change
-
Is a flexible type of linear list due to how data is held
Disadvantages:
-
Slow to get to the element you want - needs to iterate through all elements until the element requested is found.
-
Maintenance of pointers can be complicated and time consuming.
Linked lists are a dynamic data structure containing nodes, pointers, a head and a terminator. The term 'node' refers to each part/location of the array. The term 'pointer' refers to the part of the node that points to another node, in actual terms it points to the memory location that the data is stored. The 'head' refers to the first node in the array and the 'terminator' the last node in the array.
SECTION 3 | SINGLE LINKED LISTS
Image illustrating a single linked list where each node has a link to the next node.
A singular linked list has a link from each node to the next node, it has a head node and a terminator/end node. This is the most basic type of linked list and the traversal between nodes can only go forward, from start to end, there is no way to traverse back to a previous node.
SECTION 4 | DOUBLE LINKED LISTS
Image illustrating a double linked list where each node has a link to the next and previous node.
A double linked list has a pointer to the next node and a pointer to the previous node, this allows forward and backward traversal. This principle is often used on features such as forward and backward page navigation on web-browsers, with each node recording pages visited it is easy to navigate using the forward and back buttons. The same principle is also used on undo and redo features on various software.
SECTION 5 | CIRCULAR LINKED LISTS
Single Circular Linked List
Double Circular Linked List
Circular linked lists link the end of the list back to the start of the list.They are used for many applications that run on a continuous loop, such as:
-
A music playlist that is on repeat
-
A multiplayer gaming environment where the OS cycles through each player at a time in a loop.
-
Running multiple applications on a computer, the OS will cycle through each application giving it an allocated slot of time per cycle.
SECTION 6 | MODIFYING ELEMENTS OF A LINKED LIST
To modify items within a linked list it is important that you do not delete the pointer of previous or next nodes until a copy is made or they are no longer needed.
For the purpose of examination style questions it is important that you take a methodical approach to the process, giving structured step by step answers.
ADD AN ELEMENT
Use the following steps to add an item to a linked list:
1: Create the new node with the data to be added
2: Search the linked list to find the location where the new node should go
3: Set the pointer of the new node to the value held by the node before the location where the new node will be placed
4: Set the pointer of the previous node to the point to the new node being added
DELETE AN ELEMENT
Use the following steps to add an item to a linked list:
1: Search the linked list to find the location where the node needs to be deleted.
2: Set the node in the location before where the deleted item will be removed from to the value of the pointer held in the node to be deleted.
3: Delete the node
UPDATE AN ELEMENT
Use the following steps to add an item to a linked list:
1: Search the linked list to find the location where node to be modified is located
2: If found, modify the data within the node