簡體   English   中英

C++ 莖葉圖。 我不明白如何制作鏈表的鏈表

[英]C++ Stem and Leaf Plot. I don't understand how to make a linked list of linked lists

我有一個項目,用鏈表制作莖葉圖,但我不斷收到我什至不明白的錯誤。 你如何制作鏈表的鏈表? 我必須以這種方式完成作業,所以我不能以其他方式完成,我必須從下面的文件中工作。 我的頂部詞干鏈表有效,但它似乎沒有訪問葉部分,並且出現以下錯誤:

“0x0000000000404050 處的指令在 0x000000000000001E 處引用。無法讀取內存。”

這是我的代碼:

* Driver.cpp
*/

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

#include "StemAndLeaf2.h"

int main()
{

 ifstream fin("input.txt");

 int x;
 unsigned short leafunit, stemunit;

 fin >> leafunit >> stemunit;

 StemAndLeaf one;
 one.setLeafUnit(leafunit);
 one.setStemUnit(stemunit);

 while (fin >> x)
 {
   one.addTerm(x);
   cout << "Insert: " << x << endl;
   cout << "StemAndLeafPlot: " << endl;
   cout << one << endl;
 }

 cout << "The smallest term: " << one.getSmallestTerm() << endl;
 cout << "The largest term: " << one.getLargestTerm() << endl;
 
   return 0;
}
/*
* SteamAndLeaf2.h
*/

#ifndef STEAMANDLEAF2_H_
#define STEAMANDLEAF2_H_

#include <math.h>
#include <list>
#include <ostream>
// DO NOT REMOVE THE LINE BELOW
/* CMSC341_TEST_INCLUDES */

#include "Stem2.h"

class StemAndLeaf
{
   // DO NOT REMOVE THE LINE BELOW
   /* CMSC341_FRIEND_CLASSES */
public:
   StemAndLeaf(){FRONT_ptr = NULL; REAR_ptr = NULL;};
   ~StemAndLeaf()
   {
       while(FRONT_ptr != NULL)
       {
           Cursor = FRONT_ptr;
           FRONT_ptr = Cursor->getNext();
           delete Cursor;
       }
   }

   unsigned short getLeafUnit() const { return leafUnit; }
   void setLeafUnit(unsigned short leafUnit) { this->leafUnit = leafUnit; }
   unsigned short getStemUnit() const { return stemUnit; }
   void setStemUnit(unsigned short stemUnit) { this->stemUnit = stemUnit; }

   /**
    * @brief Get the smallest term in the datastructure. The term MUST be reconstructed
    *
    * @return unsigned short
    */
   unsigned short getSmallestTerm() const
   {
       return ((FRONT_ptr->getValue()*10))+(FRONT_ptr->findSmallestValue()));
   }

   /**
    * @brief Get the largest term in the datastructure. The term MUST be reconstructed
    *
    * @return unsigned short
    */
   unsigned short getLargestTerm() const
   {
       return ((REAR_ptr->getValue()*10))+(REAR_ptr->findLargestValue()));
   }


   /**
    * @brief Adds the term to the data structure. Insertion MUST be ordered, both for Stem and the Leaf
    *
    * @param term the value to add
    */
   void addTerm(unsigned short term)
   {
       Insert = new Stem();
       Insert->setValue(term/10);
       Insert->setLeafList(Insert->getLeafLink());
       if(isEmpty())
       {
           Insert->setLeafCount(1);
           Insert->insert(term%10);
           FRONT_ptr = Insert;
           REAR_ptr = Insert;
       }
       else
       {
           for(Cursor = FRONT_ptr; Cursor != NULL; Prev = Cursor, Cursor = Cursor->getNext())
           {
               Insert->setLeafCount(1);
               Insert->insert(term%10);
               if((Cursor == FRONT_ptr) && (Cursor->getValue() > Insert->getValue()))
               {
                   Insert->setNext(Cursor);
                   FRONT_ptr = Insert;
               }
               else if((Cursor == REAR_ptr) && (Cursor->getValue() < Insert->getValue()))
               {
                   Cursor->setNext(Insert);
                   REAR_ptr = Insert;
               }
               else if((Prev->getValue() < Insert->getValue()) && (Cursor->getValue() > Insert->getValue()))
               {
                   Prev->setNext(Insert);
                   Insert->setNext(Cursor);
               }
               else if(Insert->getValue() == Cursor->getValue())
               {
                   if(!Cursor->insert(term%10))
                   {
                       Cursor->incrementLeafCount();
                   }
               }
               break;
           }
       }
   }

   /*
    * Used to display/debug the StemAndLeaf diagram
    * will look exactly like this (using example in project):
    *
    * 0 | 0:1
    * ...
    * 2 |
    * 3 | 6:2 9:1
    *
    * You will not be given too much data to overrun 80 characters
    */
   friend std::ostream &operator<<(std::ostream &out, const StemAndLeaf &x)//;
   {
       out << x.FRONT_ptr->getValue() << " | ";
       return out;
   }

private:
   unsigned short leafUnit; // increment of each leaf in this particular structure
   unsigned short stemUnit; // increment of each stem in this particular structure

   Stem *FRONT_ptr, *REAR_ptr, *Insert, *Prev, *Cursor;

   inline bool isEmpty() const { return FRONT_ptr == nullptr; }

};
#endif /* STEAMANDLEAF2_H_ */

/*
 * Steam2.h
 */
#ifndef STEM2_H_
#define STEM2_H_
#include <ostream>
#include <list>
// DO NOT REMOVE THE LINE BELOW
/* CMSC341_TEST_INCLUDES */

#include "Leaf2.h"

class Stem
{
    // DO NOT REMOVE THE LINE BELOW
    /* CMSC341_FRIEND_CLASSES */

public:
    Stem(){FRONT_ptr = NULL; REAR_ptr = NULL;}
    virtual ~Stem(){};

    unsigned short getLeafCount() const { return m_LeafCount; }
    void setLeafCount(unsigned short LeafCount) { m_LeafCount = LeafCount; }
    void incrementLeafCount() { m_LeafCount++; }
    void decrementLeafCount() { m_LeafCount--; }

    Leaf *getLeafLink() const {return FRONT_ptr;}
    void setLeafList(Leaf *LeafList) { FRONT_ptr = LeafList; }

    unsigned short getValue() const { return m_Value; }
    void setValue(unsigned short value) { m_Value = value; }

    Stem *getNext() const { return m_Next; }
    void setNext(Stem *next) { m_Next = next; }

    /**
     * @brief Add the value to the stem. Returns trues if a new lef was created
     *
     * @param value The number to insert
     * @return true if a new leaf was created
     * @return false if we've seen the value before
     */
    bool insert(unsigned short value)
    {
        Insert = new Leaf();
        Insert->setValue(value);
        if(FRONT_ptr = NULL)
        {
            FRONT_ptr = Insert;
            REAR_ptr = Insert;
            return true;
        }
        else
        {
            for(Cursor = FRONT_ptr; Cursor != NULL; Prev = Cursor, Cursor = Cursor->getNext())
            {
                if(Cursor->getValue() == Insert->getValue())
                {
                    Cursor->incrementCount();
                    return false;
                }
                else
                {
                    if((Cursor == FRONT_ptr) && (Cursor->getValue() > Insert->getValue()))
                    {
                        Insert->setNext(Cursor);
                        FRONT_ptr = Insert;
                    }
                    else if((Cursor == REAR_ptr) && (Cursor->getValue() < Insert->getValue()))
                    {
                        Cursor->setNext(Insert);
                        REAR_ptr = Insert;
                    }
                    else if((Prev->getValue() < Insert->getValue()) && (Cursor->getValue() > Insert->getValue()))
                    {
                        Prev->setNext(Insert);
                        Insert->setNext(Cursor);
                    }
                    return true;
                }
            }
        }
    }
    unsigned short findSmallestValue()
    {
        if(FRONT_ptr != NULL){
        return FRONT_ptr->getValue();}
        else {return 0;}
    }
    unsigned short findLargestValue()
    {
        if(REAR_ptr != NULL){
        return REAR_ptr->getValue();}
        else {return 0;}
    }

private:
    // they are given the code for a L.L.
    Leaf *FRONT_ptr, *REAR_ptr, *Insert, *Prev, *Cursor;

    /**
     * Stem Value
     */
    unsigned short m_Value;

    /**
     * How many leaves in this stem
     */
    unsigned short m_LeafCount;

    /**
     * Pointer to the next stem
     */
    Stem *m_Next;
};
#endif /* STEM2_H_ */
/*
*Leaf2.h
*/

#ifndef LEAF2_H_
#define LEAF2_H_

#include <ostream>
// DO NOT REMOVE THE LINE BELOW
/* CMSC341_TEST_INCLUDES */


struct Leaf
{
    // DO NOT REMOVE THE LINE BELOW
    /* CMSC341_FRIEND_CLASSES */

    Leaf()
        : m_Count(1)
    {
    }

    unsigned short getValue() const { return m_Value; }
    void setValue(unsigned short value) { m_Value = value; }

    unsigned short getCount() const { return m_Count; }
    void setCount(unsigned short count) { m_Count = count; }
    void incrementCount() { m_Count++; }
    void decrementCount() { m_Count--; }

    Leaf *getNext() const { return m_Next; }
    void setNext(Leaf *next) { m_Next = next; }

    /**
     * @brief Operator overload to print the Leaf in a nice format According to
     * the project description the format should be "value:count"
     *
     * @param out
     * @param leaf
     * @return std::ostream&
     */
    friend std::ostream &operator<<(std::ostream &out, const Leaf &leaf)
    {
        out << leaf.m_Value << ":" << leaf.m_Count;
        return out;
    }

private:
    unsigned short m_Value;
    unsigned short m_Count;
    Leaf *m_Next;
};
#endif /* LEAF2_H_ */

輸入文件是具有以下值的 .txt 文件:

1
10
22
13
8
23

即使我將“添加術語”調用后的所有內容都轉換為注釋,我也會收到錯誤,並且我知道葉部分不起作用,因為當我運行“findSmallestTerm”和“findLargestTerm”時,它只給我詞干值。 除了葉值的錯誤之外,我的代碼還有很多錯誤,但我不是在詢問整個項目的答案,如果有意義的話,我只需要幫助獲取莖和葉列表的連接。 很抱歉在這里轉儲所有代碼,但我不知道問題的哪一部分,所以我不知道什么是相關的或不相關的。

更新:我刪除了一些不必要的代碼並添加了輸入文件的信息。

崩潰是由未初始化的數據成員和錯誤的檢查語句引起的:

if(FRONT_ptr = NULL)

這應該是:

if(FRONT_ptr == NULL)

Stem::m_Next 應該被初始化:

Stem *m_Next=NULL;

Stem::m_Next 應該被初始化:

Leaf *m_Next=NULL;

變量Prev應該在進入 for 循環后初始化:

void addTerm(unsigned short term) {
    // Prev should be inited or the crash happens
    for (Prev = Cursor, Cursor = FRONT_ptr; Cursor != NULL;
         Prev = Cursor, Cursor = Cursor->getNext()) {
}
}


Stem 類應該有析構函數以避免內存泄漏:

virtual ~Stem() {
  while (FRONT_ptr != NULL) {
    Cursor = FRONT_ptr;
    FRONT_ptr = Cursor->getNext();
    delete Cursor;
  }
};

該代碼還有其他一些難聞的氣味和錯誤:

  • 臨時變量不應是數據成員:INsert、Prev、Cursor
  • Stem::Insert , StemAndLeaf::Insert當數據已經存在於鏈表中時存在內存泄漏,新的元素還沒有插入鏈表,指針應該被刪除
  • 在訪問之前忘記檢查指針是否為 NULL: getSmallestTerm , getLargestTerm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM