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

UnsortedArray

The document describes the implementation of an unsorted list array using the UnsortedType class in C++. It explains the difference between MAX_ITEMS, which defines the maximum capacity of the array, and length, which tracks the current number of elements. Additionally, it includes examples of functions for inserting, deleting, retrieving items, and exercises for splitting and merging unsorted lists.

Uploaded by

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

UnsortedArray

The document describes the implementation of an unsorted list array using the UnsortedType class in C++. It explains the difference between MAX_ITEMS, which defines the maximum capacity of the array, and length, which tracks the current number of elements. Additionally, it includes examples of functions for inserting, deleting, retrieving items, and exercises for splitting and merging unsorted lists.

Uploaded by

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

Lab Session No.

3
Unsorted list array

Unsorted list array: A list in which data items are placed in no particular order.

Understanding the difference between MAX_ITEMS and length is crucial when working
with an array-based list, such as the UnsortedType class. Here's a detailed explanation:

MAX_ITEMS: MAX_ITEMS is a constant that defines the maximum capacity of the


array that stores the list items.

Purpose: It sets the limit on how many items the list can hold.
The array info is declared with this size: ItemType info[MAX_ITEMS];.

length:length is an integer variable that keeps track of the current number of elements in
the list.
It is initialized in the constructor and updated as items are inserted or deleted from the
list.

Purpose: It represents the actual number of items currently stored in the list, which can
be less than or equal to MAX_ITEMS.

Example:
Initialization:
MAX_ITEMS is set to 100, meaning the array can hold up to 100 items.
length is initially 0, indicating the list is empty.

Example:

#include <iostream>
using namespace std;

const int MAX_ITEMS = 100; // Maximum number of items in the list

template<class ItemType>
class UnsortedType {
public:
UnsortedType(); // Constructor to initialize the list
void MakeEmpty(); // Function to make the list empty
bool IsFull() const; // Function to check if the list is full
int LengthIs() const; // Function to get the length of the list
void RetrieveItem(ItemType& item, bool& found); // Function to retrieve an item
void InsertItem(ItemType item); // Function to insert an item
void DeleteItem(ItemType item); // Function to delete an item
void ResetList(); // Function to reset the list iterator
bool IsLastItem(); // Function to check if the current item is the last item
void GetNextItem(ItemType& item); // Function to get the next item in the list

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

template<class ItemType>
UnsortedType<ItemType>::UnsortedType() { // Constructor
length = 0;
currentPos = -1;
}

template<class ItemType>
void UnsortedType<ItemType>::MakeEmpty() { // make the list empty
length = 0;
}

template<class ItemType>
bool UnsortedType<ItemType>::IsFull() const { // check if the list is full

return (length == MAX_ITEMS);


}

template<class ItemType>
int UnsortedType<ItemType>::LengthIs() const { // get the length of the list
return length;
}

template<class ItemType> //retrieve an item


void UnsortedType<ItemType>::RetrieveItem(ItemType& item, bool& found) {
found = false;
for (int i = 0; i < length; i++) { // In the chapter, (while) was used.
if (info[i] == item) {
item = info[i];
found = true;
return;
}
}
}

template<class ItemType>
void UnsortedType<ItemType>::InsertItem(ItemType item) { //insert an item
if (!IsFull()) {
info[length] = item;
length++;
} else {
cout << "List is full. Cannot insert." << endl;
}
}

template<class ItemType>
void UnsortedType<ItemType>::DeleteItem(ItemType item) { //delete an item
for (int i = 0; i < length; i++) {
if (info[i] == item) {
info[i] = info[length - 1];
length--;
return;
}
}
cout << "Item not found. Cannot delete." << endl;
}

template<class ItemType>
void UnsortedType<ItemType>::ResetList() {// to reset the list iterator

currentPos = -1;
}

template<class ItemType>
bool UnsortedType<ItemType>::IsLastItem() { //check if the current item is the last item
return (currentPos == length - 1);
}

template<class ItemType>
void UnsortedType<ItemType>::GetNextItem(ItemType& item) { //get the next item in
the list
if (currentPos < length - 1) {
currentPos++;
item = info[currentPos];
} else {
cout << "No more items in the list." << endl;
}
}

int main() {

UnsortedType<int> list;

// Insert items
list.InsertItem(5);
list.InsertItem(10);
list.InsertItem(3);
list.InsertItem(8);

cout << "List length: " << list.LengthIs() << endl;

// Retrieve and print items


int item;
bool found;

item = 10;
list.RetrieveItem(item, found);
if (found) {
cout << "Item " << item << " found in the list." << endl;
} else {
cout << "Item " << item << " not found in the list." << endl;
}

// Delete an item
list.DeleteItem(10);
cout << "List length after deleting item 10: " << list.LengthIs() << endl;

// Iterate and print all items


list.ResetList();
while (!list.IsLastItem()) {
list.GetNextItem(item);
cout << item << " ";
}
cout << endl;

return 0;
}
 Exercise 1: Write a client function that splits an unsorted list into two unsorted
lists using the following specification.
void SplitLists (UnsortedType list, ItemType item, UnsortedType& list1,
UnsortedType& list 2)
Function: Divides list into two lists according to the key of item.
Preconditions: list has been initialized and is not empty.
Postconditions: list1 contains all the items of list whose keys are less than
or equal to item’s key; list2 contains all the items of list whose keys are greater
than item’s key; list remains unchanged.
Solution:

#include <iostream>
using namespace std;

const int MAX_ITEMS = 100;

template<class ItemType>
class UnsortedType {
public:
UnsortedType();
void MakeEmpty();
bool IsFull() const;
int LengthIs() const;
void RetrieveItem(ItemType& item, bool& found);
void InsertItem(ItemType item);
void DeleteItem(ItemType item);
void ResetList();
bool IsLastItem();
void GetNextItem(ItemType& item);

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

template<class ItemType>
UnsortedType<ItemType>::UnsortedType() {
length = 0;
currentPos = -1;
}
template<class ItemType>
void UnsortedType<ItemType>::MakeEmpty() {
length = 0;
}

template<class ItemType>
bool UnsortedType<ItemType>::IsFull() const {
return (length == MAX_ITEMS);
}

template<class ItemType>
int UnsortedType<ItemType>::LengthIs() const {
return length;
}

template<class ItemType>
void UnsortedType<ItemType>::RetrieveItem(ItemType& item, bool& found) {
found = false;
for (int i = 0; i < length; i++) {
if (info[i] == item) {
item = info[i];
found = true;
return;
}
}
}

template<class ItemType>
void UnsortedType<ItemType>::InsertItem(ItemType item) {
if (!IsFull()) {
info[length] = item;
length++;
} else {
cout << "List is full. Cannot insert." << endl;
}
}

template<class ItemType>
void UnsortedType<ItemType>::DeleteItem(ItemType item) {
for (int i = 0; i < length; i++) {
if (info[i] == item) {
info[i] = info[length - 1];
length--;
return;
}
}
}

template<class ItemType>
void UnsortedType<ItemType>::ResetList() {
currentPos = -1;
}

template<class ItemType>
bool UnsortedType<ItemType>::IsLastItem() {
return (currentPos == length - 1);
}

template<class ItemType>
void UnsortedType<ItemType>::GetNextItem(ItemType& item) {
if (currentPos < length - 1) {
currentPos++;
item = info[currentPos];
} else {
cout << "No more items in the list." << endl;
}
}

// SplitLists function
template<class ItemType>
void SplitLists(UnsortedType<ItemType> list, ItemType item,
UnsortedType<ItemType>& list1, UnsortedType<ItemType>& list2) {
list.ResetList();
ItemType currentItem;
while (!list.IsLastItem()) {
list.GetNextItem(currentItem);
if (currentItem <= item) {
list1.InsertItem(currentItem);
} else {
list2.InsertItem(currentItem);
}
}
}

int main() {
UnsortedType<int> list;
UnsortedType<int> list1;
UnsortedType<int> list2;

// Insert items into the original list


list.InsertItem(5);
list.InsertItem(10);
list.InsertItem(3);
list.InsertItem(8);
list.InsertItem(7);

int pivot = 7;

// Split the list


SplitLists(list, pivot, list1, list2);

// Output the contents of list1 and list2


int item;
list1.ResetList();
cout << "List1 (<= " << pivot << "): ";
while (!list1.IsLastItem()) {
list1.GetNextItem(item);
cout << item << " ";
}
cout << endl;

list2.ResetList();
cout << "List2 (> " << pivot << "): ";
while (!list2.IsLastItem()) {
list2.GetNextItem(item);
cout << item << " ";
}
cout << endl;

return 0;
}

 Exercise 2: Write a client function that merges two unsorted lists into another
unsorted list using the following specification.
 MergeLists (UnsortedType list1, UnsortedType list2, UnsortedType& list)

 Function: Merge list1 and list2 into another list.
 Preconditions: list1 and list2 have been initialized.
 Postconditions: list contains all the items of list1 and list2; list is not
allowed to contain duplicates.
Solution:

You might also like