Lista simpla inlantuita
user_1419384
c_cpp
20 days ago
5.0 kB
3
Indexable
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <malloc.h> #include <string.h> #include <stdlib.h> #define LINESIZE 128 typedef struct Student { int anulDeNastere; char* nume; int nrNote; float* note; } Student; Student adaugareStudent(int _anulDeNastere, const char*_nume, int _nrNote, float* note ) { Student s; s.anulDeNastere = _anulDeNastere; s.nume = malloc((strlen(_nume) + 1)* sizeof(char) ); strcpy(s.nume, _nume); s.nrNote = _nrNote; if(s.nrNote > 0 && note !=NULL){ s.note = malloc(s.nrNote * sizeof(float)); for (int i = 0; i < s.nrNote; i++) s.note[i] = note[i]; } else { s.nrNote = 0; s.note = NULL; } return s; } typedef struct Node Node; struct Node { Student student; Node* next; }; void insertAtEnd(Node** start, Student student) { Node* newNode = malloc(sizeof(Node)); newNode->next = NULL; newNode->student = student; if (*start) { Node* aux = *start; while (aux->next) aux = aux->next; aux->next = newNode; } else { *start = newNode; } } void insertAtBeginning(Node** start, Student student) { Node* newNode = malloc(sizeof(Node)); newNode->next = *start; newNode->student = student; *start = newNode; } Student stergereStudentInceput(Node** start) { if (*start) { Node* nodSters = *start; Student studentSters = nodSters->student; *start = (*start)->next; free(nodSters); return studentSters; } else { printf("Lista este goala!"); Student err = { .anulDeNastere = 0, .nume = "err",.nrNote = 0, .note = NULL }; return err; } } Student stergereStudentFinal(Node** start) { if (*start) { //Un singur nod if ((*start)->next == NULL) { Node* nodSters = *start; Student studentSters = (*start)->student; *start = NULL; free(nodSters); return studentSters; } else {//mai multe noduri Node* nodCurent = *start; while (nodCurent->next->next != NULL) nodCurent = nodCurent->next; Node* nodSters = nodCurent->next; Student studentSters = nodSters->student; nodCurent->next = NULL; free(nodSters); return studentSters; } } else { printf("Lista este goala !"); Student err = { .anulDeNastere = 0, .nume="Err", .nrNote=0,.note=NULL}; return err; } } void printStudent(Student student) { printf("Studentul %s nascut in anul %i cu cele %i note:", student.nume, student.anulDeNastere, student.nrNote); for (int i = 0; i < student.nrNote; i++) printf("%.2f ", student.note[i]); printf("\n"); } void parseListPrint(Node* start) { while (start) { printStudent(start->student); start = start->next; } } Student citireStudentFisier(char* buffer) { //Creez o structure de tip Student Student student; //Populez structura //cream tokenurile char* token; token = strtok(buffer, ","); student.anulDeNastere = atoi(token); token = strtok(NULL, ","); student.nume = malloc( (strlen(token)+1)*sizeof(char) ); strcpy(student.nume, token); token = strtok(NULL, ","); student.nrNote = atoi(token); if (student.nrNote > 0) { student.note = malloc(student.nrNote * sizeof(float)); for (int i = 0; i < student.nrNote; i++) { token = strtok(NULL, ","); student.note[i] = atof(token); } } else { student.nrNote = 0; student.note = NULL; } return student; } void citireStudentiFisier(Node** Start, int* nrStudenti, const char* fileName) { FILE* f = fopen(fileName, "r"); char buffer[LINESIZE]; if (f) { while (fgets(buffer,LINESIZE,f)) { insertAtEnd(Start, citireStudentFisier(buffer)); (*nrStudenti)++; } fclose(f); } else printf("Fisierul %s nu s-a putut deschide",fileName); } int main() { int nrStudenti=0; Node* list = NULL; float Note[] = { 9.3,4.3,9 }; citireStudentiFisier(&list,&nrStudenti,"Studenti.txt"); parseListPrint(list); printf("%d studenti au fost cititi din fisier ", nrStudenti); insertAtEnd(&list, adaugareStudent(2004, "Iordachi Ionut", 3, Note)); nrStudenti++; printf("\n------Dupa Modificare (adaugare final )-----------\n"); parseListPrint(list); //insertAtBeginning insertAtBeginning(&list, adaugareStudent(2001, "Iordachi Bianca", 3, Note)); printf("\n------Dupa Modificare (adaugare la inceput )-----------\n"); parseListPrint(list); // // //printf("\n------Dupa Modificare (stergere inceput )-----------\n"); //Student studentExmatriculat = stergeStudentDeLaInceput(&list); nrStudenti--; //printf("\n------studentul sters :"); printStudent(studentExmatriculat); //parseListPrint(list); printf("\n------Dupa Modificare (stergere final )-----------\n"); Student studentExmatriculat2 = stergereStudentFinal(&list); nrStudenti--; printf("\n------studentul sters :"); printStudent(studentExmatriculat2); printf("\n"); parseListPrint(list); return 0; }
Editor is loading...
Leave a Comment