User Tools

Site Tools


notes:data:spring2024:projects:mpg2

MPG2

structures

singly-linked list

Reference: https://www.geeksforgeeks.org/introduction-to-linked-list-data-structure-and-algorithm-tutorial/

A linked list is a data structure that allows for a list of elements that can dynamically increase and decrease in size.

Usually, in C, an array can only have a static size once it is declared, and once it is filled, it can no longer get any more space or elements added to it.

Since a linked list is a series of “nodes”, or structs that point to each other, then new elements can easily be added: as long as there is enough memory to be allocated to a new struct, the last struct in a linked list can be made to point to this new struct.

The same also applies for removing elements. Once an element has been removed from a linked list, the memory can be freed so it can be used elsewhere, if needed.

malloc

Since this project deals with linked lists and dynamically adding and removing elements from a list, malloc will still need to be used to allocate memory.

Generally, malloc will need to be used to add a new element to a linked list.

The pseudocode will look like so:

functionForAddingElement(args) {
 
    struct* newStruct = (struct*)malloc(sizeof(struct));
 
    // Place Data into New Struct
 
    // Loop through Linked List to the End
 
    // Place New Struct at End of List
}

Reference: https://www.geeksforgeeks.org/insertion-in-linked-list/

notes/data/spring2024/projects/mpg2.txt · Last modified: 2024/02/29 04:49 by rspringe