Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.74 KB | None | 0 0
  1. cpp
  2. ~~~~~~~~~~
  3. #include "UnsortedType.h"
  4. #include <exception>
  5. #include <iostream>
  6.  
  7.  
  8. UnsortedType::UnsortedType(){
  9.  length = 0;
  10.  listData = nullptr;
  11.  currentPos = nullptr;
  12. }
  13.  
  14.  
  15. UnsortedType::~UnsortedType(){
  16.  UnsortedType o;
  17.  o.MakeEmpty();
  18. }
  19.  
  20.  
  21. void UnsortedType::MakeEmpty(){
  22. NodeType* locationToDelete = nullptr;
  23. while (listData != nullptr){
  24.  locationToDelete = listData;
  25.  listData = listData->next;
  26.  if (currentPos == locationToDelete){
  27.  //Iterator is pointing to the item to be deleted
  28.  currentPos = currentPos->next;
  29.  }
  30.  delete locationToDelete;
  31.  length--;
  32.  }
  33. }
  34.  
  35.  
  36. bool UnsortedType::IsFull(){
  37. try{
  38.  NodeType* newNode = new NodeType;
  39.  delete newNode;
  40.  }
  41. catch (std::bad_alloc exception){
  42.  //This means a new node can not be allocated
  43.  //From the dynamic memory.
  44.  //This means we have reached capacity.
  45.  return true;
  46.  }
  47. return false;
  48. }
  49.  
  50.  
  51. int UnsortedType::GetLength(){
  52. return length;
  53. }
  54.  
  55.  
  56. bool UnsortedType::IsEmpty(){
  57. return (length == 0 && listData == nullptr);
  58. }
  59.  
  60.  
  61. ItemType UnsortedType::GetItem(ItemType item, bool & found){
  62. NodeType* curr = listData;
  63. found = false;
  64. while (curr != nullptr && !found){
  65.  if (curr->info != item)
  66.  curr = curr->next;
  67.  else{
  68.  //Found a copy of the item inside the linked list.
  69.  //return the fresh copy of the item inside the list.
  70.  found = true;
  71.  return curr->info;
  72.  }
  73.  }
  74. return item;
  75. }
  76.  
  77.  
  78. bool UnsortedType::PutItem(ItemType item){
  79. if (IsFull())
  80.  return false;
  81. NodeType* newNode = new NodeType;
  82.  newNode->info = item;
  83.  newNode->next = listData;
  84.  listData = newNode;
  85.  length++;
  86. return true;
  87. }
  88.  
  89.  
  90. void UnsortedType::ResetList(){
  91.  currentPos = nullptr;
  92. }
  93.  
  94.  
  95. bool UnsortedType::DeleteItem(ItemType item){
  96. NodeType* curr = listData;
  97. NodeType* prev = nullptr;
  98. bool found = false;
  99. while (curr != nullptr && !found){
  100.  if (curr->info == item) {
  101.  found = true;
  102.  if (prev == nullptr) {
  103.  //This means, the item to delete is the linked list
  104. //Head. This means, after deleteing current node,
  105. //we need to update listData head pointer.
  106.  listData = curr->next;
  107.  }
  108.  else {
  109.  prev->next = curr->next;
  110.  }
  111.  delete curr;
  112.  length--;
  113.  }
  114.  else {
  115.  prev = curr;
  116.  curr = curr->next;
  117.  }
  118.  }
  119. return found;
  120. }
  121.  
  122.  
  123. bool UnsortedType::HasNextItem(){
  124. if (currentPos == nullptr)
  125.  return (listData != nullptr);
  126.  
  127. return (currentPos->next != nullptr);
  128. }
  129.  
  130.  
  131. ItemType UnsortedType::GetNextItem(){
  132.  currentPos = ((currentPos == nullptr) ? listData : currentPos->next);
  133. return currentPos->info;
  134. }
  135. ~~~~~~
  136. headerfile
  137. ~~~~~~~~~
  138. #ifndef UNSORTEDTYPE_H_INCLUDED
  139. #define UNSORTEDTYPE_H_INCLUDED
  140. #include "ItemType.h"
  141. class UnsortedType
  142. {
  143. struct NodeType {
  144.  ItemType info = ItemType();
  145.  NodeType* next = nullptr;
  146.  };
  147. public:
  148.  UnsortedType();
  149.  ~UnsortedType();
  150.  bool IsFull();
  151. int GetLength();
  152. void MakeEmpty();
  153. bool IsEmpty();
  154. ItemType GetItem(ItemType item, bool& found);
  155. bool PutItem(ItemType item);
  156. bool DeleteItem(ItemType item);
  157. //Iterator Operations
  158. void ResetList();
  159. bool HasNextItem();
  160. ItemType GetNextItem();
  161. //Printing List Contents
  162. void PrintList();
  163. private:
  164. NodeType* listData = nullptr;
  165. int length = 0;
  166. NodeType* currentPos = nullptr;
  167. };
  168. #endif
  169. ~~~~~~~~~
  170. itemtype ( if there is a better way to code this let me know)
  171. ~~~~~~~
  172. #include <fstream>
  173. #include "ItemType.h"
  174. #include <iostream>
  175. using namespace std;
  176.  
  177. ItemType::ItemType()
  178. {
  179.         words;
  180.         numbers;
  181.         flt;
  182. }
  183.  
  184. RelationType ItemType::ComparedTo(ItemType otherItem) const
  185. {
  186.         if(words<otherItem.words)
  187.                 return LESS;
  188.         else if(words>otherItem.words)
  189.                 return GREATER;
  190.         else return EQUAL;
  191. }
  192.  
  193.  
  194. void ItemType::Initialize(string word)
  195. {      
  196.         words=word;
  197. }
  198.  
  199. void ItemType::Initialize(int number)
  200. {      
  201.         numbers=number;
  202. }
  203. void ItemType::Initialize(float fl)
  204. {      
  205.         flt=fl;
  206. }
  207. void ItemType::Print(std::ofstream& dataFile) const
  208. {
  209.         cout << words << " "<<endl;
  210.         dataFile<< words << " "<<endl;
  211.         }
  212. void ItemType::PrintString(std::ofstream& dataFile) const
  213. {
  214.         cout<< words << " "<<endl;
  215.         dataFile<< words << " "<<endl;
  216.         }
  217.  
  218. ~~~~~~~~~~~~~~
  219. #include <fstream>
  220. #include <string>
  221. using namespace std;
  222.  
  223. const int MAX_ITEMS=5;
  224. enum RelationType {LESS, GREATER, EQUAL};
  225.  
  226. class ItemType
  227. {
  228.         public:
  229.                 ItemType();
  230.                 RelationType ComparedTo(ItemType) const;
  231.                 void Print(std::ofstream&) const;
  232.                 void PrintString(std::ofstream&) const;
  233.                 void Initialize(string words);
  234.                 void Initialize(int number);
  235.                 void Initialize(float fl);
  236.                
  237.         private:
  238.                 int numbers;
  239.                 string words;
  240.                 float flt;
  241. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement