Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cpp
- ~~~~~~~~~~
- #include "UnsortedType.h"
- #include <exception>
- #include <iostream>
- UnsortedType::UnsortedType(){
- length = 0;
- listData = nullptr;
- currentPos = nullptr;
- }
- UnsortedType::~UnsortedType(){
- UnsortedType o;
- o.MakeEmpty();
- }
- void UnsortedType::MakeEmpty(){
- NodeType* locationToDelete = nullptr;
- while (listData != nullptr){
- locationToDelete = listData;
- listData = listData->next;
- if (currentPos == locationToDelete){
- //Iterator is pointing to the item to be deleted
- currentPos = currentPos->next;
- }
- delete locationToDelete;
- length--;
- }
- }
- bool UnsortedType::IsFull(){
- try{
- NodeType* newNode = new NodeType;
- delete newNode;
- }
- catch (std::bad_alloc exception){
- //This means a new node can not be allocated
- //From the dynamic memory.
- //This means we have reached capacity.
- return true;
- }
- return false;
- }
- int UnsortedType::GetLength(){
- return length;
- }
- bool UnsortedType::IsEmpty(){
- return (length == 0 && listData == nullptr);
- }
- ItemType UnsortedType::GetItem(ItemType item, bool & found){
- NodeType* curr = listData;
- found = false;
- while (curr != nullptr && !found){
- if (curr->info != item)
- curr = curr->next;
- else{
- //Found a copy of the item inside the linked list.
- //return the fresh copy of the item inside the list.
- found = true;
- return curr->info;
- }
- }
- return item;
- }
- bool UnsortedType::PutItem(ItemType item){
- if (IsFull())
- return false;
- NodeType* newNode = new NodeType;
- newNode->info = item;
- newNode->next = listData;
- listData = newNode;
- length++;
- return true;
- }
- void UnsortedType::ResetList(){
- currentPos = nullptr;
- }
- bool UnsortedType::DeleteItem(ItemType item){
- NodeType* curr = listData;
- NodeType* prev = nullptr;
- bool found = false;
- while (curr != nullptr && !found){
- if (curr->info == item) {
- found = true;
- if (prev == nullptr) {
- //This means, the item to delete is the linked list
- //Head. This means, after deleteing current node,
- //we need to update listData head pointer.
- listData = curr->next;
- }
- else {
- prev->next = curr->next;
- }
- delete curr;
- length--;
- }
- else {
- prev = curr;
- curr = curr->next;
- }
- }
- return found;
- }
- bool UnsortedType::HasNextItem(){
- if (currentPos == nullptr)
- return (listData != nullptr);
- return (currentPos->next != nullptr);
- }
- ItemType UnsortedType::GetNextItem(){
- currentPos = ((currentPos == nullptr) ? listData : currentPos->next);
- return currentPos->info;
- }
- ~~~~~~
- headerfile
- ~~~~~~~~~
- #ifndef UNSORTEDTYPE_H_INCLUDED
- #define UNSORTEDTYPE_H_INCLUDED
- #include "ItemType.h"
- class UnsortedType
- {
- struct NodeType {
- ItemType info = ItemType();
- NodeType* next = nullptr;
- };
- public:
- UnsortedType();
- ~UnsortedType();
- bool IsFull();
- int GetLength();
- void MakeEmpty();
- bool IsEmpty();
- ItemType GetItem(ItemType item, bool& found);
- bool PutItem(ItemType item);
- bool DeleteItem(ItemType item);
- //Iterator Operations
- void ResetList();
- bool HasNextItem();
- ItemType GetNextItem();
- //Printing List Contents
- void PrintList();
- private:
- NodeType* listData = nullptr;
- int length = 0;
- NodeType* currentPos = nullptr;
- };
- #endif
- ~~~~~~~~~
- itemtype ( if there is a better way to code this let me know)
- ~~~~~~~
- #include <fstream>
- #include "ItemType.h"
- #include <iostream>
- using namespace std;
- ItemType::ItemType()
- {
- words;
- numbers;
- flt;
- }
- RelationType ItemType::ComparedTo(ItemType otherItem) const
- {
- if(words<otherItem.words)
- return LESS;
- else if(words>otherItem.words)
- return GREATER;
- else return EQUAL;
- }
- void ItemType::Initialize(string word)
- {
- words=word;
- }
- void ItemType::Initialize(int number)
- {
- numbers=number;
- }
- void ItemType::Initialize(float fl)
- {
- flt=fl;
- }
- void ItemType::Print(std::ofstream& dataFile) const
- {
- cout << words << " "<<endl;
- dataFile<< words << " "<<endl;
- }
- void ItemType::PrintString(std::ofstream& dataFile) const
- {
- cout<< words << " "<<endl;
- dataFile<< words << " "<<endl;
- }
- ~~~~~~~~~~~~~~
- #include <fstream>
- #include <string>
- using namespace std;
- const int MAX_ITEMS=5;
- enum RelationType {LESS, GREATER, EQUAL};
- class ItemType
- {
- public:
- ItemType();
- RelationType ComparedTo(ItemType) const;
- void Print(std::ofstream&) const;
- void PrintString(std::ofstream&) const;
- void Initialize(string words);
- void Initialize(int number);
- void Initialize(float fl);
- private:
- int numbers;
- string words;
- float flt;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement