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
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
#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
#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 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; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #pragma once #include #include #include 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 #include #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 << ""; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started