Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. template <typename T>struct node
  7. {
  8. T data;
  9. node *prev;
  10. };
  11.  
  12. template <class T> class templateinterface
  13. {
  14. int numberofitems;
  15. node<T> *head;
  16. node<T> *current;
  17. node<T> *previous;
  18.  
  19. public:
  20. templateinterface()
  21. {
  22. head = new node<T>;
  23. current = head;
  24. previous = head;
  25. }
  26. bool isEmpty()
  27. {
  28. bool re = false;
  29. if(numberofitems == 0)
  30. {
  31. re = true;
  32. }
  33. return re;
  34. }
  35. bool add(T newEntry)
  36. {
  37. current = new node<T>;
  38. current->data = newEntry;
  39. current ->prev = NULL;
  40. previous->prev = current;
  41. previous = current;
  42. numberofitems++;
  43. return (true);
  44. }
  45. bool remove(T anEntry)
  46. {
  47. node<T> *runner;
  48. bool found = false;
  49.  
  50. runner = head;
  51. while(runner->prev != NULL)
  52. {
  53. if (runner->data == anEntry)
  54. {
  55. runner->prev = runner->prev->prev;
  56. numberofitems-=1;
  57. found = true;
  58. }
  59. else
  60. {
  61. runner = runner->prev;
  62. }
  63. }
  64. return (found);
  65. }
  66. void clear()
  67. {
  68. node<T> *runner;
  69. node<T> *temp;
  70.  
  71. runner = head;
  72. while(runner->prev != NULL)
  73. {
  74. temp = runner->prev;
  75. runner->data = NULL;
  76. runner->prev = NULL;
  77. runner = temp;
  78. }
  79.  
  80. }
  81. int getFrequencyOf(T anEntry)
  82. {
  83. node<T> *runner;
  84. int index = 0;
  85.  
  86. runner = head;
  87. while(runner->prev != NULL)
  88. {
  89. if (runner->data == anEntry)
  90. {
  91. index+= 1;
  92. cout << index;
  93. }
  94. runner = runner->prev;
  95. }
  96. return index;
  97. }
  98. bool contains(T anEntry)
  99. {
  100. node<T> *runner;
  101. bool found = false;
  102.  
  103. runner = head;
  104. while(runner->prev != NULL)
  105. {
  106. if (runner->data == anEntry)
  107. {
  108. runner->prev = runner->prev->prev;
  109. found = true;
  110. }
  111. else
  112. {
  113. runner = runner->prev;
  114. }
  115. }
  116. return found;
  117. }
  118. };
  119.  
  120. int main()
  121. {
  122. templateinterface<string> face;
  123. string anEntry;
  124. string test = "house";
  125.  
  126. cout << "Reading file ... Words.txt " << endl;
  127. ifstream infile("Words.txt");
  128. while(!infile.eof())
  129. {
  130. infile >> anEntry;
  131. face.add(anEntry);
  132. }
  133. cout << "The word house " << (face.contains() ? "is" : "is not") << " in the file"
  134. << " (occurs " << face.getFrequencyOf(test) << " times)" << endl;
  135. return (0);
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement