wheelsmanx

CPS 272 linkList

Nov 9th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include "Header.h"
  5. using namespace std;
  6.  
  7. template <typename T>
  8. struct linkNode {
  9. int data;
  10. linkNode<T> *nextNode;
  11. };
  12.  
  13. template <typename T>
  14. class headNode {
  15. private:
  16. linkNode<T> *head, *tail;
  17.  
  18. public:
  19. int sizeList;
  20. headNode() {
  21. head = nullptr;
  22. tail = nullptr;
  23. }
  24. void sort() {
  25. int counter = 0;
  26. linkNode<T> *tempHead = head;
  27. T *tempNode = 0;
  28. while (tempHead != nullptr && tempNode != nullptr) {
  29. tempHead = tempHead->nextNode;
  30. counter++;
  31. for (int i = 0; i < counter; i++) {
  32. for (int j = 0; j < counter; j++) {
  33. if (tempHead->data > tempHead->nextNode->data) {
  34. *tempNode = tempHead->data;
  35. tempHead = tempHead->nextNode;
  36. tempHead->nextNode->data = *tempNode;
  37. }
  38. }
  39. }
  40. }
  41. }
  42. void addNode(T userData) {
  43. linkNode<T> *tempNode = new linkNode<T>;
  44. tempNode->data = userData;
  45. tempNode->nextNode = nullptr;
  46. if (head == nullptr) {
  47. head = tempNode;
  48. tail = tempNode;
  49. tempNode = nullptr;
  50. }
  51. else {
  52. tail->nextNode = tempNode;
  53. tail = tempNode;
  54. }
  55. sizeList++;
  56. }
  57. void display() {
  58. linkNode<T> *tempNode = new linkNode<T>;
  59. tempNode = head;
  60. while (tempNode != nullptr) {
  61. cout << tempNode->data << endl;
  62. tempNode = tempNode->nextNode;
  63. }
  64. }
  65. T at(int userInput) {
  66. linkNode<T> *tempNode = new linkNode<T>;
  67. tempNode = head;
  68. if (sizeList != 0) {
  69. for (int i = 0; i < sizeList; i++) {
  70. if (i == userInput) {
  71. break;
  72. }
  73. tempNode = tempNode->nextNode;
  74. }
  75. return tempNode->data;
  76. }
  77. else {
  78. cout << "Link List Empty" << endl;
  79. }
  80. }
  81. };
  82. class listData {
  83. public:
  84. string listOne = "10, 22, 34, 45, 48, 55, 56, 57, 57, 69, 70, 72, 74, 74, 80, 83, 84, 85, 88, 88";
  85. string listTwo = "50, 55, 57, 79, 81, 84, 87, 88, 90, 92, 95, 95, 95, 96, 99";
  86. vector<string> vectorOne;
  87. vector<string> vectorTwo;
  88. headNode<int> finalList;
  89. double average;
  90. int listSum;
  91. headNode<int> realFirstList;
  92. headNode<int> realSecondList;
  93. listData() {
  94. vectorOne = cStringSplit(listOne, ", ");
  95. vectorTwo = cStringSplit(listTwo, ", ");
  96. finalList = convert(vectorOne, vectorTwo);
  97. showSum();
  98. }
  99. void run() {
  100. for (int i = 0; i < 20; i++) {
  101. realFirstList.addNode(rand() % 100 + 0);
  102. }
  103. for (int i = 0; i < 15; i++) {
  104. realSecondList.addNode(rand() % 100 + 0);
  105. }
  106.  
  107.  
  108. }
  109. headNode<int> convert(vector<string> first, vector<string> second) {
  110. headNode<int> returnObject;
  111. for (int i = 0; i < first.size(); i++) {
  112. returnObject.addNode(String2Int(first[i]));
  113. }
  114. for (int i = 0; i < second.size(); i++) {
  115. returnObject.addNode(String2Int(second[i]));
  116. }
  117. return returnObject;
  118. }
  119. void showSum() {
  120. for (int i = 0; i < finalList.sizeList; i++) {
  121. listSum = listSum + finalList.at(i);
  122. }
  123. for (int i = 0; i < finalList.sizeList; i++) {
  124. average = listSum / finalList.sizeList;
  125. }
  126. finalList.sort();
  127. cout << "test" << endl;
  128. finalList.display();
  129. }
  130. void displayMess() {
  131. cout << "Result:" << endl << listOne << ", " << listTwo;
  132. cout << endl;
  133. cout << endl << "The sum of the final list's element is: " << listSum << endl;
  134. cout << "The average of the final list's elemets is:" << average << endl << endl;
  135. }
  136. };
  137.  
  138. void main() {
  139. listData temp;
  140. system("pause");
  141. }
Advertisement
Add Comment
Please, Sign In to add comment