Struct
ures 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 };
("Student: %s (%d)\n", x.name, x.areaCode);
printf}
struct
(->
)->
: Operator to select a member of a struct
through a pointer.
.
operator for pointers to struct
s.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
("Student: %s (%d)\n", (*ptr).name, (*ptr).areaCode);
printf// II. Doing the same thing as above but with ->
("Student: %s (%d)\n", ptr->name, ptr->areaCode);
printf}
You can have functions return pointers.
struct
s.Example: Trivial example of returning a pointer
int *createIntArray(int n) { int *p = malloc(n*sizeof(n)); if (p == NULL) { ("Out of memory"); putsreturn 0; } return(p) ; } int main() { int *a; = createIntArray(5) ; a (a); freereturn 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!
->data = 1;
head->next = NULL; head
To add another node to the end of our linked list we can just create a new node and make head point to it:
->next = malloc(sizeof(node));
head->next->data = 2;
head->next->next = NULL; head
To work with linked lists programmatically, we should first know how to traverse (iterate over) them.
void print_list(node* head) {
* current = head;
nodewhile (current != NULL) {
("%d\n", current->data);
printf= current->next;
current }
}
Returning to our first example where we did—
->next = malloc(sizeof(node));
head->next->data = 2;
head->next->next = NULL; head
—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) {
* current = head;
nodewhile (current->next != NULL) {
= current->next;
current }
->next = malloc(sizeof(node));
current->next->data = data;
current->next->next = NULL;
current}
Steps:
void push(node** head, int data) {
// 1
* new_node;
node= malloc(sizeof(node));
new_node ->data = data;
new_node// 2
->next = *head;
new_node// 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.