C++ (Data Structure) Unsorted and Sorted lists. I\'m confused with an Unsorted a
ID: 3882013 • Letter: C
Question
C++ (Data Structure) Unsorted and Sorted lists.
I'm confused with an Unsorted and Sorted list implementation. I already have created an Unsorted list program by watching videos and looking up in websites. Then, I tried to use all the functions and instructions from the Textbook to create a new Unsorted List program, but I can't complete because I don't know how to declare in the main function.
My confusion is that in the first program (The one that I created using Internet info) I was able to create lists in my main function, and I tried to do the same in my second program (The implementation is a little bit different from the first one) and wasn't successful. I have the feeling that in my second program it's not possible to create the list in the code, but the list has to be created out of my files codes, AM I CORRECT?. (I KNOW I NAMED MY CLASS SORTED LIST, BUT IT'S ACTUALLY UNSORTED LIST, I just named Sorted because my next assignment will be sorted lists PLEASE DON'T GET CONFUSED)
Please guide me on how to create a list using the following code, and tell me if my code is done correctly:
#pragma once
#include<iostream>
#include"main.h" //included
using namespace std;
ItemType info[MAX_ITEMS];
class SortedType { // This is actually an unsorted type. dont get confused
public:
//constructor
SortedType();
void MakeEmpty();
bool IsFull () const;
ItemType GetItem(ItemType item, bool& found); //return item
int GetLength() const; //number of items in the list.. how many??
//void SplitLists();
void PutItem(ItemType item); // is going to add item in the list
//void DeleteItem(int item);
void ResetList();
ItemType GetNextItem(); //returns item
private:
int length;//number of item lists
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 if list is full or not
return (length == MAX_ITEMS);
}//COMP
int SortedType::GetLength() const { //return length
return length;
} //complete
void SortedType::MakeEmpty() //clear list
{
length = 0;
} //complete
void SortedType::PutItem(ItemType item)
{
info[length] = item;
length++;
} //complete
//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 <length);
while (moreToSearch && !found)
{
switch (item.ComparedTo(info[location]))
{
case LESS:
case GREATER: location++;
moreToSearch = (location<length);
moreToSearch = (location != NULL);
break;
case EQUAL: found = true;
item = info[location];
break;
}
}
return item;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <fstream>
#include<string>
#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 << "";
}
Explanation / Answer
The code seems fine.There are some points:
1.What is the need of global array ItemType info[MAX_ITEMS].
2.The array has already been defined in the class SortedType.There is no need of global one
Currently this is sufficient to create an unsorted list.Create an object of SortedType and
using its PutItem method, create an unsorted list.
The important thing is in case of SortedType(Actually sorted list), PutItem function needs to be modified. After inserting the item , you need to sort them.For sorting the following code
can be used:
int temp;
for (int i = 0; i<length; i++){
for (int j = i; j<length; j++){
if (info[i].getValue() > info[j].getValue()){ //Have getter and setter methods in
temp = info[i].getValue(); // the ItemType class.
info[i].setValue(info[j].getValue());
info[j].setValue(temp)
}
}
}
Also moreToSearch = (location != NULL); is sufficient for the case LESS/GREATER in SortedType::GetItem method.No need of the statement moreToSearch = (location != NULL);
Rest of all seems fine.
In the main just create an object of type SortedType and make the sortred list with modified PutItem method.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.