链接列表插入无法正常工作C ++

时间:2017-09-23 00:06:30

标签: c++ linked-list

我目前正在尝试插入链接列表,我不知道我做错了什么我知道我几乎就在那里似乎无法确定这个错误。打印发生在printList中,但我认为那里的逻辑非常可靠。

`

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


struct listBinTreeNode{
public:
    string chStr;
    int prob = 0;
    listBinTreeNode *next;
    listBinTreeNode *left;
    listBinTreeNode *right;


    listBinTreeNode()
    {
        chStr = "dummy";
        next = nullptr;
        left = nullptr;
        right = nullptr;

    }

    listBinTreeNode(string chStr, int prob)
    {
        this->chStr = chStr;
        this->prob = prob;
        next = nullptr;
        left = nullptr;
        right= nullptr;
    }

    listBinTreeNode(string chStr, listBinTreeNode *next, listBinTreeNode* left, listBinTreeNode* right)
    {
        this->chStr = chStr;
        this->next = next;
        this->left = left;
        this->right = right;

    }

  void  printNode(string fileOut)
    {
        ofstream outFile;
        outFile.open(fileOut);
        cout<< "chStr: " <<chStr<< "next's chStr: " << next->chStr << "left's chStr: "<< left->chStr<< "right's chStr: " << right->chStr<< endl;
        outFile<< "chStr: " <<chStr<< "next's chStr: " << next->chStr << "left's chStr: "<< left->chStr<< "right's chStr: " << right->chStr<< "\n";
        outFile.close();
    }
};


class HuffmanLinkedList{
public:
    listBinTreeNode * listHead=nullptr;
    listBinTreeNode * oldListHead=nullptr;
    listBinTreeNode dummy;
    HuffmanLinkedList()
    {   //listBinTreeNode *dummy = new listBinTreeNode();
        listHead = &dummy;
    }
  void  constructHuffmanLList(string fileIn, string fileOut)
    {
        ifstream inFile;
        inFile.open(fileIn);
        //fstream outFile.open(fileOut);
       // listBinTreeNode *dummy = new listBinTreeNode();
        listHead = &dummy;
        string word;
        int wordProb;
        listBinTreeNode  spot;
        while(inFile >> word >> wordProb)
            {
             spot = findSpot(listHead,wordProb);
            // cout<< "this is spot: "<< spot<<endl;
            // cout << "this is listHead.next: "<< listHead->next;
             listBinTreeNode *newNode = new listBinTreeNode(word, wordProb);
             listInsert(spot, *newNode);
             printList(fileOut, listHead);
            } // while
            inFile.close();
          //  outFile.close();
    }
listBinTreeNode findSpot(listBinTreeNode* head, int prob)
    {
        listBinTreeNode * walker = head;
        while( (walker->next !=nullptr) && (prob > (walker->next->prob) )){
            cout<< "here";
            walker = walker->next;
        }
        //if(walker == head){
       //     return walker->next;
      //  }
        return *walker;
    }

 void listInsert(listBinTreeNode spot, listBinTreeNode newNode)
     {
        //if(spot.next!=nullptr){
            newNode.next = spot.next;
            spot.next = &newNode;
        //}
        //spot.next = newNode;
        cout<< spot.next->chStr<< endl;
     }
 void printList(string file, listBinTreeNode* head)
   {
            ofstream outFile;
            outFile.open(file);
            listBinTreeNode* walker = head;
            while(walker->next!=nullptr){
                if(walker == head){
                    cout<<"listHead -->";
                    outFile<<"listHead -->";
                }
                if(walker->next!=nullptr){
                cout << "("<< walker->chStr << ","<<walker->prob << ","<< walker->next->chStr<<") -->";
                outFile << "("<< walker->chStr << ","<<walker->prob << ","<< walker->next->chStr<<") -->";
                walker = walker->next;
                }
            }
            cout << "(" << walker->chStr<<", NULL)"<<endl;
            outFile <<"(" << walker->chStr<<", NULL)--> NULL"<<"\n";

   }
//   outFile.close();
};

主:

int main(int argc, char **argv)
{
    HuffmanLinkedList linkedList;
    HuffmanBinaryTree binTree;
    linkedList.constructHuffmanLList(argv[1], argv[4]);
  //  binTree.constructHuffmanBinTree(linkedList, argv[6]);
 //   string code = binTree.holder.code;
 //   binTree.constructCharCode(binTree.root,code, argv[2]);
 //   binTree.preOrderTraversal(binTree.root,argv[3]);
 //   binTree.inOrderTraversal(binTree.root,argv[4]);
 //   binTree.postOrderTraversal(binTree.root,argv[5]);
    return 0;
}

的argv [1]:

d   2
f   4 
h  15
k  6 
m  47
p  25
w  1

我的输出是:

d
(dummy, NULL)
f
(dummy, NULL)
h
(dummy, NULL)
k
(dummy, NULL)
m
(dummy, NULL)
p
(dummy, NULL)
w
(dummy, NULL)

进程返回0(0x0)执行时间:0.032秒 按任意键继续。

0 个答案:

没有答案