Advertisement
Crackbone

2

Oct 18th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.64 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<ctype.h>
  5.  
  6. struct _student;
  7. typedef struct _student* Position;
  8. struct _student
  9. {
  10.     char ime[128];
  11.     char prezime[128];
  12.     int godina_rodenja;
  13.     Position Next;
  14. };
  15.  
  16. void Insert(Position);
  17. void PrintList(Position);
  18. void InsertEnd(Position);
  19. Position FindPrez(Position);
  20. void DeletePrez(Position);
  21. void InsertAfter(Position);
  22. Position FindPrevPrez(Position);
  23. void InsertBefore(Position);
  24. void ReadFromFile(Position);
  25. void PrintToFile(Position);
  26. void SortList(Position);
  27.  
  28. void main(void)
  29. {
  30.     struct _student head;
  31.     Position studentF = NULL;
  32.     char izbor = 0;
  33.  
  34.     head.Next = NULL;
  35.  
  36.     while(toupper(izbor) != 'K')
  37.     {
  38.         printf("****************************************\r\n");
  39.         printf("***   VJEZBA BR. 02  -  Vezane liste ***\r\n");
  40.         printf("****************************************\r\n");
  41.         printf("\r\n\r\n");
  42.         printf("\t1. Unos\r\n");
  43.         printf("\t2. Ispis\r\n");
  44.         printf("\t3. Unos na kraj\r\n");
  45.         printf("\t4. Pronadji po prezimenu\r\n");
  46.         printf("\t5. Izbrisi po prezimenu\r\n");
  47.         printf("\t6. Unos nakon studenta\r\n");
  48.         printf("\t7. Unos prije studenta\r\n");
  49.         printf("\t8. Sortiraj\r\n");
  50.         printf("\t9. Ucitaj listu iz datoteke\r\n");
  51.         printf("\tA. Ispisi listu u datoteku\r\n");
  52.         printf("\tK. Izlaz iz programa\r\n");
  53.         printf("\r\n\tIzbor : ");
  54.  
  55.         scanf_s(" %c", &izbor, 1);
  56.  
  57.         switch(izbor)
  58.         {
  59.         case '1' :
  60.             Insert(&head);
  61.             break;
  62.         case '2' :
  63.             PrintList(head.Next);
  64.             break;
  65.         case '3' :
  66.             InsertEnd(&head);
  67.             break;
  68.         case '4' :
  69.             studentF = FindPrez(head.Next);
  70.             if(studentF != NULL)
  71.                 printf("\r\nTrazeni student je :\r\n%s %s %d\r\n", studentF->ime, studentF->prezime, studentF->godina_rodenja);
  72.             else
  73.                 printf("\r\nStudent s trazenim prezimenom nije pronaden\r\n");
  74.             break;
  75.         case '5' :
  76.             DeletePrez(&head);
  77.             PrintList(head.Next);
  78.             break;
  79.         case '6' :
  80.             InsertAfter(&head);
  81.             PrintList(head.Next);
  82.             break;
  83.         case '7' :
  84.             InsertBefore(&head);
  85.             PrintList(head.Next);
  86.             break;
  87.         case '8':
  88.             SortList(&head);
  89.             PrintList(head.Next);
  90.             break;
  91.         case '9':
  92.             ReadFromFile(&head);
  93.             PrintList(head.Next);
  94.             break;
  95.         case 'A' :
  96.             PrintToFile(head.Next);
  97.             break;
  98.         case 'k' :
  99.         case 'K' :
  100.             break;
  101.         default:
  102.             printf("\r\nPogresan izbor <%c>.\r\nPokusajte ponovno.\r\n", izbor);
  103.             break;
  104.         }
  105.     }
  106. }
  107.  
  108. void Insert(Position P)
  109. {
  110.     Position q;
  111.  
  112.     q=(Position)malloc(sizeof(struct _student));
  113.     if(q == NULL)
  114.         printf("\r\nGRESKA!\r\nAlokacija memorije neuspjela.\r\n");
  115.     else
  116.     {
  117.         printf("Unesite ime, prezime i godinu rodenja studenta :\r\n");
  118.         scanf_s(" %s %s %d", q->ime, 128, q->prezime, 128, &q->godina_rodenja);
  119.  
  120.         q->Next = P->Next;
  121.         P->Next = q;
  122.     }
  123. }
  124.  
  125. void PrintList(Position P)
  126. {
  127.     if(P == NULL)
  128.         printf("\r\nLista je prazna!\r\n");
  129.     else
  130.     {
  131.         printf("\r\n\r\nU listi se nalaze : \r\n");
  132.         while(P != NULL)
  133.         {
  134.             printf("\t%s %s %d\r\n", P->ime, P->prezime, P->godina_rodenja);
  135.             P = P->Next;
  136.         }
  137.     }
  138.     printf("\r\n\r\n");
  139. }
  140.  
  141. void InsertEnd(Position P)
  142. {
  143.     while(P->Next != NULL)
  144.         P = P->Next;
  145.  
  146.     Insert(P);
  147. }
  148.  
  149. Position FindPrez(Position P)
  150. {
  151.     char prez[128];
  152.  
  153.     printf("\r\nUnesite prezime koje trazite : ");
  154.     scanf_s(" %s", prez, 128);
  155.     while(P != NULL && _strcmpi(P->prezime, prez) != 0)
  156.         P = P->Next;
  157.  
  158.     return P;
  159. }
  160. void DeletePrez(Position P)
  161. {
  162.     Position tmp;
  163.     char prez[128];
  164.  
  165.     printf("\r\nUnesite prezime koje zelite izbrisatie : ");
  166.     scanf_s(" %s", prez, 128);
  167.     while(P->Next != NULL && _strcmpi(P->Next->prezime, prez) != 0)
  168.         P = P->Next;
  169.  
  170.     tmp = P->Next;
  171.     if(tmp != NULL)
  172.     {
  173.         P->Next = tmp->Next;
  174.         printf("\r\nIz liste se brise : %s %s %d", tmp->ime, tmp->prezime, tmp->godina_rodenja);
  175.         free(tmp);
  176.     }
  177. }
  178.  
  179. void InsertAfter(Position P)
  180. {
  181.     Position tmp;
  182.  
  183.     tmp = FindPrez(P);
  184.  
  185.     if(tmp != NULL)
  186.         Insert(tmp);
  187. }
  188.  
  189. Position FindPrevPrez(Position P)
  190. {
  191.     char prez[128];
  192.  
  193.     printf("\r\nUnesite prezime koje trazite : ");
  194.     scanf_s(" %s", prez, 128);
  195.     while(P->Next != NULL && _strcmpi(P->Next->prezime, prez) != 0)
  196.         P = P->Next;
  197.  
  198.     if(P->Next == NULL)
  199.         P = NULL;
  200.     return P;
  201. }
  202.  
  203. void InsertBefore(Position P)
  204. {
  205.     Position tmp;
  206.  
  207.     tmp = FindPrevPrez(P);
  208.  
  209.     if(tmp != NULL)
  210.         Insert(tmp);
  211. }
  212. void ReadFromFile(Position P)
  213. {
  214.     FILE *stream;
  215.     char fname[1024];
  216.     Position q;
  217.  
  218.     printf("\r\nUnesite datoteku za citanje : ");
  219.     scanf_s(" %s", fname, 1024);
  220.  
  221.     fopen_s(&stream, fname, "r");
  222.     if(stream == NULL)
  223.         printf("\r\nDatoteka \"%s\" nije otvorena.", fname);
  224.     else
  225.     {
  226.         while(!feof(stream))
  227.         {
  228.             q = (Position)malloc(sizeof(struct _student));
  229.             if(q == NULL)
  230.                 printf("Greska!\r\nMemorija nije alocirana.");
  231.             else
  232.             {
  233.                 fscanf_s(stream, " %s %s %d", q->ime, 128, q->prezime, 128, &q->godina_rodenja);
  234.  
  235.                 q->Next = P->Next;
  236.                 P->Next = q;
  237.             }
  238.         }
  239.         fclose(stream);
  240.     }
  241. }
  242.  
  243. void PrintToFile(Position P)
  244. {
  245.     FILE *stream;
  246.     char fname[1024];
  247.  
  248.     printf("\r\nUnesite datoteku za upisivanje : ");
  249.     scanf_s(" %s", fname, 1024);
  250.  
  251.     fopen_s(&stream, fname, "w+");
  252.     if(stream == NULL)
  253.         printf("\r\nDatoteka \"%s\" nije otvorena.", fname);
  254.     else
  255.     {
  256.         while(P != NULL)
  257.         {
  258.             fprintf(stream, "%s %s %d\r\n", P->ime, P->prezime, P->godina_rodenja);
  259.             P = P->Next;
  260.         }
  261.         fclose(stream);
  262.     }
  263. }
  264.  
  265. void SortList(Position P)
  266. {
  267.     Position i, j, prev_j, end;
  268.  
  269.     end = NULL;
  270.  
  271.     while(P->Next != end)
  272.     {
  273.         i = P;
  274.         prev_j = i->Next;
  275.         j = prev_j->Next;
  276.  
  277.         while(j != end)
  278.         {
  279.             if(_strcmpi(prev_j->prezime, j->prezime) > 0)
  280.             {
  281.                 prev_j->Next = j->Next;
  282.                 j->Next = prev_j;
  283.                 i->Next = j;
  284.             }
  285.             else
  286.                 i = prev_j;
  287.             prev_j = i->Next;
  288.             j = prev_j->Next;
  289.         }
  290.         end = prev_j;
  291.     }
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement