如何在链表中显示特定的节点



我对c++还是个新手。

我有这个代码从分配,我不太了解它的全部,但我必须使程序在最后为用户提供一个选项,以召回任何零件号和型号年/引擎号。

我不知道如何着手做这项任务…也许每个节点都有一个id,这样我就能回忆起来了?

或者它是我唯一的选择重写程序使用数组或向量数据结构?

     #include <iostream>
     using namespace std;
     typedef unsigned long ULONG;
     typedef unsigned short USHORT;

     // **************** Part ************
     // Abstract base class of parts
     class Part
     {
     friend void showPart();
     public:
        Part():itsPartNumber(1) {}
        Part(ULONG PartNumber):itsPartNumber(PartNumber){}
        virtual ~Part(){};
        ULONG GetPartNumber() const { return itsPartNumber; }
        virtual void Display() const =0;  // must be overridden
     private:
        ULONG itsPartNumber;
     };
     // implementation of pure virtual function so that
     // derived classes can chain up
     void Part::Display() const
     {
         cout << "nPart Number: " << itsPartNumber << endl;
     }
     // **************** Car Part ************
     class CarPart : public Part
     {
     friend void showPart();
     public:
        CarPart():itsModelYear(94){}
        CarPart(USHORT year, ULONG partNumber);
        virtual void Display() const
       {
          Part::Display(); cout << "Model Year: ";
          cout << itsModelYear << endl;
       }
     private:
        USHORT itsModelYear;
     };
     CarPart::CarPart(USHORT year, ULONG partNumber):
        itsModelYear(year),
        Part(partNumber)
     {}

     // **************** AirPlane Part ************
     class AirPlanePart : public Part
     {
     friend void showPart();
     public:
        AirPlanePart():itsEngineNumber(1){};
        AirPlanePart(USHORT EngineNumber, ULONG PartNumber);
        virtual void Display() const
       {
          Part::Display(); cout << "Engine No.: ";
          cout << itsEngineNumber << endl;
       }
     private:
        USHORT itsEngineNumber;
     };
     AirPlanePart::AirPlanePart(USHORT EngineNumber, ULONG PartNumber):
        itsEngineNumber(EngineNumber),
        Part(PartNumber)
     {}
     // **************** Part Node ************
     class PartNode
     {
     public:
         PartNode (Part*);
         ~PartNode();
         void SetNext(PartNode * node) { itsNext = node; }
         PartNode * GetNext() const;
         Part * GetPart() const;
      private:
         Part *itsPart;
         PartNode * itsNext;
      };
      // PartNode Implementations...
      PartNode::PartNode(Part* pPart):
      itsPart(pPart),
      itsNext(0)
      {}
      PartNode::~PartNode()
      {
         delete itsPart;
         itsPart = 0;
         delete itsNext;
         itsNext = 0;
      }
      // Returns NULL if no next PartNode
      PartNode * PartNode::GetNext() const
      {
            return itsNext;
      }
      Part * PartNode::GetPart() const
      {
         if (itsPart)
            return itsPart;
         else
            return NULL; //error
      }
      // **************** Part List ************
      class PartsList
      {
      public:
         PartsList();
         ~PartsList();
         // needs copy constructor and operator equals!
         Part*      Find(ULONG & position, ULONG PartNumber)  const;
         ULONG      GetCount() const { return itsCount; }
         Part*      GetFirst() const;
         static     PartsList& GetGlobalPartsList()
       {
          return  GlobalPartsList;
       }
         void       Insert(Part *);
         void       Iterate(void (Part::*f)()const) const;
         Part*      operator[](ULONG) const;
      private:
         PartNode * pHead;
         ULONG itsCount;
         static PartsList GlobalPartsList;
      };
      PartsList PartsList::GlobalPartsList;
      // Implementations for Lists...
      PartsList::PartsList():
         pHead(0),
         itsCount(0)
         {}
      PartsList::~PartsList()
      {
         delete pHead;
      }
      Part*   PartsList::GetFirst() const
      {
         if (pHead)
            return pHead->GetPart();
         else
            return NULL;  // error catch here
      }
      Part *  PartsList::operator[](ULONG offSet) const
      {
         PartNode* pNode = pHead;
         if (!pHead)
            return NULL; // error catch here
         if (offSet > itsCount)
            return NULL; // error
         for (ULONG i=0;i<offSet; i++)
            pNode = pNode->GetNext();
        return   pNode->GetPart();
      }
      Part*   PartsList::Find(ULONG & position, ULONG PartNumber)  const
      {
         PartNode * pNode = 0;
         for (pNode = pHead, position = 0;
               pNode!=NULL;
               pNode = pNode->GetNext(), position++)
         {
            if (pNode->GetPart()->GetPartNumber() == PartNumber)
               break;
         }
         if (pNode == NULL)
            return NULL;
         else
            return pNode->GetPart();
      }
      void PartsList::Iterate(void (Part::*func)()const) const
      {
         if (!pHead)
            return;
         PartNode* pNode = pHead;
         do
            (pNode->GetPart()->*func)();
         while (pNode = pNode->GetNext());
      }
      void PartsList::Insert(Part* pPart)
      {
         PartNode * pNode = new PartNode(pPart);
         PartNode * pCurrent = pHead;
         PartNode * pNext = 0;
         ULONG New =  pPart->GetPartNumber();
         ULONG Next = 0;
         itsCount++;
         if (!pHead)
         {
            pHead = pNode;
            return;
         }
         // if this one is smaller than head
         // this one is the new head
         if (pHead->GetPart()->GetPartNumber() > New)
         {
            pNode->SetNext(pHead);
            pHead = pNode;
            return;
         }
         for (;;)
         {
            // if there is no next, append this new one
            if (!pCurrent->GetNext())
            {
               pCurrent->SetNext(pNode);
               return;
            }
            // if this goes after this one and before the next
            // then insert it here, otherwise get the next
            pNext = pCurrent->GetNext();
            Next = pNext->GetPart()->GetPartNumber();
            if (Next > New)
            {
               pCurrent->SetNext(pNode);
               pNode->SetNext(pNext);
               return;
            }
            pCurrent = pNext;
         }
      }
      int main()
      {
         PartsList pl = PartsList::GetGlobalPartsList();
         Part * pPart = 0;
         ULONG PartNumber;
         USHORT value;
         ULONG choice;
         while (1)
         {
            cout << "(0)Quit (1)Car (2)Plane: ";
            cin >> choice;
            if (!choice)
               break;
            cout << "New PartNumber?: ";
            cin >>  PartNumber;
            if (choice == 1)
            {
               cout << "Model Year?: ";
               cin >> value;
               pPart = new CarPart(value,PartNumber);
            }
            else
            {
               cout << "Engine Number?: ";
               cin >> value;
               pPart = new AirPlanePart(value,PartNumber);
            }
            pl.Insert(pPart);
         }
         void (Part::*pFunc)()const = &Part::Display;
         pl.Iterate(pFunc);
         cout << "nnnThere are " << pl.GetCount() << " items in the list" << endl;

        return 0;
     }

我尝试在PartsList类中使用Find()。Find()是否接受零件编号并返回零件的地址?

我写这个是为了解引用检索到的地址,但它给了我错误no match for 'operator<<' in 'std::cout << * show':

    int findnumber;
         ULONG position;
         cout << "Enter Partnumber" << endl;
         cin >> findnumber;
         Part* show = pl.Find(position, findnumber);
         cout << *show;

我做错了吗?D:请给我看看……

Find函数接受零件编号,但返回一个指向零件的指针,该指针与零件的地址不同(这将是一个引用,由&表示)。此外,Find会引用一个名为'position'的变量,因此在调用Find函数之后,传递给'position'的变量将包含该部件在链表中的位置值。

不能使用<<操作符,是它没有提供给Part类。然而,从提供的源代码来看,我们的目标似乎是让您理解多态性,而不是尝试使用<<,而是在找到的部分上调用Display函数。如:-

Part* part = pl.Find(position, findnumber);
part->Display();

这将显示相关部件类型的文本,因此,如果返回的部件是CarPart,则将调用CarPart的display函数,而如果部件是AirPlane部件,则调用其display函数。

如果你想使用流操作符(<<),你需要重载io操作符,你可以在这里了解更多。

PartsList已经有一个Find()方法,可用于根据其零件号检索任何零件。然后可以调用该部分的Display()方法。

相关内容

  • 没有找到相关文章

最新更新