Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. class SeqList
  6. {
  7. private:
  8. Type *data;
  9. int size;
  10. int max;
  11. void Error(const char *s)const { cout << s; exit(1); }
  12. public:
  13. SeqList(int n = 100);
  14. SeqList(const SeqList& l);
  15. ~SeqList(){delete[]data;}
  16. SeqList& operator=(const SeqList& l);
  17. void InsertRear(const Type& item);
  18. void Insert(Type *itr, const Type& item);
  19. void Erase(Type *itr);
  20. void Clear() { size = 0; }
  21. Type& operator[](int id);
  22. const Type& operator[](int id) const;
  23. int Size()const { return size; }
  24. int Empty()const { return size == 0; }
  25. int Full()const { return size == max; }
  26. Type *Begin() { return data; }
  27. const Type *Begin()const { return data; }
  28. Type *End() { return data + size; }
  29. const Type *End()const { return data + size; }
  30. void Swap(int id1, int id2);
  31. Type *Print(const SeqList& l, int id) { cout << l.data[id]<<" "; return 0; }
  32. };
  33.  
  34. SeqList::SeqList(int n)
  35. {
  36. data = new Type[n];
  37. if (data == NULL)
  38. Error("Memory allocation error!\n");
  39. size = 0;
  40. max = n;
  41. }
  42.  
  43. void SeqList::InsertRear(const Type& item)
  44. {
  45. if (size == max)
  46. Error("InsertRear:list is full!\n");
  47. data[size] = item;
  48. size++;
  49. }
  50.  
  51. SeqList::SeqList(const SeqList& l)
  52. {
  53. data = new Type[l.max];
  54. if (data == NULL)
  55. Error("Memoty allocation error!\n");
  56. for (int i = 0; i < l.size; ++i)
  57. data[i] = l.data[i];
  58. size = l.size;
  59. max = l.max;
  60. }
  61.  
  62. SeqList& SeqList::operator=(const SeqList& l)
  63. {
  64. if (max != l.max)
  65. {
  66. delete[]data;
  67. data = new Type[l.max];
  68. if (data == NULL)
  69. Error("Memory allocation error!\n");
  70. }
  71. for (int i = 0; i < l.size; ++i)
  72. data[i] = l.data[i];
  73. size = l.size;
  74. max = l.max;
  75. return *this;
  76. }
  77.  
  78. Type& SeqList::operator[](int id)
  79. {
  80. if (id<0 || id>size - 1)
  81. Error("id is illegal!\n");
  82. return data[id];
  83. }
  84.  
  85. const Type& SeqList::operator[](int id)const
  86. {
  87. if (id<0 || id>size - 1)
  88. Error("id is illegal!\n");
  89. return data[id];
  90. }
  91.  
  92. void SeqList::Insert(Type *itr, const Type& item)
  93. {
  94. for (Type *p = data + size, *q = data + size - 1; p != itr; --p, --q)
  95. *p = *q;
  96. *itr = item;
  97. size++;
  98. }
  99.  
  100. void SeqList::Erase(Type *itr)
  101. {
  102. for (Type *p = itr, *q = itr + 1; q != data + size; ++p, ++q)
  103. *p = *q;
  104. size--;
  105. }
  106.  
  107. void SeqList::Swap(int id1, int id2)
  108. {
  109. Type temp;
  110. if (id1<0 || id1>size || id2<0 || id2>size)
  111. Error("id is illegal!\n");
  112. temp = data[id1];
  113. data[id1] = data[id2];
  114. data[id2] = temp;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement