Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #define MAX_NAZIV 30
- #define MAX 15
- #define MIN 1
- typedef struct student*Position;
- typedef struct student
- {
- int brojIndexa;
- char *ime;
- char *prezime;
- int orderNum;
- Position next;
- }Student;
- Position KreirajClan();
- int CitanjeIzFilea(Position);
- int RandomBroj();
- int FreeData(Position);
- int IspisListe(Position);
- int SortiranjeUnosa(Position, int, char*, char*, int);
- int BrisanjeIstih(Position);
- int IzbrisiClanove(Position , int );
- int SortirajClanove(Position);
- int main()
- {
- Student root;
- root.next = NULL;
- srand((unsigned)time(NULL));
- printf("\t------Lista studenata------\n\n");
- CitanjeIzFilea(&root);
- IspisListe(&root);
- printf("\n\n\t------Lista studenata s jedistvenim orderNumb------\n\n");
- BrisanjeIstih(&root);
- IspisListe(&root);
- printf("\n\n\t------Lista studenata s jedistvenim orderNumb (sortirana)------\n\n");
- SortirajClanove(&root);
- IspisListe(&root);
- printf("\n\n\t------Brisanje liste------\n\n");
- FreeData(&root);
- IspisListe(&root);
- return 0;
- }
- int CitanjeIzFilea(Position p)
- {
- FILE*fp;
- Position newItem;
- int broj=0;
- int rndBr = 0;
- char *ime=(char*)malloc(sizeof(char)*MAX_NAZIV);
- char *prezime = (char*)malloc(sizeof(char)*MAX_NAZIV);
- fp = fopen("studenti.txt", "r");
- if (fp == NULL)
- {
- printf("Datoteka se nije uspjesno otvorila");
- return -1;
- }
- while (!feof(fp))
- {
- ime = (char*)malloc(MAX_NAZIV * sizeof(char));
- prezime = (char*)malloc(MAX_NAZIV * sizeof(char));
- fscanf(fp, "%d %s %s",&broj, ime,prezime);
- rndBr = RandomBroj();
- if (p->next == NULL)
- {
- Position newItem;
- newItem = KreirajClan();
- newItem->brojIndexa = broj;
- newItem->ime = ime;
- newItem->prezime = prezime;
- newItem->orderNum = rndBr;
- newItem->next = p->next;
- p->next = newItem;
- }
- else
- {
- SortiranjeUnosa(p, broj, ime, prezime, rndBr);
- }
- }
- fclose(fp);
- return 0;
- }
- int SortiranjeUnosa(Position p, int index, char*ime, char*prezime, int rnd)
- {
- p = p->next;
- Position newItem;
- newItem = KreirajClan();
- newItem->brojIndexa = index;
- newItem->ime = ime;
- newItem->prezime = prezime;
- newItem->orderNum = rnd;
- newItem->next = NULL;
- while (p->next!=NULL)
- {
- if (strcmp(newItem->prezime, p->next->prezime) < 0)
- {
- newItem->next = p->next;
- p->next = newItem;
- return 0;
- }
- else
- {
- p = p->next;
- }
- }
- if (newItem->next == NULL)
- {
- p->next = newItem->next;
- p->next = newItem;
- }
- return 0;
- }
- Position KreirajClan()
- {
- Position temp;
- temp = (Position)malloc(sizeof(Student));
- temp->brojIndexa = 0;
- temp->orderNum = NULL;
- return temp;
- }
- int RandomBroj()
- {
- int broj;
- broj = rand() % (MAX - MIN + 1) + MIN;
- return broj;
- }
- int BrisanjeIstih(Position p)
- {
- Position i=NULL;
- int broj;
- int brojac;
- for (broj=1; broj < 16; broj++)
- {
- i = p;
- brojac = 0;
- while (i!=NULL)
- {
- if (i->orderNum == broj)
- {
- brojac++;
- }
- i= i->next;
- }
- if (brojac > 1)
- {
- IzbrisiClanove(p, broj);
- }
- }
- return 0;
- }
- int IzbrisiClanove(Position p, int br)
- {
- Position previ = NULL;
- Position i = NULL;
- Position temp=NULL;
- while (p->next!=NULL)
- {
- if (br == p->next->orderNum)
- {
- temp = p->next;
- p->next = temp->next;
- free(temp);
- }
- else
- {
- p = p->next;
- }
- }
- return 0;
- }
- int SortirajClanove(Position p)
- {
- Position i = NULL;
- Position j = NULL;
- Position prevj = NULL;
- Position temp = NULL;
- Position end=NULL;
- i = p;
- while (i->next!=end)
- {
- prevj = i;
- j = i->next;
- while (j->next!=end)
- {
- if (j->orderNum > j->next->orderNum)
- {
- temp = j->next;
- prevj->next = temp;
- j->next = temp->next;
- temp->next = j;
- j = temp;
- }
- prevj = j;
- j=j->next;
- }
- end = j;
- }
- return 0;
- }
- int IspisListe(Position p)
- {
- if (p->next == NULL)
- {
- printf("\t Lista je prazna!!\n\n");
- return -1;
- }
- p = p->next;
- printf("\n INDEKS\tIME\t\tPREZIME\t\tORDERnumb\n\n\n");
- while (p != NULL)
- {
- printf(" %d\t%s\t\t%s\t\t%d\n", p->brojIndexa, p->ime, p->prezime, p->orderNum);
- p = p->next;
- }
- return 0;
- }
- int FreeData(Position p)
- {
- Position temp = NULL;
- while (p->next!=NULL)
- {
- temp = p->next;
- p->next = temp->next;
- free(temp);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement