Untitled

 avatar
unknown
plain_text
a month ago
2.6 kB
15
Indexable

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "header.h"


void inputNodeData(STUDENT* s) {
    char tempIme[50];
    char tempPrezime[50];


    scanf("%d", &s->id);
    scanf("%s", tempIme);
    scanf("%s", tempPrezime);
    scanf("%f", &s->prosjek);
    s->ime = (char*)malloc(strlen(tempIme) + 1);
    s->prezime = (char*)malloc(strlen(tempPrezime) + 1);
    strcpy(s->ime, tempIme);
    strcpy(s->prezime, tempPrezime);

    s->nextNode = NULL;
}


STUDENT* createSLList(void) {
    STUDENT* head = (STUDENT*)malloc(sizeof(STUDENT));
    if (head == NULL) {
        printf("Greska pri alokaciji memorije!\n");
        exit(1);
    }
    inputNodeData(head);
    return head;
}


STUDENT* insertNewNodeSLList(STUDENT* head) {
    STUDENT* newNode = (STUDENT*)malloc(sizeof(STUDENT));
    if (newNode == NULL) {
        printf("Greska pri alokaciji memorije!\n");
        exit(1);
    }
    inputNodeData(newNode);
    newNode->nextNode = head;
    return newNode;
}


void traverseSLList(STUDENT* head) {
    STUDENT* temp = head;
    while (temp != NULL) {
        printf("ID: %d - %s %s, prosjek: %.2f\n", temp->id, temp->ime, temp->prezime, temp->prosjek);
        temp = temp->nextNode;
    }
}


STUDENT* searchSLList(STUDENT* head, int id) {
    STUDENT* temp = head;
    while (temp != NULL) {
        if (temp->id == id)
        return temp;
        temp = temp->nextNode;
    }
    return NULL;
}


void deleteNodeSLList(STUDENT** headRef, STUDENT* target) {
    if (headRef == NULL || *headRef == NULL || target == NULL)
    return;
    STUDENT* temp = *headRef;
    if (temp == target) {
        *headRef = temp->nextNode;
    }
    else{
        STUDENT* prev = NULL;
        while (temp != NULL && temp != target) {
            prev = temp;
            temp = temp->nextNode;
        }
        if (temp == NULL) return;
        prev->nextNode = temp->nextNode;
    }

    free(target->ime);
    free(target->prezime);
    free(target);
}


STUDENT* deleteWholeSLList(STUDENT* head) {
    STUDENT* temp;
    while (head != NULL) {
        temp = head;
        head = head->nextNode;
        free(temp->ime);
        free(temp->prezime);
        free(temp);
    }
    return NULL;
}


float prosjekSvih(STUDENT* head) {
    if (head == NULL) return 0.0f;
    int count = 0;
    float sum = 0.0f;
    STUDENT* temp = head;
    while (temp != NULL) {
        sum += temp->prosjek;
        count++;
        temp = temp->nextNode;
    }
    return (count > 0) ? (sum / count) : 0.0f;
}
    }
}
    }
}
        }
    }
    }
}
    }
}
    }
}
    }
}
    }
}
}
Editor is loading...
Leave a Comment