Linked Lists

Structs are like classes in an object-oriented language, storing values in variables determined by the user. When accessing one of the struct's variable's value, we refer to it with a ".", while accessing a variable's pointer is through "->".

Ex:
struct vector2 {
float x;
float y;
struct vector2* nextvector;
};

int main() {
struct vector2 position;
position.x = 1;
position.y = 2;

struct vector2* destination;

position->next = destination;
}



To simplify referring to a struct, use typedef to define it as its own data type:

struct vector2 {
float x;
float y;
float* nextvector;
} Vector2;
//OR
typedef struct vector2 Vector2;

int main() {
Vector2 position;
position.x = 3;
position.y = 4;
}



A clever use of structs is to create a linked list system, where a struct contains a piece of data and a pointer to the next instance of that struct.




//This defines the individual nodes of a linked list

struct list {
int data;
struct list* next;
} Node;


//This creates an empty instance of a linked list

Node create_list() {
return NULL;
}


//This inserts a new value into the start of the list

Node* insert_start(Node old_start, int data) {
Node* newList = (Node*)malloc(sizeof(Node));
newList.info = data;
newList->next = old_start;
return newList;
}


//This will iterate through all values in te linked list, printing them

void print_list(Node* list) {
for (Node* pointer = list; pointer != null; pointer->next) }


int main () {
Node item1, item2, item3;
item1->next = item2;
item2->next = item3;
}

Stacks and Queues

Binary Trees

AVL Trees

Graphs

File Processing

File Ordering

B and B+ Trees