Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. /*
  2. Description: Make a program that uses link lists
  3. Ismaeel Varis
  4. 2/3/16
  5. */
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9. using namespace std;
  10. struct Node
  11. {
  12. char letter;
  13. Node * link;
  14. Node(char let = ' ', Node * next = nullptr)
  15. {
  16. letter = let; link = next;
  17. }
  18. };
  19. class List
  20. {
  21. public:
  22. List(Node * ptr = nullptr)
  23. {
  24. head = ptr;
  25. }
  26. void add2Beg(char let)
  27. {
  28. Node *ptr;
  29. if (head == nullptr)
  30. {
  31. head = new Node(let);
  32. }
  33. else
  34. {
  35. ptr = new Node(let, head);
  36. head = ptr;
  37. }
  38. }
  39. void print()
  40. {
  41. print(head);
  42. }
  43. void add2End(char let)
  44. {
  45. Node * ptr = head;
  46. if (head == nullptr)
  47. head = new Node(let);
  48. else
  49. {
  50. while (ptr->link != nullptr)
  51. ptr = ptr->link;
  52. ptr->link = new Node(let);
  53. }
  54. }
  55. void fromString(const string & word)
  56. {
  57. Node * ptr = head;
  58. if (head == nullptr)
  59. {
  60. head = new Node(word[0], nullptr);
  61. }
  62. else
  63. {
  64. while (ptr->link != nullptr)
  65. {
  66. ptr->link;
  67. }
  68. }
  69. for (int i = 0; i > word.length(); i++)
  70. {
  71. ptr->link = new Node(word[i]);
  72. ptr = ptr->link;
  73. }
  74. }
  75. List operator+ (const List & rhs) const
  76. {
  77. List list1;
  78. Node *ptr = head;
  79. Node *rtptr = rhs.head;
  80. while (ptr != nullptr)
  81. {
  82. list1.add2End(ptr->letter);
  83. ptr = ptr->link;
  84. }
  85. while (rtptr != nullptr)
  86. {
  87. list1.add2End(rtptr->letter);
  88. rtptr->link;
  89. }
  90. return list1;
  91.  
  92. // add2End will add to the end of rtptr
  93. }
  94.  
  95. friend ostream & operator<<(ostream & out, const List & rhs);
  96. void deleteList(Node * & ptr)
  97. {
  98.  
  99. if (ptr != nullptr)
  100. {
  101. deleteList (ptr->link);
  102. delete ptr;
  103. }
  104. // deletes info stored in headPtr
  105. }
  106. ~List()
  107. {
  108. deleteList(head);
  109. //default destructor
  110. }
  111. private:
  112. Node * head;
  113. void print(Node * ptr)
  114. {
  115. if (ptr != nullptr)
  116. {
  117. cout << ptr->letter << " ";
  118. print(ptr->link);
  119. }
  120. }
  121. };
  122. ostream & operator<<(ostream & out, const List & rhs)
  123. {
  124. Node *ptr = rhs.head;
  125. while (ptr != nullptr)
  126. {
  127. out << ptr->letter;
  128. ptr = ptr->link;
  129. }
  130.  
  131. return out;
  132. }
  133.  
  134. int main()
  135. {
  136. List list1;
  137. List list2;
  138. List list3;
  139. string word1 = "cat";
  140. string word2 = "fish";
  141.  
  142. list1.fromString(word1);
  143. list2.fromString(word2);
  144. cout << list1 << " + " << list2 << " = "; //cat + fish = catfish
  145. list3 = list1 + list2;
  146. cout << list3 << endl;
  147. return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement