Repository navigation
Expand file tree
/
Copy pathDataFactory.cpp
More file actions
122 lines (93 loc) · 2.14 KB
/
Copy pathDataFactory.cpp
File metadata and controls
122 lines (93 loc) · 2.14 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
//
// Created by Gino Tateo on 4/28/22.
//
#include "DataFactory.h"
#include <iostream>
Factory::Factory() {
head = NULL;
DataSize = 0;
}
void Factory::read(){
std::ifstream file;
file.open ("read.txt");
int line;
if (file.is_open())
{
if(head==NULL) {
Node *NewNode = new Node;
head = NewNode;
file >> line;
head->data = line;
head->next = NULL;
DataSize++;
}
Node *curr = head;
while (file >> line) {
curr->next = new Node;
curr = curr->next;
curr->data = line;
curr->next = NULL;
DataSize++;
}
}
file.close();
}
void Factory::write() {
std::ofstream file;
file.open ("write.txt");
Node* curr = head;
int line;
if (file.is_open())
{
for (int i = 0;i<DataSize;i++) {
line = curr->data;
file << line << "\n";
curr = curr->next;
}
}
file.close();
}
void Factory::dataMachine() {
int setData = 1000; // Default 1000 ints
std::cout << "Enter data size: ";
std::cin >> setData;
// while(DataSize<setData){
// data[setData] = rand()%1000;
// DataSize++;
// }
// singly linked list for data
if(head==NULL) {
Node *NewNode = new Node;
head = NewNode;
head->next = NULL;
head->data = rand()%1000;
DataSize++;
}
Node *curr = head;
while(DataSize<setData){
curr->next = new Node;
curr = curr->next;
curr->data = rand()%1000;
curr->next = NULL;
DataSize++;
}
}
void Factory::selectionSort() {
Node* temp = head;
// Traverse the List
while (temp) {
Node* min = temp;
Node* r = temp->next;
// Traverse the unsorted sublist
while (r) {
if (min->data > r->data)
min = r;
r = r->next;
}
// Swap Data
int x = temp->data;
temp->data = min->data;
min->data = x;
temp = temp->next;
}
}