Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define STRING_MAX_LENGTH (50)
- typedef struct _osoba* Position;
- typedef struct _osoba
- {
- char ime[STRING_MAX_LENGTH];
- char prezime[STRING_MAX_LENGTH];
- int godinaRodenja;
- Position next;
- }Student;
- typedef struct _studentPodaci
- {
- char ime[STRING_MAX_LENGTH];
- char prezime[STRING_MAX_LENGTH];
- int godinaRodenja;
- }StudentPodaci;
- int InsertAfter(Position head, StudentPodaci studentPodaci);
- int InsertEnd(Position position, StudentPodaci studentPodaci);
- void IspisListe(Position head);
- int DeleteElement(Position position);
- Position FindPreviousElement(Position head, char *prezime);
- int IspisIzbornika();
- void PrintElement(Position position);
- StudentPodaci GetStudentDataFromConsole();
- void FreeAlocatedData(Position head);
- int InsertBefore(Position position, StudentPodaci studentPodaci);
- int SwapElements(Position prev, Position first);
- int WriteIntoFile(Position head, char name);
- int SortList(Position head);
- int ElementCounter(Position head);
- int main(int agrc, char *argv[])
- {
- char izbor = 0;
- char prezime[STRING_MAX_LENGTH];
- char fileName[STRING_MAX_LENGTH] = "lista.txt";
- Student head;
- StudentPodaci bufferStudent;
- Position bufferPosition;
- head.next = NULL;
- while (izbor != 'K' && izbor != 'k')
- {
- IspisIzbornika();
- scanf(" %c", &izbor);
- switch (izbor)
- {
- case '1':
- bufferStudent = GetStudentDataFromConsole();
- InsertAfter(&head, bufferStudent);
- break;
- case '2':
- IspisListe(&head);
- break;
- case '3':
- bufferStudent = GetStudentDataFromConsole();
- InsertEnd(&head, bufferStudent);
- break;
- case '4':
- printf("\nUnesi prezime studenta cije podatke ispisujemo: ");
- scanf("%s", prezime);
- bufferPosition = FindPreviousElement(&head, prezime);
- if (bufferPosition != NULL)
- PrintElement(bufferPosition->next);
- else
- printf("\nElement ne postoji u listi.");
- break;
- case '5':
- break;
- case '6':
- WriteIntoFile(&head, fileName);
- break;
- case 'k':
- case 'K':
- break;
- default:
- printf("\r\nPogresan izbor <%c>.\r\nPokusajte ponovno.\r\n", izbor);
- break;
- }
- }
- FreeAlocatedData(&head);
- IspisListe(&head);
- }
- int IspisIzbornika()
- {
- printf("\r\n\r\n");
- printf("\t1. Unos elementa u listu\r\n");
- printf("\t2. Ispis liste\r\n");
- printf("\t3. Unos na kraj\r\n");
- printf("\t4. Pronadji po prezimenu\r\n");
- printf("\t5. Izbrisi po prezimenu\r\n");
- printf("\t6. Upisi u datoteku\r\n");
- printf("\tK. Izlaz iz programa\r\n");
- printf("\r\n\tIzbor : ");
- return 0;
- }
- int InsertAfter(Position position, StudentPodaci studentPodaci)
- {
- Position newItem = NULL;
- if (position == NULL)
- return -1;
- newItem = (Position)malloc(sizeof(Student));
- if (newItem == NULL)
- return -2;
- //inicijalizacija alocirane strukture
- newItem->next = NULL;
- newItem->godinaRodenja = studentPodaci.godinaRodenja;
- strcpy(newItem->ime, studentPodaci.ime);
- strcpy(newItem->prezime, studentPodaci.prezime);
- //unos u listu
- newItem->next = position->next;
- position->next = newItem;
- return 0;
- }
- int InsertEnd(Position position, StudentPodaci studentPodaci)
- {
- while (position->next != NULL)
- position = position->next;
- InsertAfter(position, studentPodaci);
- return 0;
- }
- void IspisListe(Position head)
- {
- while (head->next != NULL)
- {
- printf("%s %s %d", head->next->ime, head->next->prezime, head->next->godinaRodenja);
- printf("\n");
- head = head->next;
- }
- }
- int DeleteElement(Position position)
- {
- Position temp = NULL;
- if ((position->next != NULL || position->next->next != NULL) && position->next->next->ime != NULL)
- {
- temp = position->next;
- free(position->next);
- position = temp;
- return 1;
- }
- else
- return -1;
- }
- Position FindPreviousElement(Position head, char *prezime)
- {
- Position temp = NULL;
- if ((head->next != NULL || head->next->next != NULL) && head->next->next->prezime != NULL)
- temp = head->next;
- return temp;
- }
- void PrintElement(Position position)
- {
- if (position->next != NULL)
- printf("%s %s %d /n", position->ime, position->prezime, position->godinaRodenja);
- }
- StudentPodaci GetStudentDataFromConsole()
- {
- StudentPodaci tempStudent;
- printf("Unesi ime, prezime i godinu rodenja: \n");
- scanf(" %s %s %d", tempStudent.ime, tempStudent.prezime, &tempStudent.godinaRodenja);
- printf("\n");
- return tempStudent;
- }
- void FreeAlocatedData(Position head)
- {
- Position temp = NULL;
- temp = head;
- while (temp->next != NULL)
- {
- free(temp->ime);
- free(temp->prezime);
- free(temp->godinaRodenja);
- }
- free(head);
- }
- int SwapElements(Position prev, Position first)
- {
- Position temp = first->next;
- prev->next = first->next;
- if (temp == NULL)
- {
- return -1;
- }
- first->next = temp->next;
- temp->next = first;
- return 0;
- }
- int WriteIntoFile(Position head, char name)
- {
- FILE *f;
- f = fopen(name, "w");
- if (f == NULL)
- {
- return -1;
- }
- else
- {
- head = head->next;
- while (head != NULL)
- {
- fprintf(f, "%s %s %d \n", head->ime, head->prezime, head->godinaRodenja);
- head = head->next;
- }
- fclose(f);
- return 0;
- }
- }
- int ElementCounter(Position head)
- {
- int counter = 0;
- head->next;
- while (head->next != NULL)
- {
- counter++;
- }
- return counter;
- }
- int InsertBefore(Position position, StudentPodaci studentPodaci)
- {
- Position temp = NULL;
- if ((position->next != NULL || position->next->next != NULL))
- temp = position->next;
- InsertAfter(temp, studentPodaci);
- return 0;
- }
- int SortList(Position head)
- {
- int i = 0;
- int j = 0;
- int n = ElementCounter(head);
- for (i = 0; i < j - 1; i++)
- for (j = 0; j < i - n - 1; j++)
- SwapElements(head, head->next);
- return 0;
- }
- int ReadFromFile(Position head, char fileName)
- {
- FILE *f;
- f = fopen(fileName, "r");
- StudentPodaci temp;
- if (f == NULL)
- {
- return -1;
- }
- else
- {
- head->next;
- while(!feof(f))
- {
- fscanf(f, "%s %s %d", temp.ime, temp.prezime, &temp.godinaRodenja);
- InsertAfter(head, temp);
- }
- fclose(f);
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement