Skip to content

Instantly share code, notes, and snippets.

@bchaber
Created November 19, 2024 11:04
Show Gist options
  • Save bchaber/c2dc0dfd4319ee37d9ea2bed0e806bb4 to your computer and use it in GitHub Desktop.
Save bchaber/c2dc0dfd4319ee37d9ea2bed0e806bb4 to your computer and use it in GitHub Desktop.
Program do zarządzania studentami
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef struct student {
int uid; // unique identifier
float gpa; // grade point average
char *firstname;
char *lastname;
} student_t;
typedef struct list {
struct student *stud;
struct list *next;
} list_t;
list_t* init(student_t *stud) {
list_t *head = malloc(sizeof(list_t));
if (head == NULL) {
fprintf(stderr, "[!] Nie udało się zainicjalizować listy\n");
return NULL;
}
head->stud = stud;
head->next = NULL;
return head;
}
size_t length(list_t *head) {
list_t *last = head;
size_t n = 0;
while (last != NULL) {
n += 1;
last = last->next;
}
return n;
}
int push(list_t *head, student_t *stud) {
list_t *last = head;
list_t *tail;
if (head == NULL) {
fprintf(stderr, "[!] Niezainicjalizowana lista\n");
return 1;
}
while (last->next != NULL) {
last = last->next;
}
last->next = malloc(sizeof(list_t));
if (last->next == NULL) {
fprintf(stderr, "[!] Nie udało się dodać elementu do listy\n");
return 2;
}
tail = last->next;
tail->stud = stud;
tail->next = NULL;
return 0;
}
/* Poprzednia implementacja funkcji
student_t* pop(list_t *head) {
list_t *last = NULL;
list_t *tail = head;
if (head == NULL) {
fprintf(stderr, "[!] Lista jest pusta\n");
return NULL;
}
if (head->next == NULL) {
fprintf(stderr, "[!] Lista zawiera tylko jeden element\n");
return NULL;
}
while (tail->next != NULL) {
last = tail;
tail = tail->next;
}
student_t *stud = tail->stud;
free(tail);
last->next = NULL;
if (stud != NULL) {
printf("[<] (%d) %s %s\n", stud->uid, stud->firstname, stud->lastname);
} else {
printf("[<] (nothing)\n");
}
return stud;
}
*/
list_t* pop(list_t *head, student_t* output) {
list_t *tail = head;
list_t *last = NULL;
if (head == NULL) {
fprintf(stderr, "[!] Nie zainicjalizowana lista\n");
return head;
}
while (tail->next != NULL) {
last = tail;
tail = tail->next;
}
if (output == NULL) {
fprintf(stderr, "[!] Nie mam gdzie zapisać wyniku\n");
return head;
}
if (tail->stud == NULL) {
fprintf(stderr, "[!] Brak informacji o studencie\n");
return head;
}
// FIXME: what if tail->stud and output overlap?
memcpy(output, tail->stud, sizeof(student_t));
printf("[<] (%d) %s %s\n", output->uid, output->firstname, output->lastname);
// tail->stud is managed by other function
free(tail);
if (last != NULL) {
last->next = NULL;
return head;
} else {
return NULL;
}
}
void print(list_t *head) {
list_t *last = head;
printf("[*] Lista ma %zu elementów\n", length(head));
while (last != NULL) {
printf("[%p] stud => %p\n", (void*)last, (void*)last->stud);
last = last->next;
}
}
int main(int argc, char **argv) {
student_t bchaber = {207390, 3.0, "Bartosz", "Chaber"};
student_t kojak = {275981, 5.0, "Telly", "Savalas"};
student_t gal = {999999, 1.0, "Gal", "Anonim"};
student_t output = {0};
list_t *head = init(&kojak);
push(head, &gal);
push(head, &bchaber);
print(head);
head = pop(head, &output);
print(head);
head = pop(head, &output);
print(head);
head = pop(head, &output);
print(head);
head = pop(head, &output);
print(head);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment