UnsortedList

생성일: 2021년 9월 10일 오후 9:42

ADT Unsorted List Ooperations

Transformers

  • MakeEmpty
  • InsertItem
  • DeleteItem

Observers

  • IsFull
  • LengthIs
  • RetrieveItem

Iterators

  • ResetList
  • GetNextItem

Generic Data Type

A generic data type is a type for which the operations are difined but the types of the items being manipulated ar not defined.

⇒ Integer, String 등 다양한 type에서 구동하게 한다.

구현

ItemType.h

#pragma once

const int MAX_ITEM = 5;
enum RelationType
{
	LESS,
	EQUAL,
	GREATER
};

class ItemType
{
public:
	RelationType ComparedTo(ItemType otherItem) const;
	void Print() const;
	void Initialize(int number);

private:
	int value;
};

ItemType.cpp

#include "ItemType.h"
#include <iostream>

RelationType ItemType::ComparedTo(ItemType otherItem) const
{
	if (value < otherItem.value)
		return LESS;
	else if (value > otherItem.value)
		return GREATER;
	else
		return EQUAL;
} //두 아이템을 비교하여 enum을 리턴

void ItemType::Print() const
{
	using namespace std;
	cout << value << endl;
}

void ItemType::Initialize(int number)
{
	value = number;
}

Unsorted.h

#pragma once
#include "ItemType.h"
#define MAX_ITEMS 50

class UnsortedType
{
public :
	UnsortedType();	//Constructor
	bool IsFull() const;	//Observer
	int LenghthIs() const;	//Observer
	bool RetrieveItem(ItemType& item);	//Observer
	void InsertItem(ItemType item);	//Transformer
	void DeleteItem(ItemType item);	//Transformer
	void ResetList();	//Iterator
	void GetNextItem(ItemType& item);	//Iterator

private:
	int length;
	ItemType info[MAX_ITEMS];
	int currentPos;
};

Unsorted.cpp

#include <iostream>
#include "ItemType.h"
#include "unsorted.h"

UnsortedType::UnsortedType()
{
    length = 0;
}

void UnsortedType::InsertItem(ItemType item)
{
    info[length] = item;
    length++;
}

int UnsortedType::LenghthIs() const
{
    return length;
}

bool UnsortedType::IsFull() const
{
    return (length == MAX_ITEMS);
}

bool UnsortedType::RetrieveItem(ItemType& item)
{
    bool moreToSearch;
    int location = 0;
    bool found = false;

    moreToSearch = (location < length);
    while (moreToSearch && !found)
    {
        switch (item.ComparedTo(info[location]))
        {
        case LESS:
            location++;
            moreToSearch = (location < length);
            break;
        case GREATER:
            location++;
            moreToSearch = (location < length);
            break;
        case EQUAL:
            found = true;
            break;
        default:
            std::cout << "Can not compare.";
            break;
        }
    }
    
    return found;
}

void UnsortedType::DeleteItem(ItemType item)
{
    int location = 0;

    while (item.ComparedTo(info[location]) != EQUAL)
    {
        location++;
    }

    info[location] = info[length - 1]; //제일 마지막에 있는 item을 없애고자 하는 item의 위치로 옮긴다.(Unsorted list이기 때문에 순서가 바뀌어도 OK) 
    length--;   //item을 하나 지웠으니 list의 길이도 하나 줄인다.
}

void UnsortedType::ResetList()
{
    currentPos = -1;    //커서를 제일 앞으로 옮긴다.
}

void UnsortedType::GetNextItem(ItemType& item)
{
    currentPos++;   //커서를 한칸 뒤로 옮기고 item을 가져온다.
    item = info[currentPos];
}

int main()
{
    using namespace std;
    cout << "Hello World!\n";
    UnsortedType a;
    ItemType item1;
    item1.Initialize(3);

    a.InsertItem(item1);

    cout << a.IsFull() << endl;
    cout << a.LenghthIs() << endl;
}

좋은 웹페이지 즐겨찾기