Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.7 kB
3
Indexable
Never
Disciplina.h:

#ifndef DISCIPLINA_H
#define DISCIPLINA_H

typedef struct Disciplina {
    char denumirea[50];
    int anul;
    char facultatea[50];
    char departamentul[50];
    int numarOre;
    struct Disciplina* next;
} Disciplina;

Disciplina* creareDisciplina(char denumirea[], int anul, char facultatea[], char departamentul[], int numarOre);
void introducereDisciplina(Disciplina** head_ref);
void afisareDisciplina(Disciplina* head);
Disciplina* cautareDisciplina(Disciplina* head, char denumirea[]);
void modificareDisciplina(Disciplina* head);
Disciplina* ultimulElement(Disciplina* head);
int lungimeLista(Disciplina* head);
void interschimbareElemente(Disciplina** head_ref, char denumirea1[], char denumirea2[]);
void sortareLista(Disciplina** head_ref);
void eliberareMemorie(Disciplina** head_ref);

#endif

Disciplina.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "disciplina.h"

Disciplina* creareDisciplina(char denumirea[], int anul, char facultatea[], char departamentul[], int numarOre) {
    Disciplina* disciplina = (Disciplina*)malloc(sizeof(Disciplina));
    strcpy(disciplina->denumirea, denumirea);
    disciplina->anul = anul;
    strcpy(disciplina->facultatea, facultatea);
    strcpy(disciplina->departamentul, departamentul);
    disciplina->numarOre = numarOre;
    disciplina->next = NULL;
    return disciplina;
}

void introducereDisciplina(Disciplina** head_ref) {
    char denumirea[50], facultatea[50], departamentul[50];
    int anul, numarOre;

    printf("Introduceti denumirea disciplinei: ");
    scanf("%s", denumirea);
    printf("Introduceti anul disciplinei: ");
    scanf("%d", &anul);
    printf("Introduceti facultatea disciplinei: ");
    scanf("%s", facultatea);
    printf("Introduceti departamentul disciplinei: ");
    scanf("%s", departamentul);
    printf("Introduceti numarul de ore al disciplinei: ");
    scanf("%d", &numarOre);

    Disciplina* disciplina = creareDisciplina(denumirea, anul, facultatea, departamentul, numarOre);
    disciplina->next = *head_ref;
    *head_ref = disciplina;
}

void afisareDisciplina(Disciplina* head) {
    Disciplina* current = head;
    if (current == NULL) {
        printf("Lista este goala.\n");
        return;
    }
    while (current != NULL) {
        printf("Denumirea disciplinei: %s\n", current->denumirea);
        printf("Anul disciplinei: %d\n", current->anul);
        printf("Facultatea disciplinei: %s\n", current->facultatea);
        printf("Departamentul disciplinei: %s\n", current->departamentul);
        printf("Numarul de ore al disciplinei: %d\n", current->numarOre);
        current = current->next;
        printf("\n");
    }
}

Disciplina* cautareDisciplina(Disciplina* head, char denumirea[]) {
    Disciplina* current = head;
    while (current != NULL) {
        if (strcmp(current->denumirea, denumirea) == 0) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}

void modificareDisciplina(Disciplina* head) {
    char denumirea[50];
    int anul, numarOre;

    printf("Introduceti denumirea disciplinei pe care doriti sa o modificati: ");
    scanf("%s", denumirea);

    Disciplina* disciplina = cautareDisciplina(head, denumirea);

    if (disciplina == NULL) {
        printf("Disciplina nu a fost gasita.\n");
        return;
    }

    printf("Introduceti noul an al disciplinei: ");
    scanf("%d", &anul);
    printf("Introduceti noul numar de ore al disciplinei: ");
    scanf("%d", &numarOre);

    disciplina->anul = anul;
    disciplina->numarOre = numarOre;

}

Disciplina* ultimulElement(Disciplina* head) {
  Disciplina *current = head;
while (current != NULL && current->next != NULL) {
    current = current->next;
  }

  return current;
}

void stergereDisciplina(Disciplina** head_ref, char denumirea[]) {
    Disciplina* temp = *head_ref, *prev;

    if (temp != NULL && strcmp(temp->denumirea, denumirea) == 0) {
        *head_ref = temp->next;
        free(temp);
        return;
    }

    while (temp != NULL && strcmp(temp->denumirea, denumirea) != 0) {
        prev = temp;
        temp = temp->next;
    }

    if (temp == NULL) {
        printf("Disciplina nu a fost gasita.\n");
        return;
    }

    prev->next = temp->next;

    free(temp);
}

void stergereLista(Disciplina** head_ref) {
   Disciplina* current = *head_ref;
   Disciplina* next;

   while (current != NULL) {
       next = current->next;
       free(current);
       current = next;
   }

   *head_ref = NULL;
}

Main.c:
#include <stdio.h>
#include <stdlib.h>
#include "disciplina.h"

int main() {
    Disciplina* head = NULL;
    int optiune;

    while (1) {
        printf("\n\nMeniu:\n");
        printf("1. Introducere disciplina\n");
        printf("2. Afisare lista discipline\n");
        printf("3. Stergere disciplina\n");
        printf("4. Stergere lista discipline\n");
        printf("5. Iesire\n");

        printf("\nIntroduceti optiunea: ");
        scanf("%d", &optiune);

        switch (optiune) {
            case 1:
                introducereDisciplina(&head);
                break;
            case 2:
                afisareDisciplina(head);
                break;
            case 3:
                stergereDisciplina(&head);
                break;
            case 4:
                stergereLista(&head);
                break;
            case 5:
                exit(0);
            default:
                printf("Optiune invalida!\n");
        }
    }

    return 0;
}