-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitMerge.cpp
More file actions
126 lines (98 loc) · 2.82 KB
/
Copy pathsplitMerge.cpp
File metadata and controls
126 lines (98 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct node{
char data[15];
node *next;
};
struct node *listOne = NULL;
struct node *listTwo;
struct node *listThree = NULL;
struct node *listFour = NULL;
int i = 1;
struct node* buildList(FILE *in,char *data,struct node *&head,int &i,char test);
struct node* split(struct node *head,int i);
struct node* merge(struct node *head1,struct node *&head2);
void traverse(FILE *out,struct node *head);
int main(){
FILE *in1, *in2, *out1, *out2;
in1 = fopen("inputSplit.txt","r");
in2 = fopen("inputMerge.txt","r");
out1 = fopen("outputSplit.txt","w");
out2 = fopen("outputMerge.txt","w");
char data[15];
buildList(in1,data,listOne,i,'y');
buildList(in2,data,listThree,i,'n');
buildList(in2,data,listFour,i,'n');
listTwo = split(listOne,i);
merge(listThree,listFour);
traverse(out1,listOne);
traverse(out1,listTwo);
traverse(out2,listThree);
fclose(in1);
fclose(in2);
fclose(out1);
fclose(out2);
free(listOne);
free(listTwo);
free(listThree);
free(listFour);
}
struct node* buildList(FILE *in,char *data,struct node *&head,int &i,char test){
head = (node *)malloc(sizeof(node));
fscanf(in,"%s",data);
strcpy(head->data,data);
head->next = NULL;
if(test == 'y'){
while(fscanf(in,"%s",data) != EOF){
struct node *current = head;
while(current->next != NULL){
current = current->next;
}
current->next = (node *)malloc(sizeof(node));
current = current->next;
strcpy(current->data, data);
current->next = NULL;
i++;
}
}
else{
for(int i=1; i<5;i++){
struct node *current = head;
while(current->next != NULL){
current = current->next;
}
current->next = (node *)malloc(sizeof(node));
current = current->next;
fscanf(in,"%s",data);
strcpy(current->data, data);
current->next = NULL;
}
}
}
struct node* split(struct node *head,int i){
struct node *listTwo;
for(int e = 1; e < i/2; e++){
head = head->next;
}
listTwo = head->next;
head->next = NULL;
return listTwo;
}
struct node* merge(struct node *head1,struct node *&head2){
struct node *current = head1;
while(current->next != NULL){
current = current->next;
}
current->next = head2;
head2 = NULL;
}
void traverse(FILE *out,struct node *head){
struct node *current = head;
while (current != NULL){
fprintf(out,"%s\n",current->data);
current = current->next;
}
fprintf(out,"\n\n\n");
}