From 971c34f4c1c9e8fdf5d348871c49763c9a066e23 Mon Sep 17 00:00:00 2001 From: kunal jain Date: Tue, 12 Apr 2022 14:01:07 -0400 Subject: [PATCH] structure intro, arrays and linked list --- linked_list.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ struct-arrays.c | 71 ++++++++++++++++++++++++++++++++++ struct.c | 57 +++++++++++++++++++++++++++ 3 files changed, 229 insertions(+) create mode 100644 linked_list.c create mode 100644 struct-arrays.c create mode 100644 struct.c diff --git a/linked_list.c b/linked_list.c new file mode 100644 index 0000000..fa157aa --- /dev/null +++ b/linked_list.c @@ -0,0 +1,101 @@ +/** + * @file linked_list.c + * @author Add and print characters using a linked list + * @brief + * @version 0.1 + * @date 2022-04-12 + * + * @copyright Copyright (c) 2022 + * + */ + +#include +#include + +// Create a structure for node of linked list +// Each node contains 2 attributes/members +// 1. Value -> Value of the node +// 2. Next -> Pointer to the next node +struct NODE { + char value; + struct NODE * next; +}; + +int main(void) { + + int character; + // Create pointer to NODE and initialize it with null value + // This is first NODE in the linked list + struct NODE * first = NULL; + + // Read input characters + while(1) { + character = getchar(); + + // Break loop if character is EOF + if (character == EOF) { + break; + } + + // Add character if not EOF + else { + // create a new_node + // allocate memory using malloc + // new_node.value = character + // new_node.next = null + struct NODE * new_node = malloc(sizeof(struct NODE)); + (*new_node).value = character; + (*new_node).next = NULL; + + // If first = null, that means list is empty + if (first == NULL) { + // add new_node at starting position + // first = new_node + first = new_node; + } + + // If first != null + else { + // Go to the end of the linked list, + // and add a new node + + // Create a pointer to a node and initialize it with null + // This node keeps track of last element of linked list. + struct NODE * last = NULL; + + // After initialization, set last = first. + last = first; + + // Go to the end of linked list + // through next member of last NODE + while((*last).next != NULL) { + // last = last.next + last = (*last).next; + } + + // Once reached to the end, set last.next to new_node + (*last).next = new_node; + } + } + } + // print all characters that we just read. + // start printing from first character + + // To print, create a pointer to a NODE and initialize if with null + // This code keeps track of current element of the linked list + struct NODE * current = NULL; + + // Start with current = first + current = first; + + // print till last character. + while(current != NULL) { + printf("%c", (*current).value); + + // set current to current.next in each iteration + current = (*current).next; + } + + return 0; + +} diff --git a/struct-arrays.c b/struct-arrays.c new file mode 100644 index 0000000..22064bc --- /dev/null +++ b/struct-arrays.c @@ -0,0 +1,71 @@ +/** + * @file struct-arrays.c + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2022-04-07 + * + * @copyright Copyright (c) 2022 + * + */ + +// Structure array + +// Create a structure array for a class of students, +// and then print the structure using function. +// 2 functions - simple print, print using pointers + +#include +#include + +struct student { + char* name; + int age; +}; + +void printClassInfo(struct student class[], int no_of_students) { + int i; + for (i = 0; i < no_of_students; i++) { + printf("Name of the student is -> %s\n", class[i].name); + printf("Age of the student is -> %d\n", class[i].age); + } +} + +void printClassInfoUsingPointer(struct student* p, int no_of_students) { + int i; + for (i = 0; i < no_of_students; i++) { + printf("Name of the student is -> %s\n", (*(p + i)).name); + printf("Age of the student is -> %d\n", (*(p + i)).age); + } +} + + + +int main (void) { + + struct student class[10]; + + struct student std; + + std.name = "john"; + std.age = 21; + class[0] = std; + + std.name = "jane"; + std.age = 20; + class[1] = std; + + printClassInfo(class, 2); + + struct student* p = malloc(2 * sizeof(struct student)); + + (*(p)).name = "john"; + (*(p)).age = 21; + + (*(p + 1)).name = "jane"; + (*(p + 1)).age = 20; + + printClassInfoUsingPointer(&(p[0]), 2); + + return 0; +} \ No newline at end of file diff --git a/struct.c b/struct.c new file mode 100644 index 0000000..20b29f3 --- /dev/null +++ b/struct.c @@ -0,0 +1,57 @@ +/** + * @file struct.c + * @author kunal jain (kunal.jain001@umb.edu) + * @brief Program to demonstrate structs and their use in C + * @version 0.1 + * @date 2022-04-06 + * + * @copyright Copyright (c) 2022 + * + */ +#include + +struct point { + int x; + int y; +}; + +struct rect { + struct point pt1; + struct point pt2; +}; + +// Create a function that will tell us if a point is in a rectangle or not +int isPointInsideBox(struct rect box, struct point p) { + return p.x >= box.pt1.x && p.x <= box.pt2.x && p.y >= box.pt1.y && p.x <= box.pt2.y; +} + +int main (void) { + + struct rect box; + + // Declaration of Structure + // Here point is a structure which has + // variables x and y as its members. + box.pt1.x = 300; + box.pt1.y = 200; + + box.pt2.x = 400; + box.pt2.y = 400; + + struct point p = {350, 250}; + + printf("Is point inside rectangle -> %s", isPointInsideBox(box, p) == 1 ? "yes" : "no"); + // isPointInsideBox(box, p); + + // printf("p1.x -> %d\n", p1.x); + // printf("p1.y -> %d\n", p1.y); + + // printf("p2.x -> %d\n", p2.x); + // printf("p2.y -> %d\n", p2.y); + + // p2 = p1; + + // printf("After assignment\n"); + // printf("p2.x -> %d\n", p2.x); + // printf("p2.y -> %d\n", p2.y); +} \ No newline at end of file