• 将数字文件转成链表


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

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

      

  • 相关阅读:
    Google analytics平均页面停留时间为何是0
    用易语言拦截窗口消息(带例程)
    OS + Linux RHEL / RedHat Enterprise 5 / 6 / 6.3
    Oracle学习教程:动态SQL与游标详解
    图解Oracle数据库(二)
    Oracle数据库中listener.ora sqlnet.ora tnsnames.ora的区别
    正确理解javascript的this关键字
    深入理解Javascript之this关键字
    Oracle中动态SQL详解
    oracle维护常用SQL语句(查看系统表和视图)
  • 原文地址:https://www.cnblogs.com/lautsie/p/3588733.html
Copyright © 2020-2023  润新知