1/6
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Dynamic Memory
used to allocate elements in data structures
use pointers between dynamic instances to organise our data structure
Building a Dynamic ‘Singly Linked’ List
we can start with ‘no items’
and build up our data structure one item at a time
Chaining Nodes / Growing the List
e.g. Allocate a new node (using malloc)
‘chain it’ so our first item points to the new item
General Rules
allocate space for a ‘node’ (a struct) of the appropriate type
find where to add our item (start, end, insertion point)
adjust the pointers to stay consistent with the type of data structure we’re working with
In Memory
as the data structure grows, we allocate more ‘blocks’ of dynamic memory
we chain these together to form our data structure
we need to be careful to manage our pointers and hand memory back to the memory allocator
Declaring a ‘node’ struct in C
//define listItem node type
struct listItem {
char name [20];
char college [20];
//self-referential pointer
struct listItem *next;
};
Declaring a variable (head pointer) in C
int main()
{
struct listItem *head = NULL;
}