Advertisement
cesarcardinale

M3103 Alg Av - TD3

Sep 20th, 2018
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef LIST_HPP
  2. #define LIST_HPP
  3. #include <iostream>
  4. #include <memory>
  5. #include "node.hpp"
  6.  
  7.  
  8. template <typename T>
  9. class CList
  10. {
  11. private:
  12.     std::shared_ptr <CNode<T> >  m_fictionaHead;
  13.     std::shared_ptr <CNode<T> > m_realTail;
  14.  
  15. public:
  16.     CList ();
  17.     ~CList ();
  18.     void push_front (const T & val);
  19.     void Show () const;
  20.     std::shared_ptr <CNode<T> > Find (const T & val) const;
  21.     void Add (const T & val, const std::shared_ptr <CNode<T> > &);
  22.     void Delete (const std::shared_ptr <CNode<T> > &);
  23.     void push_back (const T & val);
  24.     const std::shared_ptr  <CNode<T> > & Begin () const;
  25.     void AddAfter (const std::shared_ptr <CNode<T> > &, const T & val);
  26.     void AddBefore (const std::shared_ptr <CNode<T> > &, const T & val);
  27. };
  28.  
  29. template <typename T>
  30. const std::shared_ptr <CNode<T> > & CList<T>::Begin () const
  31. {
  32.     return m_fictionaHead;
  33. }
  34.  
  35. template <typename T>
  36. void CList<T>::AddAfter (const std::shared_ptr <CNode<T> > & ptr, const T & val)
  37. {
  38.     ptr -> SetNextNode (std::shared_ptr <CNode<T> > (new CNode<T> (val, ptr-> GetNextNode(), ptr)));
  39.     if(ptr != m_realTail){
  40.         ptr -> GetNextNode() -> GetNextNode() -> SetPrevNode(ptr -> GetNextNode());
  41.     } else {
  42.         m_realTail = ptr -> GetNextNode();
  43.     }
  44. }
  45.  
  46. template <typename T>
  47. void CList<T>::AddBefore (const std::shared_ptr <CNode<T> > & ptr, const T & val)
  48. {
  49.     ptr -> SetPrevNode (std::shared_ptr <CNode<T> > (new CNode<T> (val, ptr, ptr->GetPrevNode())));
  50.     ptr -> GetPrevNode() -> GetPrevNode() -> SetNextNode(ptr->GetPrevNode());
  51. }
  52.  
  53. template <typename T>
  54. void CList<T>::Add (const T & val, const std::shared_ptr <CNode<T> > & ptr)
  55. {
  56.     std::shared_ptr <CNode<T> > ptrTmp ( new CNode<T> (val, ptr -> GetNextNode ()));
  57.     ptr -> SetNextNode (ptrTmp);
  58. }
  59.  
  60. /*Ptr_t AjoutApres (Ptr_t Tete, int NewVal, Ptr_t PtrElem)
  61. {
  62.     if (! PtrElem) PtrElem = Tete;
  63.  
  64.     PtrElem->SetSuivant
  65.            (new C1Link (NewVal, PtrElem->GetSuivant()));
  66.  
  67.     return PtrElem->GetSuivant();
  68.  
  69. } // AjoutApres()
  70. */
  71.  
  72. template <typename T>
  73. CList<T>::CList () : m_fictionaHead (new CNode<T> ()), m_realTail (m_fictionaHead) {}
  74.  
  75. template <typename T>
  76. CList<T>::~CList () {
  77.     // delete m_fictionaHead;
  78. }
  79.  
  80. template <typename T>
  81. std::shared_ptr <CNode<T> > CList<T>::Find (const T & val) const
  82. {
  83.     std::shared_ptr <CNode<T> > Ptr (m_fictionaHead->GetNextNode ());
  84.     for (; Ptr != nullptr && Ptr -> GetData () != val ; Ptr = Ptr->GetNextNode ());
  85.  
  86.     return Ptr;
  87. }
  88.  
  89.  
  90.  
  91. template <typename T>
  92. void CList<T>::push_front (const T & val)
  93. {
  94.     m_fictionaHead->SetNextNode (std::shared_ptr <CNode<T> > (new CNode<T> (val, m_fictionaHead->GetNextNode ())));
  95.     if (m_realTail == m_fictionaHead) m_realTail = m_fictionaHead->GetNextNode ();
  96. }
  97.  
  98. template <typename T>
  99. void CList<T>::Show () const
  100. {
  101.     //for (CNodeInt* Ptr (m_Head); Ptr; ++*Ptr)
  102.     for (std::shared_ptr <CNode<T> > Ptr (m_fictionaHead -> GetNextNode()); Ptr; Ptr = Ptr->GetNextNode ())
  103.             std::cout << Ptr->GetData() << "; ";
  104.     std::cout << std::endl;
  105. }
  106.  
  107. template <typename T>
  108. void CList<T>::Delete (const std::shared_ptr <CNode<T>> & pDelete)
  109. {
  110.     std::shared_ptr <CNode<T> > pFind (m_fictionaHead);
  111.     for ( ; pFind -> GetNextNode () != pDelete ; pFind = pFind -> GetNextNode ());
  112.  
  113.     pFind -> SetNextNode (pDelete -> GetNextNode ());
  114.     if (pDelete == m_realTail) m_realTail = pFind;
  115.     pDelete -> SetNextNode (nullptr);
  116.  
  117.     // delete pDelete;
  118. }
  119.  
  120. template <typename T>
  121. void CList<T>::push_back (const T & val)
  122. {
  123.     std::shared_ptr <CNode<T> > pTmp (new CNode<T> (val));
  124.     m_realTail -> SetNextNode (pTmp);
  125.     m_realTail = pTmp;
  126. }
  127.  
  128.  
  129. #endif // LIST_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement