Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
- #include <time.h>
- #define MIN_RAND 0
- #define MAX_RAND 30
- #define BROJ_RANNDOM_BROJEVA 10
- struct list{
- int broj;
- struct list* next;
- };
- struct list* CreateNode(int broj);
- int InsertAfter(struct list* head,int nodeInfo);
- int InsertLast(struct list* head,int nodeInfo);
- int IspisListe(struct list* head);
- struct list* FindNode(struct list* head, int broj);
- int DeleteNode(struct list* head,int broj);
- struct list* FindPrevNode(struct list* head, int broj);
- int InsertBefore(struct list* head,int nodeBroj,int newBroj);
- int FreeAlloData(struct list* head);
- int SortedInput(struct list* head,int node);
- int RandomFillList(struct list* head);
- int Unija(struct list* list1,struct list* list2, struct list* unija);
- int DoesNodeExist(struct list* head,int node);
- int Presjek(struct list* list1,struct list* list2, struct list* presjek);
- int main() {
- struct list list1;
- struct list list2;
- struct list unija;
- struct list presjek;
- srand((unsigned) time(NULL));
- RandomFillList(&list1);
- RandomFillList(&list2);
- Unija(&list1,&list2,&unija);
- Presjek(&list1,&list2,&presjek);
- IspisListe(&list1);
- IspisListe(&list2);
- IspisListe(&unija);
- IspisListe(&presjek);
- /*
- FreeAlloData(&list1);
- FreeAlloData(&list2);
- FreeAlloData(&unija);
- FreeAlloData(&presjek);
- */
- return 0;
- }
- struct list* CreateNode(int broj){
- struct list* returnList;
- returnList =(struct list*) malloc(sizeof(struct list));
- if(returnList != NULL) {
- returnList->broj = broj;
- returnList->next = NULL;
- }
- return returnList;
- }
- int InsertAfter(struct list* head,int nodeInfo){
- struct list* temp;
- if(head == NULL)
- return -1;
- temp = CreateNode(nodeInfo);
- temp->next = head->next;
- head->next = temp;
- return 0;
- }
- int InsertLast(struct list* head,int nodeInfo){
- if(head == NULL)
- return -1;
- while(head->next != NULL)
- head = head->next;
- InsertAfter(head,nodeInfo);
- return 0;
- }
- int IspisListe(struct list* head){
- if(head == NULL)
- return -1;
- if(head->next == NULL)
- return 0;
- head = head->next;
- while(head != NULL){
- if(head->next == NULL)
- break;
- printf("%d\t",head->broj);
- head = head->next;
- }
- printf("%d\n",head->broj);
- return 0;
- }
- struct list* FindNode(struct list* head, int broj){
- if(head == NULL)
- return NULL;
- if(head->next == NULL)
- return NULL;
- head = head->next;
- while(head->next != NULL && head->broj != broj)
- head = head->next;
- return head;
- }
- int DeleteNode(struct list* head,int broj){
- struct list* temp;
- if(head == NULL)
- return -1;
- head = FindPrevNode(head,broj);
- if(head == NULL || head->next == NULL)
- return -1;
- temp = head->next;
- head->next = temp->next;
- free(temp);
- return 0;
- }
- struct list* FindPrevNode(struct list* head, int broj){
- if(head == NULL)
- return NULL;
- if(head->next == NULL)
- return NULL;
- head = head->next;
- while(head->next != NULL && head->next->broj != broj)
- head = head->next;
- return head;
- }
- int InsertBefore(struct list* head,int nodeBroj,int newBroj){
- struct list* temp;
- if(head == NULL)
- return -1;
- temp = FindPrevNode(head,nodeBroj);
- InsertAfter(temp,newBroj);
- return 0;
- }
- int FreeAlloData(struct list* head){
- struct list* start;
- if(head == NULL)
- return -1;
- start = head;
- while(start->next != NULL){
- while(head->next != NULL){
- head = head->next;
- }
- free(head);
- head= start;
- }
- return 0;
- }
- int SortedInput(struct list* head,int node){
- if(head == NULL)
- return -1;
- while(head->next != NULL && head->next->broj < node){
- head = head->next;
- }
- InsertAfter(head,node);
- return 0;
- }
- int RandomFillList(struct list* head){
- int i =0;
- int x =0;
- if(head == NULL)
- return -1;
- for(i = 0;i<BROJ_RANNDOM_BROJEVA;i++){
- x = rand()%(MAX_RAND+1-MIN_RAND+MIN_RAND);
- SortedInput(head,x);
- }
- return 0;
- }
- int Unija(struct list* list1,struct list* list2, struct list* unija){
- int flag = 0;
- if(list1 == NULL || list2 == NULL || unija == NULL)
- return -1;
- unija->next = NULL;
- while(list1->next != NULL){
- list1 = list1->next;
- flag = DoesNodeExist(unija,list1->broj);
- if(flag == 0)
- SortedInput(unija,list1->broj);
- flag = 1;
- }
- while(list2->next != NULL){
- list2 = list2->next;
- flag = DoesNodeExist(unija,list2->broj);
- if(flag == 0)
- SortedInput(unija,list2->broj);
- flag = 1;
- }
- return 0;
- }
- int DoesNodeExist(struct list* head,int node){
- if(head == NULL)
- return -1;
- if(head->next == NULL)
- return 0;
- head = head->next;
- while(head->next != NULL){
- head = head->next;
- if(head->broj == node)
- return 1;
- }
- return 0;
- }
- int Presjek(struct list* list1,struct list* list2, struct list* presjek){
- int flagL = 0;
- int flagP = 0;
- if(list1 == NULL || list2 == NULL || presjek == NULL)
- return -1;
- presjek->next = NULL;
- while(list1->next != NULL){
- list1 = list1->next;
- flagL = DoesNodeExist(list2,list1->broj);
- flagP = DoesNodeExist(presjek,list1->broj);
- if(flagL == 1){
- if(flagP == 0)
- SortedInput(presjek,list1->broj);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement