如何在链接列表中显示特定节点?

时间:2013-06-20 04:33:12

标签: c++ pointers linked-list abstract-class nodes

我是C ++的新手。

我有一个来自作业的代码,我不太了解所有这些,但是我必须让程序在最后给出一个选项,让用户回忆任何partnumber和model year / engine no 。已输入。

我不知道如何去做这个任务......也许每个节点都有某种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 << "\n\n\nThere are " << pl.GetCount() << " items in the list" << endl;


        return 0;
     }

我尝试在PartsList类中使用Find()。 Find()采用partnumber并返回部件的地址吗?

我写了这个来取消引用检索到的地址,但它给了我错误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:请告诉我......

2 个答案:

答案 0 :(得分:1)

函数Find确实取一个零件号,但返回一个指向零件的指针,该零件与零件的地址不同(它将是一个参考,由&amp;表示)。另外,Find引用了一个名为'position'的变量,因此在调用Find函数之后,为'position'传入的变量将包含该部分在链表中的位置值。

您无法使用&lt;&lt;运算符,是没有为Part类提供的。但是,根据提供的源代码,您的目标似乎是了解多态性,而不是尝试使用&lt;&lt;,在您找到的部件上调用Display函数。例如: -

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

这将显示相​​关类型零件的文本,因此如果返回的零件是CarPart,则将调用CarPart的Display功能,而如果零件是AirPlane零件,则调用其Display功能。

如果你想使用流操作符(&lt;&lt;),你需要重载io操作符,你可以阅读更多关于here的内容。

答案 1 :(得分:0)

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