Week 7 Code Examples
#include <stdlib.h>
#include <stdio.h>
#include "list.h"
struct node *create_node(int num) {
struct node *new_node = malloc(sizeof(struct node));
// *, *my_num <- deferencing the pointer to get the value of it
// (*new_node).data
new_node->data = num;
new_node->next = NULL;
return new_node;
}
void print_list(struct node *head) {
struct node *current = head;
if (head == NULL) {
printf("The list is empty!\n");
return;
}
while (current != NULL) {
printf("The node's data is %d\n", current->data);
current = current->next;
}
}
struct node *insert_head(struct node *head, int num) {
struct node *new_node = create_node(num);
new_node->next = head;
head = new_node;
return head;
}
struct node *insert_tail(struct node *head, int num) {
struct node *current = head;
struct node *new_node = create_node(num);
if (head == NULL) {
head = new_node;
return head;
}
while (current->next != NULL) {
current = current->next;
}
current->next = new_node;
return head;
}
struct node *insert_nth(struct node *head, int num, int n) {
if (head == NULL) {
struct node *new_node = create_node(num);
head = new_node;
return head;
}
// insert at the head
if (n <= 0) {
head = insert_head(head, num);
return head;
}
int count = 1;
struct node *current = head;
while (count != n && current->next != NULL) {
current = current->next;
count++;
}
struct node *new_node = create_node(num);
new_node->next = current->next;
current->next = new_node;
return head;
}
struct node {
int data;
struct node *next;
};
// Function that mallocs and returns a new node
// Params:
// - num: data for the node
// Returns:
// - pointer to a struct node
struct node *create_node(int num);
// FUnction to print a linked list
// Params:
// - head: a pointer to teh first node in the list
// Return:
// nothing
void print_list(struct node *head);
// Function that inserts a node into the head of a linked list
struct node *insert_head(struct node *head, int num);
// Function to insert a node at the tail of the list
// Params:
// - head: pointer to the head of the list
// - num: the dnode data we weant to insert
// Returnbs:
// - head: a pointer to the head of the list
struct node *insert_tail(struct node *head, int num);
// Function to inser at the nth position of a list
// Params:
// - head: a pointer to the first node in the list
// - num: the niode data we want to insert into the list
// - n: the position we want to insert into our list at, 1-indexed
// Return:
// - head: a pointer to the head od the list
struct node *insert_nth(struct node *head, int num, int n);
#include <stdio.h>
#include "list.h"
int main(void) {
struct node *head = NULL;
head = insert_head(head, 1);
head = insert_head(head, 3);
head = insert_head(head, 5);
head = insert_tail(head, -1);
head = insert_nth(head, 10, 2);
head = insert_nth(head, 6, -2);
head = insert_nth(head, 7, 100);
head = insert_nth(head, 15, 7);
print_list(head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct node {
struct node *next;
int data;
};
struct test {
int num1;
int num2;
int num3;
};
int main(void) {
int array[10] = {0};
struct node new_node;
struct test test2;
printf("The size of an int is %lu\n", sizeof(int));
printf("The size of an double is %lu\n", sizeof(double));
printf("The size of an char is %lu\n", sizeof(char));
printf("The size of 10 int is %lu\n", 10 * sizeof(int));
printf("The size of the array is %lu\n", sizeof(array));
printf("The size of struct node is %lu\n", sizeof(new_node));
printf("The size of the struct test is %lu\n", sizeof(test2));
int *my_ptr_int = malloc(sizeof(int));
// printf("%p\n", my_ptr_int);
*my_ptr_int = 6;
printf("%d\n", *my_ptr_int);
free(my_ptr_int);
// printf("%d\n", *my_ptr_int);
return 0;
}
#include <stdio.h>
#include "maths.h"
int main(void) {
int area = rect_area();
printf("The area is: %d\n", area);
double c_area = circle_area();
printf("The area is: %lf\n", c_area);
return 0;
}
#include <stdio.h>
#include "maths.h"
int rect_area(void) {
int height;
int width;
int area;
printf("Enter the width and height of the rectangle: ");
scanf("%d %d", &height, &width);
area = height * width;
return area;
}
double circle_area(void) {
double radius;
printf("Enter the radius of the circle: ");
scanf("%lf", &radius);
double area = PI * (radius * radius);
return area;
}
#include <stdio.h>
#define PI 3.141592653589
// This function calculates the area of a rectangle
// it scans in a height and width and returns the result
int rect_area(void);
// THis function calculates teh area of a circle
// in scans in a radius and resturns the result
double circle_area(void);