sdd lista dublu inlantuita
user_1419384
c_cpp
8 months ago
6.0 kB
5
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;
typedef struct DoublyLinkedList DLList;
struct Node {
Student student;
Node* next;
Node* prev;
};
struct DoublyLinkedList {
Node* start;
Node* end;
};
void insertAtEnd(DLList* list, Student student) {
//Cream noul nod comform studentului nou
Node* newNode = malloc(sizeof(Node));
newNode->student = student;
//Localizam nodul
newNode->next = NULL;
newNode->prev = list->end;
if (list->start) {
list->end->next = newNode;
} else {
list->start = newNode;
}
list->end = newNode;
}
void insertAtBeginning(DLList* list, Student student) {
//Alocam noul nod cu toate info de rigoare
Node* newNode = malloc(sizeof(Node));
newNode->student = student;
//Localizam newNode
newNode->next = list->start;
newNode->prev = NULL;
//daca avem noduri
if (list->start) {
list->start->prev = newNode;
}
else {
//daca n-avem noduri, acesta o sa fie si end
list->end = newNode;
}
list->start = newNode;
}
Student stergereStudentInceput(DLList* list) {
if (list->start) {
Node* nodSters = list->start;
Student studentSters = (*list->start).student;
if (list->start->next) {
//avem mai mult de 1 nod
list->start = list->start->next;
list->start->prev = NULL;
return studentSters;
}
else {
//avem doar 1 nod
list->start = NULL;
list->end = NULL;
return studentSters;
}
free(nodSters);
}
else {
//Nu avem noduri / studenti
printf("lista este goala! ");
Student err = { .anulDeNastere = 0,.nume = "ERRR",.nrNote = 0,.note = NULL };
return err;
}
}
Student stergereStudentFinal(DLList* list) {
if (list->start) {
//avem cel putin un nod
//salvam nodul si info
Node* nodSters = list->end;
Student studentSters = (*nodSters).student;
if (list->end->prev) {
//exista cel putin 2 noduri
list->end = list->end->prev;
list->end->next = NULL;
}
else {
//este doar un nod
list->start = NULL;
list->end = 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(DLList list) {
while (list.start) {
printStudent((*list.start).student);
list.start = list.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(DLList* list, int* nrStudenti, const char* fileName) {
FILE* f = fopen(fileName, "r");
char buffer[LINESIZE];
if (f) {
while (fgets(buffer,LINESIZE,f)) {
insertAtEnd(list, citireStudentFisier(buffer));
(*nrStudenti)++;
}
fclose(f);
}
else printf("Fisierul %s nu s-a putut deschide",fileName);
}
void cautareDupaAnNastere(DLList list, int an ) {
if (list.start) {
Node* nodCurent = list.start;
while ( nodCurent ) {
if ((*nodCurent).student.anulDeNastere == an) {
printf("S-a gasit : "); printStudent((*nodCurent).student);
}
nodCurent = nodCurent->next;
}
}
else printf("Lista este goala, nu s-a putut initializa cautarea");
}
int main() {
int nrStudenti=0;
DLList list;
list.end = NULL;
list.start = 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(&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 = stergereStudentInceput(&list); nrStudenti--;
printf("\n------studentul sters :"); printStudent(studentExmatriculat); printf("\n");
parseListPrint(list);
printf("\n------Dupa Modificare (stergere final )-----------\n");
Student studentExmatriculat2 = stergereStudentFinal(&list); nrStudenti--;
printf("\n------studentul sters :"); printStudent(studentExmatriculat2); printf("\n");
parseListPrint(list);
int an = 2008;
cautareDupaAnNastere(list, an);
return 0;
}Editor is loading...
Leave a Comment