C++ DATA STRUCTURES UNSORTED LISTS. I\'m just trying to run the project, it seem
ID: 3877356 • Letter: C
Question
C++ DATA STRUCTURES UNSORTED LISTS.
I'm just trying to run the project, it seems that all my function declaration are fine (I called it sorted but I know it's unsorted lol), but I’m not sure how to declare the function in my main program. Here are the instructions of my project (please use my functions already declared).
An Unsorted Type ADT is to be extended by the addition of function SplitLists, which as the following specifications:
SplitLists(UnsortedType list, ItemType item, UnsortedType& list1. UnsortedType& list2)
Function: Divideslist into two lists according to the key of item.
Preconditions:list has been initialized and is not empty.
Post conditions: list1 contains all the items of list whose keys are less than or equal to items key;
list2 contains all of the items oflist whose keys are greater than items key.
a. Implement SplitLists as an array-based member function of the Unsorted List ADT.
b. Implement SplitLists as a linked member function of the Unsorted List ADT.
-----------------------------------------------------------------------------------------------------------------------------------------------
#pragma once
#include <fstream>
#include<iostream>
using namespace std;
const int MAX_ITEMS = 5;
enum RelationType {LESS, GREATER, EQUAL};
//create class type
class ItemType
{
public:
ItemType();
RelationType ComparedTo(ItemType) const;
void Print(ofstream&) const;
void Initialize(int number);
private:
int value;
};
//////////////////////////////////////////////////////
#include <fstream>
#include<iostream>
#include "main.h"
using namespace std;
ItemType::ItemType()
{
value = 0;
}
RelationType ItemType::ComparedTo(ItemType otherItem) const
{
if (value < otherItem.value)
return LESS;
else if (value > otherItem.value)
return GREATER;
else return EQUAL;
}
void ItemType::Initialize(int number)
{
value = number;
}
void ItemType::Print(ofstream& out) const
{
out << value << "";
}
//////////////////////////////////////////////////////
#pragma once
#include<iostream>
#include"main.h" //included
using namespace std;
ItemType info[MAX_ITEMS];
class SortedType {
public:
//constructor
SortedType();
void MakeEmpty();
bool IsFull () const;
ItemType GetItem(ItemType item, bool& found);
int GetLength() const;
//void SplitLists();
void PutItem(ItemType item);
//void DeleteItem(int item);
void ResetList();
ItemType GetNextItem();
private:
int length;
int currentPos;
ItemType info[MAX_ITEMS];
int currentPOS;
};
////////////////////////////////////////////////////////////////////////////////
#include<iostream>
#include "SortedList.h"
using namespace std;
SortedType::SortedType() //length starts at = 0
{
length = 0;
} //complete
bool SortedType::IsFull() const {
return (length == MAX_ITEMS);
}//COMP
int SortedType::GetLength() const { //return length
return length;
} //complete
void SortedType::MakeEmpty()
{
length = 0;
} //complete
void SortedType::PutItem(ItemType item)
{
info[length] = item;
length++;
} //complete
void SortedType::ResetList()
{
currentPos = -1;
} //complete
ItemType SortedType::GetNextItem()
{
currentPos ++;
return info[currentPos];
} //comple
ItemType SortedType::GetItem(ItemType item, bool& found)
{
bool moreToSearch;
int location = 0;
found = false;
moreToSearch = (location
while (moreToSearch && !found)
{
switch (item.ComparedTo(info[location]))
{
case LESS:
case GREATER: location++;
moreToSearch = (location
moreToSearch = (location != NULL);
break;
case EQUAL: found = true;
item = info[location];
break;
}
}
return item;
}
Explanation / Answer
Below is your functions
//Method in UnsortedType class to check if the item is lastItem in the unsorted collection
//This method is used by split method provided below
bool UnsortedType<ItemType>::IsLastItem() const
{
// checking if current position is null, then it is last item
return(currentPos == NULL);
}
//Method to split the list to two unsorted lists comparing each item with given item
void SplitLists(const UnsortedType& list, ItemType item,
UnsortedType& list1, UnsortedType& list2)
{
ItemType listItem;
//Making both lists empty
list1.MakeEmpty();
list2.MakeEmpty();
list.ResetList();
//iterating through each list till we get the last item
while (!list.IsLastItem()) {
list.GetNextItem(listItem);
// for each item in the list, comparing with the given item
//if the item in list is greater than given item, add it to list2 else list1
if(listItem > item)
list2.PutItem(listItem);
else
list1.PutItem(listItem);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.