No Description

Llist.cpp 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // p == NULL is interpreted as prepending
  32. if (p == NULL) {
  33. n = new Node(d, head);
  34. head = n;
  35. if (!tail) tail = n;
  36. }
  37. else {
  38. n = new Node (d, p->next);
  39. p->next = n;
  40. if (tail == p) tail = n;
  41. }
  42. length++;
  43. }
  44. Node* Llist::search(ElementType d) const {
  45. Node *p = head;
  46. while(p) {
  47. if (p->data == d) return p;
  48. p = p->next;
  49. }
  50. return NULL;
  51. }
  52. ostream& operator<< (ostream &out, const Llist& L) {
  53. L.display(out);
  54. return out;
  55. }
  56. void Llist::printRev() const {
  57. printRev(head);
  58. }
  59. void Llist::printRev(Node *p) const {
  60. if (p != NULL) {
  61. printRev(p->next);
  62. cout << p->data << endl;
  63. }
  64. }
  65. void Llist::removeAfter(Node *cur) {
  66. Node *suc;
  67. // cur == NULL is interpreted as removing the first
  68. // node of the list
  69. if (cur == NULL and head != NULL) {
  70. suc = head->next;
  71. delete head;
  72. head = suc;
  73. // in case the removed node was the only node
  74. if (suc == NULL) tail = NULL;
  75. }
  76. else if (cur->next != NULL) {
  77. suc = cur->next->next;
  78. delete cur->next;
  79. cur->next = suc;
  80. // if the removed node was the last, update the tail pointer
  81. if (suc == NULL) tail = cur;
  82. }
  83. length--;
  84. }
  85. // Assignment operator:
  86. // 1. empty the list of the invoking object
  87. // 2. traverse the list of the parameter appending each
  88. // node data to the invoking object
  89. Llist& Llist::operator=(const Llist &L) {
  90. if (this != &L) {
  91. while(head != NULL) removeAfter(NULL);
  92. Node *cur = L.head;
  93. while (cur != NULL) {
  94. append(cur->data);
  95. cur = cur->next;
  96. }
  97. }
  98. return *this;
  99. }
  100. Llist::Llist(const Llist &L) {
  101. head = tail = NULL;
  102. length = 0;
  103. if (this != &L) {
  104. Node *cur = L.head;
  105. while (cur != NULL) {
  106. append(cur->data);
  107. cur = cur->next;
  108. }
  109. }
  110. }
  111. Llist::~Llist() {
  112. while(head != NULL) removeAfter(NULL);
  113. }