Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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 main(int agrc, char *argv[])
- {
- char izbor = 0;
- char prezime[STRING_MAX_LENGTH];
- 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 '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("\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: ");
- scanf(" %s", tempStudent.ime);
- printf("Unesi prezime: ");
- scanf(" %s", tempStudent.prezime);
- printf("Unesi godinu rodenja: ");
- scanf(" %d", &tempStudent.godinaRodenja);
- return tempStudent;
- }
- void FreeAlocatedData(Position head)
- {
- free(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement