image.png
    code

    image.png

    1. #ifndef LINKLIST_H
    2. #define LINKLIST_H
    3. #include "List.h"
    4. #include "exception.h"
    5. namespace DJLib
    6. {
    7. template <typename T>
    8. class LinkList : public List<T>
    9. {
    10. protected:
    11. struct Node :public object
    12. {
    13. T value;
    14. Node* next;
    15. };
    16. mutable struct:public object{
    17. char reserved[sizeof(T)];
    18. Node* next;
    19. }m_header;
    20. int m_length;
    21. Node* position(int i) const
    22. {
    23. Node* ret = reinterpret_cast<Node*>(&m_header);//需要强制新型类型转换,因为m_header与node不同
    24. for(int p = 0; p < i ; p++)
    25. {
    26. ret = ret->next;
    27. }
    28. return ret;
    29. }
    30. public:
    31. LinkList()
    32. {
    33. m_header.next = NULL;
    34. m_length = 0;
    35. }
    36. bool insert (const T& e)
    37. {
    38. return insert(m_length,e);
    39. }
    40. bool insert(int i, const T& e)
    41. {
    42. bool ret = ((0 <= i)&&(i <= m_length));
    43. if(ret)
    44. {
    45. Node* node = new Node();
    46. if(node != NULL)
    47. {
    48. node->value = e;
    49. node->next = position(i)->next;
    50. position(i)->next = node;
    51. m_length++;
    52. }
    53. else
    54. {
    55. THROW_EXCEPTION(noEnoughMemoryException,"No memory to insert element...");
    56. }
    57. }
    58. return ret;
    59. }
    60. bool remove( int i)
    61. {
    62. bool ret = ((0 <= i)&&(i < m_length));
    63. if(ret)
    64. {
    65. Node* toDel = position(i)->next;
    66. position(i)->next = toDel->next;
    67. delete toDel;
    68. m_length--;
    69. }
    70. return ret ;
    71. }
    72. bool set(int i, const T& e)
    73. {
    74. bool ret = ((0 <= i)&&(i < m_length));
    75. if(ret)
    76. {
    77. position(i)->next->value = e;
    78. }
    79. return ret ;
    80. }
    81. T get(int i)const
    82. {
    83. T ret;
    84. if( get(i,ret ))
    85. {
    86. return ret;
    87. }
    88. else
    89. {
    90. THROW_EXCEPTION(indexOutOfBoundsException,"Invalid parameter i to get element...");
    91. }
    92. }
    93. bool get(int i, T& e) const
    94. {
    95. bool ret = ((0 <= i)&&(i < m_length));
    96. if(ret)
    97. {
    98. e = position(i)->next->value ;
    99. }
    100. return ret ;
    101. }
    102. int find(const T& e) const
    103. {
    104. int ret = -1;
    105. int n = 0;
    106. Node* node = m_header.next;
    107. while(node)
    108. {
    109. if(node->value == e)
    110. {
    111. ret = n;
    112. break;
    113. }
    114. else
    115. {
    116. node = node->next;
    117. n++;
    118. }
    119. }
    120. return ret;
    121. }
    122. int length() const
    123. {
    124. return m_length;
    125. }
    126. void clear()
    127. {
    128. while(m_header.next)
    129. {
    130. Node* toDel = m_header.next;
    131. m_header.next = toDel->next;
    132. }
    133. m_length = 0;
    134. }
    135. ~LinkList()
    136. {
    137. clear();
    138. }
    139. };
    140. }
    141. #endif // LINKLIST_H

    image.png

    1. #include <iostream>
    2. #include "LinkList.h"
    3. using namespace std;
    4. using namespace DJLib;
    5. class Test
    6. {
    7. public:
    8. Test()
    9. {
    10. // throw(0);
    11. }
    12. };
    13. int main()
    14. {
    15. LinkList<int> list;//s1数组初始长度为5
    16. for(int i = 0; i < 5;i++)
    17. {
    18. // list.insert(i);
    19. list.insert(0,i);
    20. list.set(0,i*i);
    21. }
    22. for( int i = 0; i < list.length(); i++)
    23. {
    24. cout << list.get(i) << endl;
    25. }
    26. list.remove(3);
    27. for( int i = 0; i < list.length(); i++)
    28. {
    29. cout << list.get(i) << endl;
    30. }
    31. // LinkList<Test> lst1;
    32. // // Test t;
    33. cout <<"D.J.Software" << endl;
    34. return 0;
    35. }

    增加find函数后的main.c

    #include <iostream>
    #include "LinkList.h"
    #include "SeqList.h"
    using namespace std;
    using namespace DJLib;
    
    class Test : object
    {
        int i;
    public:
        Test(int v = 0)
        {
            i = v;
        }
    
        bool operator == (const Test& t)
        {
            return (i == t.i);
    
        }
    };
    
    
    int main()
    {
    
         Test t1(1);
         Test t2(2);
         Test t3(3);
         LinkList<Test> list;
    
        list.insert(t1);
        list.insert(t2);
        list.insert(t3);
        cout << list.find(t2) << endl;
    
    
    
    
    
    
         return 0;
    }