0% found this document useful (0 votes)
9 views

Assignment - 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Assignment - 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ASSIGNMENT-1

Topic Name : Operation in a sorted list


Name: Emon Hossen
ID: 2211106042
Section:08
//main.cpp
#include <iostream>
#include "sortedtype.h"
#include "sortedtype.cpp"

int main() {
SortedType<int> myList;

// Getting 5 integer inputs from the user


std::cout << "Enter 5 integers : ";

for (int i = 0; i < MAX_ITEMS; i++) {


int input;

std::cin >> input;


myList.InsertItem(input);
}

// Printing the sorted list


std::cout << "Sorted list: ";
int item;
myList.ResetList();
for (int i = 0; i < myList.LengthIs(); i++) {
myList.GetNextItem(item);
std::cout << item << " ";
}
std::cout << std::endl;

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);

// Printing the sorted list after delete


std::cout << "Sorted list after delete: ";
myList.ResetList();
for (int i = 0; i < myList.LengthIs(); i++) {
myList.GetNextItem(item);
std::cout << item << " ";
}
std::cout << std::endl;
break;
}

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);

// Printing the sorted list after insert


std::cout << "Sorted list after insert: ";
myList.ResetList();
for (int i = 0; i < myList.LengthIs(); i++) {
myList.GetNextItem(item);
std::cout << item << " ";
}
std::cout << std::endl;
}
break;
}

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];
}
}
}

You might also like