Assignment - 1
Assignment - 1
int main() {
SortedType<int> myList;
while (true) {
std::cout << "Options:\n";
std::cout << "1. Search\n";
std::cout << "2. Delete\n";
std::cout << "3. Insert\n";
std::cout << "4. Quit\n";
std::cout << "Enter your choice: ";
int choice;
std::cin >> choice;
switch (choice) {
case 1: {
// Searching operation
int searchItem;
std::cout << "Enter the value to search: ";
std::cin >> searchItem;
bool found;
myList.RetrieveItem(searchItem, found);
if (found) {
std::cout << "Item found in the list.\n";
} else {
std::cout << "Item not found in the list.\n";
}
break;
}
case 2: {
// Deleting operation
int deleteItem;
std::cout << "Enter the value to delete: ";
std::cin >> deleteItem;
myList.DeleteItem(deleteItem);
case 3: {
// Inserting operation
if (myList.IsFull()) {
std::cout << "List is full. Delete an item before inserting.\n";
} else {
int insertItem;
std::cout << "Enter the value to insert: ";
std::cin >> insertItem;
myList.InsertItem(insertItem);
case 4:
// Quiting the program
return 0;
default:
std::cout << "Invalid choice. Please try again.\n";
break;
}
}
return 0;
}
//sortedtype.h
#ifndef SORTEDTYPE_H_INCLUDED
#define SORTEDTYPE_H_INCLUDED
const int MAX_ITEMS = 5;
template <class ItemType>
class SortedType
{
public :
SortedType();
void MakeEmpty();
bool IsFull();
int LengthIs();
void InsertItem(ItemType);
void DeleteItem(ItemType);
void RetrieveItem(ItemType&,
bool&);
void ResetList();
void GetNextItem(ItemType&);
private:
int length;
ItemType info[MAX_ITEMS];
int currentPos;
};
#endif // SORTEDTYPE_H_INCLUDED
//sortedtype.cpp
#include "sortedtype.h"
template <class ItemType>
SortedType<ItemType>::SortedType()
{
length = 0;
currentPos = - 1;
}
template <class ItemType>
void SortedType<ItemType>::MakeEmpty()
{
length = 0;
}
template <class ItemType>
bool SortedType<ItemType>::IsFull()
{
return (length == MAX_ITEMS);
}
template <class ItemType>
int SortedType<ItemType>::LengthIs()
{
return length;
}
template <class ItemType>
void SortedType<ItemType>::ResetList()
{
currentPos = - 1;
}
template <class ItemType>
void
SortedType<ItemType>::GetNextItem(ItemType&
item)
{
currentPos++;
item = info [currentPos];
}
template <class ItemType>
void SortedType<ItemType>::InsertItem(ItemType
item)
{
int location = 0;
bool moreToSearch = (location < length);
while (moreToSearch)
{
if(item > info[location])
{
location++;
moreToSearch = (location < length);
}
else if(item < info[location])
moreToSearch = false;
}
for (int index = length; index > location;
index--)
info[index] = info[index - 1];
info[location] = item;
length++;
}
template <class ItemType>
void SortedType<ItemType>::DeleteItem(ItemType
item)
{
int location = 0;
while (item != info[location])
location++;
for (int index = location + 1; index < length;
index++)
info[index - 1] = info[index];
length--;
}
template <class ItemType>
void SortedType<ItemType>::RetrieveItem(ItemType&
item, bool& found)
{
int midPoint, first = 0, last = length - 1;
bool moreToSearch = (first <= last);
found = false;
while (moreToSearch && !found)
{
midPoint = (first + last) / 2;
if(item < info[midPoint])
{
last = midPoint - 1;
moreToSearch = (first <= last);
}
else if(item > info[midPoint])
{
first = midPoint + 1;
moreToSearch = (first <= last);
}
else
{
found = true;
item = info[midPoint];
}
}
}