aboutsummaryrefslogtreecommitdiff
path: root/simple-C-programs/DynamicStudentList.c
diff options
context:
space:
mode:
Diffstat (limited to 'simple-C-programs/DynamicStudentList.c')
-rw-r--r--simple-C-programs/DynamicStudentList.c247
1 files changed, 149 insertions, 98 deletions
diff --git a/simple-C-programs/DynamicStudentList.c b/simple-C-programs/DynamicStudentList.c
index d318ea2..50cdc8f 100644
--- a/simple-C-programs/DynamicStudentList.c
+++ b/simple-C-programs/DynamicStudentList.c
@@ -1,16 +1,15 @@
+#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-
struct StudentList *STUDENTLIST();
-
/* Objectives:
1- Make a bare functional list (Doubly linked list)
2- add the sort and the highest functions afterward
3- add more features if things are going ok
- 4- Care about Encapulation and Abstraction ideas ...
+ 4- Care about Encapsulation and Abstraction ideas ...
*/
struct Student {
char *__name;
@@ -38,7 +37,7 @@ struct StudentList {
void (*dump)(struct StudentList *self);
struct Student *(*find)(struct StudentList *self, char *name);
void (*destructor)(struct StudentList *self);
- void (*pop) (struct Student *self, struct StudentList *list);
+ void (*pop)(struct Student *self, struct StudentList *list);
};
// Private Functions
@@ -51,30 +50,29 @@ int __MARK(struct Student *self) { return self->__mark; }
void __POP(struct Student *self, struct StudentList *list) {
- if (self == NULL || list == NULL)
- return;
+ if (self == NULL || list == NULL)
+ return;
- // if node is head
- if (self->__prev == NULL) {
- list->__head = self->__next;
- } else {
- self->__prev->__next = self->__next;
- }
+ // if node is head
+ if (self->__prev == NULL) {
+ list->__head = self->__next;
+ } else {
+ self->__prev->__next = self->__next;
+ }
- // if node is tail
- if (self->__next == NULL) {
- list->__tail = self->__prev;
- } else {
- self->__next->__prev = self->__prev;
- }
+ // if node is tail
+ if (self->__next == NULL) {
+ list->__tail = self->__prev;
+ } else {
+ self->__next->__prev = self->__prev;
+ }
- list->__count--;
+ list->__count--;
- free(self->__name);
- free(self);
+ free(self->__name);
+ free(self);
}
-
struct Student *__FIND(struct StudentList *self, char *name) {
struct Student *current = NULL;
@@ -165,48 +163,43 @@ void __Destructor(struct StudentList *self) {
struct Student *__HIGHEST(struct StudentList *self) {
- if (self == NULL || self->__head == NULL)
- return NULL;
+ if (self == NULL || self->__head == NULL)
+ return NULL;
- struct Student *current = self->__head;
- struct Student *highest = self->__head;
+ struct Student *current = self->__head;
+ struct Student *highest = self->__head;
- while (current != NULL) {
+ while (current != NULL) {
- if (current->__mark > highest->__mark) {
- highest = current;
- }
-
- current = current->__next;
+ if (current->__mark > highest->__mark) {
+ highest = current;
}
- return highest;
-}
+ current = current->__next;
+ }
+ return highest;
+}
struct StudentList *__SORT(struct StudentList *self) {
- struct StudentList *S = STUDENTLIST();
+ struct StudentList *S = STUDENTLIST();
- while (self->__head != NULL) {
+ while (self->__head != NULL) {
- struct Student *highest = __HIGHEST(self);
+ struct Student *highest = __HIGHEST(self);
- if (highest == NULL)
- break;
+ if (highest == NULL)
+ break;
- __PUT(S, highest->__name, highest->__age, highest->__mark);
+ __PUT(S, highest->__name, highest->__age, highest->__mark);
- __POP(highest, self);
- }
+ __POP(highest, self);
+ }
- return S;
+ return S;
}
-
-
-
-
// The CONSTRUCTOR (Public)
struct StudentList *STUDENTLIST() {
@@ -229,69 +222,127 @@ struct StudentList *STUDENTLIST() {
return S;
}
-// input functions (Public)
+// input functions (Public)
int intger_input() {
-
- int tmp;
-
- while(1) {
-
- if((scanf("%d", &tmp)) == 1) {
- while(getchar() != '\n');
- break;
+
+ int tmp;
+
+ while (1) {
+
+ if ((scanf("%d", &tmp)) == 1) {
+ while (getchar() != '\n')
+ ;
+ break;
}
-
- while(getchar() != '\n');
+
+ while (getchar() != '\n')
+ ;
printf("Invalid input, try again: ");
- }
-
- return tmp;
-}
+ }
+ return tmp;
+}
char *read_line() {
-
- char buffer[1024];
-
- if(fgets(buffer, sizeof(buffer), stdin) == NULL) {
- return NULL;
- }
-
- char *result = NULL;
- result = malloc(strlen(buffer) + 1);
-
- strcpy(result, buffer);
-
- result[strcspn(result, "\n")] = '\0';
-
- return result;
-}
+ char buffer[1024];
+
+ if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
+ return NULL;
+ }
+
+ char *result = NULL;
+ result = malloc(strlen(buffer) + 1);
+
+ strcpy(result, buffer);
+
+ result[strcspn(result, "\n")] = '\0';
+
+ return result;
+}
int main() {
struct StudentList *list = STUDENTLIST();
-
- list->put_student(list, "Hyder Hadi", 30, 100);
- list->put_student(list, "Hyder tafi", 30, 90);
- list->put_student(list, "Hyder safi", 30, 999);
- list->put_student(list, "Hyder kafi", 30, 234);
-
-
- struct Student *best = list->highest(list);
-
- printf("Name of the best: %s, Grade: %d, Age: %d\n", best->name(best), best->mark(best), best->age(best));
-
- printf("\n");
-
-
- struct StudentList *sorted = list->sort(list);
-
- sorted->dump(sorted);
-
- sorted->destructor(sorted);
+ int op_code;
+ char *name = NULL;
+ int age = 0;
+ int mark = 0;
+ struct Student *tmp = NULL;
+
+ printf("Welcome to DynamicStudentList, Enter 1 to show commands, to exit "
+ "Enter 0\n");
+ while ((op_code = intger_input()) != 0) {
+
+ switch (op_code) {
+ case 1:
+ printf("-Enter a student [Enter no. 2]\n-Find a student [Enter "
+ "no. 3]\n-Sort students [Enter no. 4]\n-Show student entries[Enter "
+ "no. 5]\n-Find best student [Enter no. 6]\n-To exit [Enter no. 0]");
+ break;
+ case 2:
+ printf("name -> Enter -> Age -> Enter -> Mark -> Enter\n");
+ name = read_line();
+ age = intger_input();
+ mark = intger_input();
+
+ list->put_student(list, name, age, mark);
+ tmp = list->find(list, name);
+
+ printf("Student's name: %s, Age: %d, Mark: %d\n", tmp->name(tmp),
+ tmp->age(tmp), tmp->mark(tmp));
+ free(name);
+ age = 0;
+ mark = 0;
+ break;
+ case 3:
+ printf("Give a name of the student to search it up (CASE SENSITIVE): ");
+ name = read_line();
+
+ tmp = list->find(list, name);
+ if (tmp == NULL) {
+ printf("Doesn't Exist!\n");
+ } else {
+ printf("STUDENT FOUND!\n");
+ printf("Student's name: %s, Age: %d, Mark: %d\n", tmp->name(tmp),
+ tmp->age(tmp), tmp->mark(tmp));
+ }
+ free(name);
+ break;
+ case 4:
+ list = list->sort(list);
+ if (list == NULL) {
+ printf("No student available\n");
+ } else {
+ printf("SORTED:\n");
+ list->dump(list);
+ }
+ break;
+
+ case 5:
+ if (list->size(list) == 0) {
+ printf("No student available!, try a Entering some: ");
+ }
+ list->dump(list);
+ break;
+
+ case 6:
+ if (list->size(list) == 0) {
+ printf("No student available!, try a Entering some: ");
+ }
+
+ tmp = list->highest(list);
+ printf("The best student is: %s, Age: %d, Mark, %d\n", tmp->name(tmp),
+ tmp->age(tmp), tmp->mark(tmp));
+ break;
+
+ default:
+ printf("Invalid command, try a command[1 for help]: ");
+ }
+ }
+
list->destructor(list);
-
+
return 0;
} \ No newline at end of file