将数字文件转成链表

有一个文件里面都是一行一行的数字,用空格分开,请把它每行转化成一个链表。

注意getline(ifstream, line)得到一行,而且使用了头结点。

https://gist.github.com/Jiacai/9431341

#include <string>
#include <fstream>
#include <iostream>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int _val) {
        val = _val;
        next = NULL;
    }
};

/*
    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");
}

第二次写,数组里保存头结点:

#include <string>
#include <fstream>
#include <iostream>
using namespace std;

struct Node
{
	int val;
	Node *next;
};

void readFile(string fileName, Node** lineHead, int lenLimit)
{
	fstream f(fileName);
	if (!f) {
		exit(1);
	}
	string line;
	int lineIdx = 0;
	while (getline(f, line) && lineIdx < lenLimit)
	{
		lineHead[lineIdx] = new Node();
		Node * current = lineHead[lineIdx];
		// skip spaces at first
		int i = 0;

		while (i != line.length())
		{
			while (i < line.length() && line[i] == ' ')
			{
				i++;
			}

			if (i == line.length()) continue;

			int num = 0;
			// get a number
			while (i != line.length() && line[i] != ' ')
			{
				num = num * 10 + (line[i] - '0');
				i++;
			}
			Node * node = new Node();
			node->val = num;
			node->next = NULL;
			current->next = node;
			current = node;
		}
		lineIdx++;
	}
	f.close();
}

int main()
{
	const int LINE_COUNT = 100;
	Node* list[LINE_COUNT];
	memset(list, 0, sizeof(list));
	readFile("d:\test.txt", list, LINE_COUNT);
	for (int i = 0; i < LINE_COUNT; i++) {
		if (list[i] == NULL) break;
		Node* current = list[i]->next;
		if (current == NULL)
		{
			cout << "NULL" << endl;
			continue;
		}
		while (current != NULL) {
			cout << current->val << "->";
			current = current->next;
		}
		cout << "NULL" << endl;
	}
	system("pause");
	return 0;
}

  

原文地址:https://www.cnblogs.com/lautsie/p/3588733.html