Last active
August 29, 2015 13:57
-
-
Save sturgle/9431341 to your computer and use it in GitHub Desktop.
TurnFileWithNumberToList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 456 789 | |
321 65 98 7 | |
1 | |
1 2 3 4 | |
111111 | |
111111 222222 333333 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <fstream> | |
#include <iostream> | |
using namespace std; | |
struct ListNode { | |
int val; | |
ListNode *next; | |
ListNode(int _val) { | |
val = _val; | |
next = NULL; | |
} | |
}; | |
/* | |
the file is of lines of numbers, seperated by spaces. Please turn them into linked lists. | |
123 456 | |
*/ | |
void readFile(string fileName, ListNode** list, int lineLimit) { | |
ifstream ifstrm(fileName); | |
if(!ifstrm) { | |
exit(1); | |
} | |
int i = 0; | |
string line; | |
while (getline(ifstrm, line) && i < lineLimit) { | |
// parse this line | |
ListNode* dummy = new ListNode(0); | |
ListNode *last = dummy; | |
int idx = 0; | |
while (idx < line.length()) { | |
// skip spaces | |
while (idx < line.length() && line[idx] == ' ') { | |
idx++; | |
} | |
if (idx == line.length()) break; | |
int num = 0; | |
while (idx < line.length() && line[idx] != ' ') { | |
num = num * 10 + line[idx] - '0'; | |
idx++; | |
} | |
ListNode* node = new ListNode(num); | |
last->next = node; | |
last = node; | |
} | |
last->next = NULL; | |
list[i] = dummy->next; | |
delete dummy; | |
i++; | |
} | |
ifstrm.close(); | |
} | |
int main() { | |
const int LINE_COUNT = 100; | |
ListNode* list[LINE_COUNT]; | |
memset(list, 0, sizeof(list)); | |
readFile("d:\\test.txt", list, LINE_COUNT); | |
for (int i = 0; i < LINE_COUNT; i++) { | |
ListNode* current = list[i]; | |
if (current == NULL) break; | |
while (current != NULL) { | |
cout << current->val << "->"; | |
current = current->next; | |
} | |
cout << "NULL" << endl; | |
} | |
system("pause"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <sstream> | |
using namespace std; | |
struct LinkNode | |
{ | |
int val; | |
LinkNode* next; | |
LinkNode(int n) : val(n), next(NULL) {} | |
}; | |
struct HeadNode { | |
LinkNode *nextNode; | |
HeadNode *nextLine; | |
HeadNode() : nextNode(NULL), nextLine(NULL) {} | |
}; | |
HeadNode* buildList() { | |
string line; | |
HeadNode headDummy; | |
HeadNode *prevHead = &headDummy; | |
while (getline(cin, line)) { | |
HeadNode *head = new HeadNode(); | |
prevHead->nextLine = head; | |
LinkNode dummy(0); | |
LinkNode *prev = &dummy; | |
int i = 0; | |
while (true) { | |
while (i < line.size() && line[i] == ' ') { | |
i++; | |
} | |
if (i == line.size()) { | |
break; | |
} | |
int n = 0; | |
while (i < line.size() && line[i] != ' ') { | |
n = n * 10 + (line[i] - '0'); | |
i++; | |
} | |
LinkNode *node = new LinkNode(n); | |
prev->next = node; | |
prev = node; | |
} | |
head->nextNode = dummy.next; | |
prevHead = head; | |
} | |
return headDummy.nextLine; | |
} | |
void printList(HeadNode* head) { | |
HeadNode *lineHead = head; | |
while (lineHead != NULL) { | |
LinkNode *node = lineHead->nextNode; | |
while (node != NULL) { | |
cout << node->val << ","; | |
node = node->next; | |
} | |
cout << endl; | |
lineHead = lineHead->nextLine; | |
} | |
} | |
int main() { | |
HeadNode* head = buildList(); | |
printList(head); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment