Skip to content

Commit

Permalink
structure intro, arrays and linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
jainkhere committed Apr 12, 2022
1 parent ffd820d commit 971c34f
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 0 deletions.
101 changes: 101 additions & 0 deletions linked_list.c
Original file line number Diff line number Diff line change
@@ -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<stdio.h>
#include<stdlib.h>

// 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;

}
71 changes: 71 additions & 0 deletions struct-arrays.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @file struct-arrays.c
* @author your name ([email protected])
* @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<stdio.h>
#include<stdlib.h>

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;
}
57 changes: 57 additions & 0 deletions struct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @file struct.c
* @author kunal jain ([email protected])
* @brief Program to demonstrate structs and their use in C
* @version 0.1
* @date 2022-04-06
*
* @copyright Copyright (c) 2022
*
*/
#include<stdio.h>

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);
}

0 comments on commit 971c34f

Please sign in to comment.