Week 9 Lecture Code
// Pantea Aria
// Returning a pointer to a struct
// Dynamic memory allocation using malloc
// dereferencing using ->
// Freeing memory using free
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100
struct student {
char name[MAX_LEN];
int id;
};
// Function prototypes
struct student *create_student(char *name, int id);
void print_student(struct student s);
int main(void) {
// 1. Create a student id =12345 name = "Pantea Aria"
struct student *new_student = create_student("Pantea Aria", 12345);
// 2. Print student details
print_student(*new_student);
// 3. Free allocated memory
free(new_student);
return 0;
}
// Function to create a student and return pointer - ToDo
struct student *create_student(char *name, int id) {
// allocate memory for the student
struct student *new = malloc(sizeof(struct student));
// check if malloc worked correctly
if (new == NULL) {
printf("malloc ERROR\n");
return NULL;
}
// assign name and id to the new struct
strcpy(new->name, name);
new->id = id;
// return the new student
return new;
}
// Function to print student details - ToDo
void print_student(struct student s) {
printf("Name: %s\n", s.name);
printf("ID: %d\n", s.id);
}
// Pantea Aria
// linked list introduction
// What is a linked list? How is it different from an array? static memory
// collection of same data type - dynamic memory in linked list malloc
// Draw a linked list of three nodes with values 10, 20, and 30.
// What does the last node of a singly linked list point to? NULL
// make linked lists with 3 nodes, print each node's data
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
int main (void) {
// declare, malloc and initialise for first node
struct node *first = malloc(sizeof(struct node));
// always check if it is not returning NULL your turn
first->data = 10;
first->next = NULL;
// declare, malloc and initialise for second node
struct node *second = malloc(sizeof(struct node));
// always check if it is not returning NULL your turn
second->data = 20;
second->next = NULL;
// declare, malloc and initialise for third node
struct node *third = malloc(sizeof(struct node));
// always check if it is not returning NULL your turn
third->data = 30;
third->next = NULL;
// link first to second
// I must update the first node's next
first->next = second;
// link second to third
// I must update the next of second node
second->next = third;
// print out all values using first node
printf("%d %d %d\n", first->data,
first->next->data,
first->next->next->data);
// print all next
printf("%p %p %p\n", first, first->next, first->next->next);
printf("%p %p %p\n", first, second, third);
free(first);
free(second);
free(third);
return 0;
}
// Pantea Aria
// linked list functions
// Draw a linked list of three nodes with values 10, 20, and 30.
// make linked lists with 3 nodes values 10, 20, and 30
// print each node's data using a function
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void print_list(struct node *head);
int main (void) {
// declare, malloc and initialise for first node
struct node *first = malloc(sizeof(struct node));
if (first == NULL) {
// print the error
return 1;
}
first->data = 10;
// declare, malloc and initialise for second node
first->next = malloc(sizeof(struct node));
// test if it is not NULL - your turn
first->next->data = 20;
// declare, malloc and initialise for third node
first->next->next = malloc(sizeof(struct node));
// test if it is not NULL - your turn
first->next->next->data = 30;
// I must set the last node's next to NULL
first->next->next->next = NULL;
// print out all values
printf("%d %d %d\n", first->data,
first->next->data, first->next->next->data);
// cal print_list
print_list(first);
return 0;
}
void print_list(struct node *head) {
// make a temporary pointer to travers my linked list
struct node *current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// Pantea Aria
// Linked Lists - Create, travers, insert
// Build a linked list by inserting values 0 to 9
// at the end (tail) of an initially empty list
// Use a loop to repeatedly add each new node to the tail
// free functions for later
// For now, this program will leak memory
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
// TODO 1:
// Write the function prototypes for:
// - create_node
struct node *create_node(int data, struct node *next);
// - print_list
void print_list(struct node *head);
// - insert_at_tail
struct node *insert_at_tail(struct node *head, int data);
// - list_length
int list_length(struct node *head);
// - insert_at_middle
struct node *insert_at_middle(struct node *head, int data);
int main(void) {
// TODO 2:
// Declare a pointer called head and initialise it to NULL
// This will represent an empty linked list
struct node *head = NULL;
// TODO 3:
// Use a loop to insert values 0–9 at the START of the list
// (Hint: create_node should be used here)
// After each insertion, print the list
for (int i = 0; i < 10; i++) {
struct node *new_node = create_node(i, head);
head = new_node;
print_list(head);
}
// TODO 4:
// Create a second list called head2
// Insert values 0–9 at the END of the list using insert_at_tail
// After each insertion:
// - Print the list length
// - Print the list contents
// TODO 5:
// Insert the value 99 into the middle of head2
// Print the new length and the updated list
// TODO 6:
// Create a third list (empty)
// Insert 42 into the middle of this empty list
// Print the result
return 0;
}
// TODO 7:
// Implement create_node
// - Allocate memory
// - Check if malloc returned NULL
// - Initialise data and next
// - Return the new node
struct node *create_node(int data, struct node *next) {
struct node *new = malloc(sizeof(struct node));
if (new == NULL) {
return NULL;
}
new->data = data;
new->next = next;
return new;
}
// TODO 8:
// Implement print_list
// - Traverse from head to NULL
// - Print each data value
void print_list(struct node *head) {
// make a temporary pointer to travers my linked list
struct node *current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// TODO 9:
// Implement insert_at_tail
// - Create a new node
// - If list is empty, return new node as head
// - Otherwise traverse to the last node
// - Attach new node at the end
// - Return head
// TODO 10:
// Implement list_length
// - Traverse the list
// - Count how many nodes exist
// - Return the count
// TODO 11:
// Implement insert_at_middle
// - Create a new node
// - If list is empty, return new node
// - Find the position size/2
// - Move to the node before that position
// - Adjust pointers to insert the new node
// - Return head
// Pantea Aria
// Returning a pointer to a struct
// Dynamic memory allocation using malloc
// dereferencing using ->
// Freeing memory using free
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100
struct student {
char name[MAX_LEN];
int id;
};
// Function prototypes
struct student *create_student(char *name, int id);
void print_student(struct student s);
int main(void) {
// 1. Create a student id =12345 name = "Pantea Aria"
// 2. Print student details
// 3. Free allocated memory
return 0;
}
// Function to create a student and return pointer - ToDo
struct student *create_student(char *name, int id) {
// allocate memory for the student
// check if malloc worked correctly
// assign name and id to the new struct
// return the new student
}
// Function to print student details - ToDo
void print_student(struct student s) {
printf("Name: %s\n", s.name);
printf("ID: %d\n", s.id);
}
// Pantea Aria
// linked list introduction
// What is a linked list? How is it different from an array? static memory
// collection of same data type - dybnamic memory in linked list malloc
// Draw a linked list of three nodes with values 10, 20, and 30.
// What does the last node of a singly linked list point to? NULL
// make linked lists with 3 nodes, print each node's data
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
int main (void) {
// declare, malloc and initialise for first node
// declare, malloc and initialise for second node
// declare, malloc and initialise for third node
// link first to second
// link second to third
// print out all values
// print all next
return 0;
}
// Pantea Aria
// linked list functions
// Draw a linked list of three nodes with values 10, 20, and 30.
// make linked lists with 3 nodes values 10, 20, and 30
// print each node's data using a function
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
int main (void) {
// declare, malloc and initialise for first node
// declare, malloc and initialise for second node
// declare, malloc and initialise for third node
// link first to second
// link second to third
// print out all values
// cal print_list
return 0;
}
// Pantea Aria
// Linked Lists - Create, travers, insert
// Build a linked list by inserting values 0 to 9
// at the end (tail) of an initially empty list
// Use a loop to repeatedly add each new node to the tail
// free functions for later
// For now, this program will leak memory
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
// TODO 1:
// Write the function prototypes for:
// - create_node
// - print_list
// - insert_at_tail
// - list_length
// - insert_at_middle
int main(void) {
// TODO 2:
// Declare a pointer called head and initialise it to NULL
// This will represent an empty linked list
// TODO 3:
// Use a loop to insert values 0–9 at the START of the list
// (Hint: create_node should be used here)
// After each insertion, print the list
// TODO 4:
// Create a second list called head2
// Insert values 0–9 at the END of the list using insert_at_tail
// After each insertion:
// - Print the list length
// - Print the list contents
// TODO 5:
// Insert the value 99 into the middle of head2
// Print the new length and the updated list
// TODO 6:
// Create a third list (empty)
// Insert 42 into the middle of this empty list
// Print the result
return 0;
}
// TODO 7:
// Implement create_node
// - Allocate memory
// - Check if malloc returned NULL
// - Initialise data and next
// - Return the new node
// TODO 8:
// Implement print_list
// - Traverse from head to NULL
// - Print each data value
// TODO 9:
// Implement insert_at_tail
// - Create a new node
// - If list is empty, return new node as head
// - Otherwise traverse to the last node
// - Attach new node at the end
// - Return head
// TODO 10:
// Implement list_length
// - Traverse the list
// - Count how many nodes exist
// - Return the count
// TODO 11:
// Implement insert_at_middle
// - Create a new node
// - If list is empty, return new node
// - Find the position size/2
// - Move to the node before that position
// - Adjust pointers to insert the new node
// - Return head