Advertisement
GbossMega9

DelegatingToALinkedList

Sep 27th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.45 KB | None | 0 0
  1. using namespace std;
  2.  
  3. # include <iostream>
  4. # include <14_5h.hpp>
  5.  
  6. // implementation of pure virtual function so that
  7. // derived classes can chain up
  8.  
  9.  
  10. void Part::Display() const
  11. {
  12.     cout << "\nPart Number: " << itsPartNumber << endl;
  13. }
  14.  
  15. CarPart::CarPart(USHORT year, ULONG partNumber):
  16.     itsModelYear(year),
  17.     Part(partNumber)
  18.     {}
  19.    
  20. AirPlanePart::AirPlanePart(USHORT EngineNumber, ULONG PartNumber):
  21.     itsEngineNumber(EngineNumber),
  22.     Part(PartNumber)
  23.     {}
  24.         // PartNode Implementations...
  25.        
  26.         PartNode::PartNode(Part* pPart):
  27.         itsPart(pPart),
  28.         itsNext(0)
  29.         {}
  30.        
  31.         PartNode::~PartNode()
  32.         {
  33.             delete itsPart;
  34.             itsPart = 0;
  35.             delete itsNext;
  36.             itsNext = 0;
  37.         }
  38.        
  39.         // Returns NULL if no next PartNode
  40.         PartNode * PartNode::GetNext() const
  41.         {
  42.             return itsNext;
  43.         }
  44.            
  45.        
  46.         Part * PartNode::GetPart() const
  47.         {
  48.             if (itsPart)
  49.                 return itsPart;
  50.             else
  51.                 return NULL;    //error
  52.             }
  53.            
  54.            
  55.                
  56.         PartsList PartsList::GlobalPartsList;
  57.        
  58.        
  59.         // Implementation for Lists....
  60.        
  61.         PartsList::PartsList():
  62.         pHead(0),
  63.         itsCount(0)
  64.         {}
  65.        
  66.        
  67.         PartsList::~PartsList()
  68.         {
  69.             delete pHead;
  70.         }
  71.        
  72.         Part* PartsList::GetFirst() const
  73.         {
  74.             if (pHead)
  75.                 return pHead->GetPart();
  76.             else
  77.                 return NULL; // error catch here
  78.         }
  79.        
  80.         Part * PartsList::operator[](ULONG offset) const
  81.         {
  82.             PartNode* pNode = pHead;
  83.            
  84.             if(!pHead)
  85.                 return NULL; // errror catch here
  86.            
  87.             if (offset > itsCount)
  88.                 return NULL; //error
  89.            
  90.             for (ULONG i=0;i<offset; i++)
  91.                 pNode->GetNext();
  92.            
  93.             return pNode->GetPart();
  94.         }
  95.        
  96.         Part* PartsList::Find(ULONG & position, ULONG PartNumber) const
  97.         {
  98.             PartNode * pNode = 0;
  99.         for (pNode = pHead, position = 0;
  100.             pNode!=NULL;
  101.             pNode = pNode->GetNext(),position++)
  102.         {
  103.         if (pNode->GetPart() ->GetPartNumber() == PartNumber)
  104.             break;
  105.             }
  106.         if (pNode == NULL)
  107.             return NULL;
  108.         else
  109.             return pNode ->GetPart();
  110.         }
  111.        
  112.         void PartsList::Iterate(void (Part::*func)()const) const
  113.         {
  114.             if (!pHead)
  115.                 return;
  116.             PartNode* pNode = pHead;
  117.             do
  118.                 (pNode->GetPart()->*func)();
  119.             while (pNode = pNode->GetNext());
  120.         }
  121.        
  122.         void PartsList::Insert(Part *pPart)
  123.         {
  124.             PartNode * pNode = new PartNode(pPart);
  125.             PartNode * pCurrent = pHead;
  126.             PartNode * pNext = 0;
  127.            
  128.             ULONG New = pPart->GetPartNumber();
  129.             ULONG Next = 0;
  130.             itsCount++;
  131.            
  132.             if (!pHead)
  133.             {
  134.                 pHead = pNode;
  135.                     return;
  136.             }
  137.            
  138.             // if this one is smaller than head
  139.             // this one is the new head
  140.             if (pHead->GetPart() ->GetPartNumber() > New)
  141.             {
  142.                 pNode->SetNext(pHead);
  143.                 pHead = pNode;
  144.                 return;
  145.             }
  146.            
  147.  
  148. for (;;)
  149.  
  150. // if there is no next, append this new one
  151. if (!pCurrent->GetNext())
  152. {
  153.     pCurrent->SetNext(pNode);
  154.     return;
  155.     }
  156.    
  157.     // if this goes after this one and before the next
  158.     // then insert it here, otherwise get the next
  159.     pNext = pCurrent->GetNext();
  160.     Next = pNext->GetPart()->GetPartNumber();
  161.     if (Next > New)
  162.     {
  163.         pCurrent->SetNext(pNode);
  164.         pNode->SetNext(pNext);
  165.         return;
  166.         }
  167.         pCurrent = pNext;
  168.         }
  169.        
  170.        
  171.        
  172.             void PartsCatalog::Insert(Part * newPart)
  173.             {
  174.                 ULONG partNumber = newPart->GetPartNumber();
  175.                 ULONG offset;
  176.                
  177.                 if (!thePartsList.Find(offset, partNumber))
  178.                 thePartsList.Insert(newPart);
  179.                 else
  180.         {
  181.             cout << partNumber << " was the ";
  182.             switch (offset)
  183.             {
  184.                 case 0:  cout << "first "; break;
  185.                 case 1:  cout << "second "; break;
  186.                 case 2:  cout << "third "; break;
  187.                 default: cout << offset+1 << "th ";
  188.             }
  189.             cout << "entry. Rejected!\n";
  190.         }
  191.     }
  192.        
  193.         ULONG PartsCatalog::Exists(ULONG PartNumber)
  194.         {
  195.             ULONG offset;
  196.             thePartsList.Find(offset,PartNumber);
  197.             return offset;
  198.         }
  199.        
  200.         Part * PartsCatalog::Get(int PartNumber)
  201.         {
  202.             ULONG offset;
  203.             Part * thePart = thePartsList.Find(offset, PartNumber);
  204.             return thePart;
  205.         }
  206.        
  207.         void startof()
  208.         {
  209.             PartsCatalog pc;
  210.             Part * pPart = 0;
  211.             ULONG PartNumber;
  212.             USHORT value;
  213.             ULONG choice;
  214.            
  215.             while (1)
  216.             {
  217.                 cout << "(0)Quit (1)Car (2)Plane: ";
  218.                 cin >> choice;
  219.                
  220.                 if (!choice)
  221.                 break;
  222.                
  223.                 cout << "New part Number?: ";
  224.                 cin >> PartNumber;
  225.                
  226.                 if (choice == 1)
  227.                 {
  228.                     cout << "ModelYear?:";
  229.                     cin >> value;
  230.                     pPart = new CarPart(value,PartNumber);
  231.                 }
  232.                 else
  233.                 {
  234.             cout << "Engine Number?: ";
  235.             cin >> value;
  236.             pPart = new AirPlanePart(value,PartNumber);
  237.         }
  238.         pc.Insert(pPart);
  239.     }
  240.         pc.ShowAll();
  241.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement