commit d0e34320b93525004ad9003096272246f0359c8c
parent 7f3e230d09d2a0121bef4b067ebdc5f0bb494703
Author: Christos Margiolis <christos@margiolis.net>
Date: Fri, 29 Jan 2021 01:11:16 +0200
more data structures
Diffstat:
10 files changed, 392 insertions(+), 53 deletions(-)
diff --git a/c-data-structures/a.out b/c-data-structures/a.out
Binary files differ.
diff --git a/c-data-structures/bintree.c b/c-data-structures/bintree.c
@@ -0,0 +1,122 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct Student {
+ char name[50];
+ float grade;
+ int id;
+ struct Student *left;
+ struct Student *right;
+};
+
+static struct Student *
+createstud(struct Student *root)
+{
+ struct Student *s;
+
+ if ((s = malloc(sizeof(struct Student))) == NULL) {
+ fputs("malloc failed\n", stderr);
+ exit(EXIT_FAILURE);
+ }
+
+ printf("Name: ");
+ fgets(s->name, sizeof(s->name), stdin);
+ s->name[strlen(s->name) - 1] = '\0';
+ printf("ID: ");
+ scanf("%d", &s->id);
+ getchar();
+ printf("Grade: ");
+ scanf("%f", &s->grade);
+ getchar();
+
+ s->left = NULL;
+ s->right = NULL;
+
+ return s;
+
+}
+
+static void
+addinorder(struct Student *root, struct Student *s)
+{
+ if (s->id < root->id) {
+ if (root->left == NULL)
+ root->left = s;
+ else
+ addinorder(root->left, s);
+ } else {
+ if (root->right == NULL)
+ root->right = s;
+ else
+ addinorder(root->right, s);
+ }
+}
+
+static void
+printinorder(struct Student *root)
+{
+ if (root != NULL) {
+ printinorder(root->left);
+ printf("Name: %s | ID: %d | Grade: %.2f\n", root->name,
+ root->id, root->grade);
+ printinorder(root->right);
+ }
+}
+
+static float
+maxgrade(struct Student *root)
+{
+ float max = -1, maxright = -1;
+
+ if (root != NULL) {
+ max = maxgrade(root->left);
+ if (root->grade > max)
+ max = root->grade;
+ maxright = maxgrade(root->right);
+ if (maxright > max)
+ max = maxright;
+ return max;
+ }
+
+ return max;
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct Student *root = NULL, *stud;
+ float max;
+ int ch = -1;
+
+ while (ch != 0) {
+ puts("1. Add student in order");
+ puts("2. Print students in order");
+ puts("3. Show maximum grade");
+ puts("0. Exit");
+ printf("\nYour choice: ");
+ scanf("%d", &ch);
+ getchar();
+
+ switch (ch) {
+ case 1:
+ stud = createstud(root);
+ if (root == NULL)
+ root = stud;
+ else
+ addinorder(root, stud);
+ break;
+ case 2:
+ printinorder(root);
+ break;
+ case 3:
+ if ((max = maxgrade(root)) == -1)
+ puts("Empty tree.");
+ else
+ printf("Max grade: %.2f\n", max);
+ break;
+ }
+ }
+
+ return 0;
+}
diff --git a/c-data-structures/data b/c-data-structures/data
@@ -0,0 +1,2 @@
+123 held 123.00
+1911 lll 11.00
diff --git a/c-data-structures/files_ex1.c b/c-data-structures/files_ex1.c
@@ -0,0 +1,47 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct Student {
+ char name[50];
+ float grade;
+ int id;
+};
+
+int
+main(int argc, char *argv[])
+{
+ FILE *fp;
+ struct Student *s;
+
+ if ((s = malloc(sizeof(struct Student))) == NULL) {
+ fputs("malloc failed\n", stderr);
+ return 1;
+ }
+
+ if ((fp = fopen("data", "w")) == NULL) {
+ fputs("fopen failed\n", stderr);
+ return 1;
+ }
+
+ for (;;) {
+ printf("Name: ");
+ fgets(s->name, sizeof(s->name), stdin);
+ s->name[strlen(s->name) - 1] = '\0';
+ printf("ID: ");
+ scanf("%d", &s->id);
+ getchar();
+ printf("Grade: ");
+ scanf("%f", &s->grade);
+ getchar();
+
+ if (s->id < 0)
+ break;
+ fprintf(fp, "%d %s %.2f\n", s->id, s->name, s->grade);
+ }
+
+ fclose(fp);
+ free(s);
+
+ return 0;
+}
diff --git a/c-data-structures/files_ex2.c b/c-data-structures/files_ex2.c
@@ -0,0 +1,34 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct Student {
+ char name[50];
+ float grade;
+ int id;
+};
+
+int
+main(int argc, char *argv[])
+{
+ FILE *fp;
+ struct Student *s;
+
+ if ((s = malloc(sizeof(struct Student))) == NULL) {
+ fputs("malloc failed\n", stderr);
+ return 1;
+ }
+
+ if ((fp = fopen("data", "r")) == NULL) {
+ fputs("fopen failed\n", stderr);
+ return 1;
+ }
+
+ while (fscanf(fp, "%d %s %f", &s->id, s->name, &s->grade) != EOF)
+ printf("%d %s %.2f\n", s->id, s->name, s->grade);
+
+ fclose(fp);
+ free(s);
+
+ return 0;
+}
diff --git a/c-data-structures/queue.c b/c-data-structures/queue.c
@@ -0,0 +1,97 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct Student {
+ char name[50];
+ float grade;
+ int id;
+ struct Student *next;
+};
+
+struct Student *head, *tail;
+
+static void
+enqueue(struct Student *s)
+{
+ struct Student *p;
+
+ if ((p = malloc(sizeof(struct Student))) == NULL) {
+ fputs("malloc failed\n", stderr);
+ exit(EXIT_FAILURE);
+ }
+
+ *p = *s;
+ p->next = NULL;
+ if (tail == NULL)
+ head = p;
+ else
+ tail->next = p;
+ tail = p;
+}
+
+static void
+dequeue(void)
+{
+ struct Student *p;
+
+ if (head != NULL) {
+ p = head;
+ head = head->next;
+ free(p);
+ if (head == NULL)
+ tail = NULL;
+ }
+}
+
+static void
+print(void)
+{
+ struct Student *p;
+
+ for (p = head; p != NULL; p = p->next)
+ printf("Name: %s | ID: %d | Grade: %.2f\n", p->name, p->id, p->grade);
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct Student stud;
+ int ch = -1;
+
+ head = NULL;
+ tail = NULL;
+
+ while (ch != 0) {
+ puts("1. Enqueue");
+ puts("2. Dequeue");
+ puts("3. Print");
+ puts("0. Exit");
+ printf("\nYour choice: ");
+ scanf("%d", &ch);
+ getchar();
+
+ switch (ch) {
+ case 1:
+ printf("Name: ");
+ fgets(stud.name, sizeof(stud.name), stdin);
+ stud.name[strlen(stud.name) - 1] = '\0';
+ printf("ID: ");
+ scanf("%d", &stud.id);
+ getchar();
+ printf("Grade: ");
+ scanf("%f", &stud.grade);
+ getchar();
+ enqueue(&stud);
+ break;
+ case 2:
+ dequeue();
+ break;
+ case 3:
+ print();
+ break;
+ }
+ }
+
+ return 0;
+}
diff --git a/c-data-structures/stack_list.c b/c-data-structures/stack_list.c
@@ -0,0 +1,90 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct Student {
+ char name[50];
+ float grade;
+ int id;
+ struct Student *next;
+};
+
+struct Student *head;
+
+static void
+push(struct Student *s)
+{
+ struct Student *p;
+
+ if ((p = malloc(sizeof(struct Student))) == NULL) {
+ fputs("malloc failed\n", stderr);
+ exit(EXIT_FAILURE);
+ }
+
+ *p = *s;
+ p->next = head;
+ head = p;
+}
+
+static void
+pop(void)
+{
+ struct Student *p;
+
+ if (head != NULL) {
+ p = head;
+ head = head->next;
+ free(p);
+ }
+}
+
+static void
+print(void)
+{
+ struct Student *p;
+
+ for (p = head; p != NULL; p = p->next)
+ printf("Name: %s | ID: %d | Grade: %.2f\n", p->name, p->id, p->grade);
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct Student stud;
+ int ch = -1;
+
+ head = NULL;
+
+ while (ch != 0) {
+ puts("1. Push");
+ puts("2. Pop");
+ puts("3. Print");
+ puts("0. Exit");
+ printf("\nYour choice: ");
+ scanf("%d", &ch);
+ getchar();
+
+ switch (ch) {
+ case 1:
+ printf("Name: ");
+ fgets(stud.name, sizeof(stud.name), stdin);
+ stud.name[strlen(stud.name) - 1] = '\0';
+ printf("ID: ");
+ scanf("%d", &stud.id);
+ getchar();
+ printf("Grade: ");
+ scanf("%f", &stud.grade);
+ getchar();
+ push(&stud);
+ break;
+ case 2:
+ pop();
+ break;
+ case 3:
+ print();
+ break;
+ }
+ }
+
+ return 0;
+}
diff --git a/c-parallel-computing/ex3/doc.pdf b/c-parallel-computing/ex3/doc.pdf
diff --git a/c-parallel-computing/ex3/doc.tex b/c-parallel-computing/ex3/doc.tex
@@ -1,32 +0,0 @@
-\documentclass{article}
-\usepackage[utf8]{inputenc}
-\usepackage[greek,english]{babel}
-\usepackage{alphabeta}
-\usepackage{fancyhdr}
-\usepackage{listings}
-\usepackage{mathtools}
-\usepackage{xcolor}
-\usepackage{biblatex}
-\usepackage[left=2cm,right=2cm]{geometry}
-
-\lstset {
- basicstyle=\ttfamily,
- columns=fullflexible,
- breaklines=true,
- keepspaces=true
-}
-
-\title{Εργαστήριο Παράλληλου Υπολογισμού - Εργασία 3}
-\author{Χρήστος Μαργιώλης}
-\date{Ιανουάριος 2020}
-
-\begin{document}
-
-\begin{titlepage}
- \maketitle
-\end{titlepage}
-
-\renewcommand{\contentsname}{Περιεχόμενα}
-\tableofcontents
-
-\end{document}
diff --git a/c-parallel-computing/ex3/ex3.c b/c-parallel-computing/ex3/ex3.c
@@ -1,21 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <mpi.h>
-
-int
-main(int argc, char *argv[])
-{
- int nproc, rank, root = 0;
- int rc;
-
- if ((rc = MPI_Init(&argc, &argv)) != 0) {
- fprintf(stderr, "%s: cannot initiliaze MPI.\n", argv[0]);
- MPI_Abort(MPI_COMM_WORLD, rc);
- }
- MPI_Comm_size(MPI_COMM_WORLD, &nproc);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
-
- MPI_Finalize();
-
- return 0;
-}