Structures in Cstruct: An abstract data type.
Abstraction is defined by two very important properties:
Namespace: Defines scope (where something is visible).
Global Namespace: Scope where something is accessible to the entire program.
#include), function prototypes, etc) here.Example: Creating a struct
#include <stdio.h>
struct Student
{
char* name;
int areaCode;
};
int main()
{
struct Student x = { "Antonio Belpaese", 666 };
printf("Student: %s (%d)\n", x.name, x.areaCode);
}struct (->)->: Operator to select a member of a struct through a pointer.
. operator for pointers to structs.Example:
#include <stdio.h>
struct Student
{
char* name;
int areaCode;
};
int main()
{
struct Student x = { "Antonio Belpaese", 666 };
struct Student *ptr = &x;
// I. Using dot operator and dereference operator
printf("Student: %s (%d)\n", (*ptr).name, (*ptr).areaCode);
// II. Doing the same thing as above but with ->
printf("Student: %s (%d)\n", ptr->name, ptr->areaCode);
}You can have functions return pointers.
structs.int *createIntArray(int n)
{
int *p = malloc(n*sizeof(n));
if (p == NULL) {
puts("Out of memory");
return 0;
}
return(p) ;
}
int main() {
int *a;
a = createIntArray(5) ;
free(a);
return 0;
}Linked List: Linear data structure in which each element is dynamically allocated and elements point to each other.
Each node typically has two components:
Head and Tail:
Types:
Operations on Linked List:
Pros and Cons:
| Pros | Cons |
|---|---|
| Dynamic size | No direct access |
| Efficient insertion and deletion | Memory cost |
| Flexible |
To define a node for a linked list create a recursive struct:
struct node {
int data;
struct node* next;
}Now we can create the head node like so:
struct node* head = malloc(sizeof(node));
if (head == NULL)
return 1;
// Remember, head is a pointer to a pointer!
head->data = 1;
head->next = NULL;To add another node to the end of our linked list we can just create a new node and make head point to it:
head->next = malloc(sizeof(node));
head->next->data = 2;
head->next->next = NULL;To work with linked lists programmatically, we should first know how to traverse (iterate over) them.
void print_list(node* head) {
node* current = head;
while (current != NULL) {
printf("%d\n", current->data);
current = current->next;
}
}Returning to our first example where we did—
head->next = malloc(sizeof(node));
head->next->data = 2;
head->next->next = NULL;—Another way to append an item to the list would be to traverse until reaching the tail (where node->next = NULL):
void append(node* head, int data) {
node* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = malloc(sizeof(node));
current->next->data = data;
current->next->next = NULL;
}Steps:
void push(node** head, int data) {
// 1
node* new_node;
new_node = malloc(sizeof(node));
new_node->data = data;
// 2
new_node->next = *head;
// 3
*head = new_node;
}**head) in order to modify the pointer itself.Note: Look, there were only 6 slides in the lecture on linked list which just showed some C code with almost zero commentary, and the professor went over none of them during lecture; so this entire section on linked lists is actually based entirely on these two articles:
- Linked lists, learn-c.org
- [Understanding the basics of Linked List, GeeksForGeeks.org}(https://www.geeksforgeeks.org/what-is-linked-list/)
Because my time is very limited and because the slides only mentioned these two ways to append to a linked list, I’m not going to expand my notes to include other stuff.
- E.g., how to insert anywhere in the middle of a linked list, pop items from a linked list, etc.