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>
- typedef struct node *position;
- typedef struct node
- {
- int num;
- position next;
- }_NODE;
- void ReadFromFile(position);
- void Print(position);
- void Union(position, position, position);
- int main(int argc, char *argv[])
- {
- _NODE L1,L2,PRS,UN;
- L1.next=NULL;
- L2.next=NULL;
- PRS.next = NULL;
- ReadFromFile(&L1);
- Print(L1.next);
- ReadFromFile(&L2);
- Print(L2.next);
- printf("\n");
- Union(&UN, L1.next, L2.next);
- Print(UN.next);
- return 0;
- }
- void ReadFromFile(position p)
- {
- FILE *input;
- char file[50];
- position tmp,z;
- printf("\nDatoteka:");
- scanf(" %s", &file);
- input = fopen(file, "r");
- if(input == NULL)
- {
- printf("Datoteka nije otvorena.");
- }
- else
- {
- while(!feof(input))
- {
- z=(position) malloc(sizeof(_NODE));
- if(z==NULL)
- printf("Greska u alokaciji memorije.");
- else
- {
- fscanf(input," %d", &z->num);
- tmp=p;
- while(tmp->next != NULL && tmp->next->num < z->num)
- tmp = tmp->next;
- z->next = tmp->next;
- tmp->next = z;
- }
- }
- fclose(input);
- }
- }
- void Print(position p)
- {
- while (p != NULL)
- {
- printf("%d\t",p->num);
- p=p->next;
- }
- }
- void Union(position un, position p, position q)
- {
- position z,tmp;
- while (p != NULL && q != NULL)
- {
- z = (position)malloc(sizeof(_NODE));
- if (z == NULL)
- {
- printf("Memory allocation error!");
- break;
- }
- else
- {
- if (p->num < q->num)
- {
- z->num = p->num;
- z->next = un->next;
- un->next = z;
- p = p->next;
- }
- else if (p->num > q->num)
- {
- z->num = q->num;
- z->next = un->next;
- un->next = z;
- q = q->next;
- }
- else
- {
- z->num = p->num;
- z->next = un->next;
- un->next = z;
- p = p->next;
- q = q->next;
- }
- if (z->num == un->num)
- {
- un->next = z->next;
- free(z);
- }
- }
- }
- if (p == NULL)
- {
- tmp = q;
- while (tmp != NULL)
- {
- z = (position)malloc(sizeof(_NODE));
- if (z == NULL)
- {
- printf("Memory allocation error!.");
- break;
- }
- else
- {
- z->num = tmp->num;
- z->next = un->next;
- un->next = z;
- tmp = tmp->next;
- }
- }
- }
- if (q == NULL)
- {
- tmp = p;
- while (tmp != NULL)
- {
- z = (position)malloc(sizeof(_NODE));
- if (z == NULL)
- {
- printf("Memory allocation error!");
- break;
- }
- else
- {
- z->num = tmp->num;
- z->next = un->next;
- un->next = z;
- }
- tmp = tmp->next;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement