将数字文件转成链表

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

注意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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值