Programming Fundamentals
Tutorial Overview:
- Part 1: Malloc and Free
- Part 2: Diagramming Linked Lists
- Part 3: Linked lists
Reminder: Assignment 2 released
If you have any questions about Assignment 2, feel free to ask them now.
Part 1: Malloc and free (15 mins)
In this section we'll visit malloc
and free
.
Group activity
Using your knowledge of malloc()
and sizeof()
, write up code to malloc the following:
- an integer
- a double
- a character
- an array of 10 characters
- the following struct
struct my_struct {
int number;
char letter;
double another_number;
}
Part 2: Diagramming Linked Lists (10 mins)
In this activity we will run through the following instructions and build a diagram.
malloc memory for a new node called node1
node1 data = 3
node1 next = NULL
make the head pointer points to node1
malloc memory for a new node called node2
node2 data = 9
node2 next = NULL
add node2 to the tail of the list, making node1 next point to node2
malloc memory for a new node called node3
node3 data = 5
node3 next = NULL
add node3 to the head of the list, making node3 next point to the current head of the list
make the head point to node3
Part 3: Programming linked lists (30 mins)
main()
is used to test your linked lists functions. We will implement functions to create a node, insert a node at the tail of a linked list, insert a node at the head of a linked list and find the length of a linked list.
// part3_linked_lists.c
// This program was written by Sofia De Bellis (z5418801) and YOUR-NAME (zID)
// Program tests linked lists functions
#include <stdio.h>
#include <stdlib.h>
struct node {
// The data stored in the node
int data;
// Pointer to the next node in the linked list
struct node *next;
};
struct node *create_node(int data);
struct node *insert_head(struct node *head, int data);
struct node *insert_tail(struct node *head, int data);
void print_list(struct node *head);
int list_length(struct node *head);
int main(void) {
// Create a pointer to the head of the linked list
struct node *head = NULL;
// Insert a node at the beginning of the linked list
head = insert_head(head, 10);
// Insert a node at the beginning of the linked list
head = insert_head(head, 5);
// Insert a node at the end of the linked list
head = insert_tail(head, 15);
// Insert a node at the end of the linked list
head = insert_tail(head, 20);
// Print the linked list
print_list(head);
// Calculate and print the length of the linked list
int length = list_length(head);
printf("There are %d nodes in the list\n", length);
return 0;
}
// Creates a new node
//
// Parameters:
// data: The data to be stored in the new node
//
// Returns:
// A pointer to the new node
struct node *create_node(int data) {
// TODO
exit(1);
}
// Inserts a new node at the head of a linked list
//
// Parameters:
// head: A pointer to the head of the linked list
// data: The data to be stored in the new node
//
// Returns:
// A pointer to the new head of the linked list
struct node *insert_head(struct node *head, int data) {
// TODO
exit(1);
}
// Inserts a new node at the tail of a linked list
//
// Parameters:
// head: A pointer to the head of the linked list
// data: The data to be stored in the new node
//
// Returns:
// A pointer to the head of the linked list
struct node *insert_tail(struct node *head, int data) {
// TODO
exit(1);
}
// Traverses a linked list and prints the data in each node
//
// Parameters:
// head: A pointer to the head of the linked list
//
// Returns:
// None
void print_list(struct node *head) {
// Set current to be the first node in the list
struct node *current = head;
// Traverse the linked list and print each node
// until we reach the end of the list
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("X\n");
}
// Finds the number of nodes in a linked list
//
// Parameters:
// head: A pointer to the head of the linked list
//
// Returns:
// The number of nodes in the linked list
int list_length(struct node *head) {
// TODO
exit(1);
}