Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace MILLS_CIS279
- {
- const int MAX_ITEMS = 5;
- enum RelationType {LESS, GREATER, EQUAL};
- class ItemType
- {
- public:
- ItemType();
- RelationType ComparedTo(ItemType otherItem);
- void Print(std::ofstream&);
- void Initialize(int number);
- int value;
- };
- }
- #endif
- ItemType::ItemType()
- {
- value = 0;
- }
- void ItemType::Print(std::ofstream&)
- {
- cout << value << endl;
- }
- void ItemType::Initialize(int number)
- {
- value = number;
- }
- RelationType ItemType::ComparedTo(ItemType otherItem)
- {
- if (value < otherItem.value)
- {
- return LESS;
- }
- else if (value > otherItem.value)
- {
- return GREATER;
- }
- else return EQUAL;
- }
- }
- #ifndef UNSORTED_H_INCLUDED
- #define UNSORTED_H_INCLUDED
- #include "itemType.h"
- namespace MILLS_CIS279
- {
- class Unsorted
- {
- public:
- Unsorted();
- void MakeEmpty();
- int GetLength();
- bool isFull();
- ItemType GetItem(ItemType item, bool& found);
- void PutItem(ItemType item);
- void DeleteItem(ItemType item);
- void ResetList();
- ItemType GetNextItem();
- void PrintList(std::ofstream&);
- private:
- struct NodeType
- {
- ItemType key;
- NodeType* newItem;
- };
- int length;
- NodeType* listData;
- NodeType* currentPos;
- };
- }
- #endif
- namespace MILLS_CIS279
- {
- Unsorted::Unsorted()
- {
- listData = NULL;
- length = 0;
- }
- void Unsorted::MakeEmpty()
- {
- length = 0;
- }
- int Unsorted::GetLength()
- {
- return length;
- }
- bool Unsorted::isFull()
- {
- if (length >= MAX_ITEMS)
- {
- return true;
- }
- return false;
- }
- ItemType Unsorted::GetItem(ItemType item, bool& found)
- {
- NodeType* position;
- bool stillSearching;
- position = listData;
- found = false;
- stillSearching = (position != NULL);
- while (stillSearching && !found)
- {
- switch(item.ComparedTo(position->key))
- {
- case LESS:
- case GREATER:
- position = position->newItem;
- stillSearching = (position != NULL);
- break;
- case EQUAL:
- found = true;
- item = position->key;
- break;
- }
- }
- return item;
- }
- void Unsorted::PutItem(ItemType item)
- {
- NodeType* position = new NodeType;
- position->key = item;
- position->newItem = listData;
- listData = position;
- length++;
- }
- void Unsorted::DeleteItem(ItemType item)
- {
- NodeType* position;
- NodeType* temp;
- position = listData;
- if (item.ComparedTo(position->key) == EQUAL)
- {
- temp = position;
- listData = listData->newItem;
- }
- else
- while (!((item.ComparedTo((position->newItem)->key) == EQUAL)))
- {
- position = position->newItem;
- temp = position->newItem;
- position->newItem = (position->newItem)->newItem;
- }
- length--;
- }
- void Unsorted::ResetList()
- {
- currentPos = NULL;
- }
- ItemType Unsorted::GetNextItem()
- {
- if (currentPos == NULL)
- {
- currentPos = listData;
- }
- else
- {
- currentPos = currentPos->newItem;
- }
- return currentPos->key;
- }
- void Unsorted::PrintList(std::ofstream& outFile)
- {
- int length = this->GetLength();
- ItemType item;
- this->ResetList();
- int i;
- cout << "nList is: " << endl;
- outFile << "nList is: ";
- for (i = 1; i <= length; i++)
- {
- this->GetNextItem();
- cout << item.value << " ";
- outFile << item.value << " ";
- }
- cout << endl;
- outFile << endl;
- }
- }
- #include <iostream>
- #include "itemType.h"
- #include "Unsorted.h"
- #include <fstream>
- #include <string>
- #include <ostream>
- using namespace std;
- using namespace MILLS_CIS279;
- void PrintList(Unsorted&, std::ofstream&);
- int main()
- {
- Unsorted list;
- ItemType item;
- item.value = 7;
- ifstream inFile ("listData.txt");
- ofstream outFile;
- outFile.open ("a3testStephenMills.txt");
- if (outFile.is_open())
- {
- while (inFile >> item.value)
- {
- list.PutItem(item);
- if (list.isFull())
- {
- break;
- }
- }
- }
- cout << "Length of the List: " << list.GetLength() << endl;
- outFile << "Length of the List: " << list.GetLength() << endl;
- // list.PrintList(outFile);
- item.value = 1;
- list.DeleteItem(item);
- cout << "Length of the List after delete item 1 : " << list.GetLength() << endl;
- outFile << "Length of the List after delete item 1 : " << list.GetLength() << endl;
- inFile >> item.value;
- if (!list.isFull())
- {
- item.value = 7;
- list.PutItem(item);
- }
- cout << "Length of the List after put new item : " << list.GetLength() << endl;
- outFile << "Length of the List after put new item: " << list.GetLength() << endl;
- // list.PrintList(outFile);
- inFile.close();
- outFile.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement