现在的位置: 首页 > 综合 > 正文

从文件读取层次结构,并用嵌套链表存储

2013年10月14日 ⁄ 综合 ⁄ 共 2509字 ⁄ 字号 评论关闭

最近遇到一个了问题,就是要从普通文本文件中读取数据,这些数据是自定义的格式的层次结构,要把这些数据转化成嵌套的链表存储起来。

数据格式如下:

begin
111
begin
222
begin
333
end
444
begin
555
end
666
end
777
begin
888
end
999
begin
000
end
aaa
end

如果用缩进表示就是下面的样子了:

begin
       111
           begin
           222
                 begin
                 333
                 end
           444
                 begin
                 555
                 end
          666
          end
     777
          begin
          888
          end
     999
          begin
          000
          end
     aaa
end

 

我的解析程序如下:

  1. #include <string>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <stack>
  5. #include <list>
  6. using namespace std;
  7. typedef struct tagNode Node;
  8. typedef struct tagNode
  9. {
  10.     string          str;
  11.     list<Node*>*    pSubList;
  12. } Node;
  13. void PrintList(list<Node*>* pList, int* piCnt)
  14. {
  15.     ++*piCnt;
  16.     for (list<Node*>::iterator it = pList->begin(); it != pList->end(); ++it)
  17.     {
  18.         for (int i = 0; i < *piCnt; ++i)
  19.             cout << "/t";
  20.         cout << (*it)->str << endl;
  21.         if ((*it)->pSubList != NULL)
  22.         {
  23.             PrintList((*it)->pSubList, piCnt);
  24.             --*piCnt;
  25.         }
  26.     }
  27. }
  28. int main()
  29. {
  30.     ifstream cin("data");
  31.     stack<Node*>    stk;
  32.     list<Node*>*    pList       = NULL;
  33.     string          str;
  34.     Node*           pParent     = NULL;
  35.     Node*           pNew        = NULL;
  36.     cin >> str;
  37.     if (str == "begin")
  38.     {
  39.         cin >> str;
  40.         pNew    = new Node;
  41.         pNew->str       = str;
  42.         pNew->pSubList  = NULL;
  43.         pList   = new list<Node*>;
  44.         pList->insert(pList->begin(), pNew);
  45.         stk.push(pNew);
  46.     }
  47.     while (!stk.empty())
  48.     {
  49.         cin >> str;
  50.         if (str == "begin")
  51.         {
  52.             cin >> str;
  53.             pNew    = new Node;
  54.             pNew->str       = str;
  55.             pNew->pSubList  = NULL;
  56.             pParent = stk.top();
  57.             pParent->pSubList = new list<Node*>;
  58.             pParent->pSubList->insert(pParent->pSubList->begin(), pNew);
  59.             stk.push(pNew);
  60.         }
  61.         else if (str == "end")
  62.         {
  63.             stk.pop();
  64.         }
  65.         else
  66.         {
  67.             stk.pop();
  68.             pNew    = new Node;
  69.             pNew->str       = str;
  70.             pNew->pSubList  = NULL;
  71.             if (stk.size() > 0)
  72.             {
  73.                 pParent = stk.top();
  74.                 pParent->pSubList->insert(pParent->pSubList->end(), pNew);
  75.             }
  76.             else
  77.             {
  78.                 pList->insert(pList->end(), pNew);
  79.             }
  80.             stk.push(pNew);
  81.         }
  82.     }
  83.     int iCnt = -1;
  84.     PrintList(pList, &iCnt);
  85.     return 0;
  86. }

运行结果如下:

抱歉!评论已关闭.