Нема описа

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "Llist.h"
  2. void Llist::append(ElementType d){
  3. Node *n = new Node(d);
  4. // in case of empty list
  5. if (!head) {
  6. head = n;
  7. }
  8. // otherwise, non empty list...
  9. else {
  10. tail->next = n;
  11. }
  12. tail = n;
  13. length++;
  14. }
  15. void Llist::prepend(ElementType d) {
  16. Node *n = new Node(d, head);
  17. head = n;
  18. // in case of empty list the tail = head = n
  19. if (length == 0) tail = n;
  20. length++;
  21. }
  22. void Llist::display(ostream &out) const {
  23. Node *p = head;
  24. while(p) {
  25. out << p->data << " ";
  26. p = p->next;
  27. }
  28. }
  29. void Llist::insertAfter(Node *p, ElementType d) {
  30. Node *n;
  31. if (p == NULL) {
  32. n = new Node(d, head);
  33. head = n;
  34. if (!tail) tail = n;
  35. }
  36. else {
  37. n = new Node (d, p->next);
  38. p->next = n;
  39. if (tail == p) tail = n;
  40. }
  41. length++;
  42. }
  43. Node* Llist::search(ElementType d) const {
  44. Node *p = head;
  45. while(p) {
  46. if (p->data == d) return p;
  47. p = p->next;
  48. }
  49. return NULL;
  50. }
  51. ostream& operator<< (ostream &out, const Llist& L) {
  52. L.display(out);
  53. return out;
  54. }
  55. void Llist::printRev() const {
  56. printRev(head);
  57. }
  58. void Llist::printRev(Node *p) const {
  59. if (p != NULL) {
  60. printRev(p->next);
  61. cout << p->data << endl;
  62. }
  63. }
  64. void Llist::removeAfter(Node *cur) {
  65. Node *suc;
  66. if (cur == NULL and head != NULL) {
  67. suc = head->next;
  68. delete head;
  69. head = suc;
  70. if (suc == NULL) tail = NULL;
  71. }
  72. else if (cur->next != NULL) {
  73. suc = cur->next->next;
  74. delete cur->next;
  75. cur->next = suc;
  76. if (suc == NULL) tail = cur;
  77. }
  78. length--;
  79. }
  80. Llist& Llist::operator=(const Llist &L) {
  81. if (this != &L) {
  82. while(head != NULL) removeAfter(NULL);
  83. Node *cur = L.head;
  84. while (cur != NULL) {
  85. append(cur->data);
  86. cur = cur->next;
  87. }
  88. }
  89. return *this;
  90. }
  91. Llist::Llist(const Llist &L) {
  92. head = tail = NULL;
  93. length = 0;
  94. if (this != &L) {
  95. Node *cur = L.head;
  96. while (cur != NULL) {
  97. append(cur->data);
  98. cur = cur->next;
  99. }
  100. }
  101. }
  102. Llist::~Llist() {
  103. while(head != NULL) removeAfter(NULL);
  104. }