• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 1 out of 3 pages
Other

Sorted and Unsorted List CPP File

Document preview thumbnail
Preview 1 out of 3 pages

The code defines two classes, `UnsortedList` and `SortedList`, to manage collections of integers. ### UnsortedList - **Constructor/Destructor**: Initializes the list with a length of 0. - **insertItem(int id)**: Inserts an integer at the end of the list and increments the length. - **deleteItem(int id)**: Searches for an item; if found, it replaces it with the last element and decrements the length. If not found, it outputs a message. - **isFull() / isEmpty()**: Checks if the list is full or empty based on its length. - **displayList()**: Displays all items in the list or indicates if the list is empty. ### SortedList - **Constructor/Destructor**: Initializes the list with a length of 0. - **insertItem(int item)**: Uses binary search to find the correct insert position, shifts elements, and adds the item to maintain order. - **binarySearch(int item)**: Implements a binary search algorithm to locate the position for inserting an item. - **deleteItem(int item)**: Uses linear search to find and remove an item, shifting remaining elements and updating the length. - **linearSearch(int item)**: Searches for an item and returns its location. - **isFull() / isEmpty()**: Checks if the list is full or empty. - **displayList()**: Outputs each number in the list. Both classes manage integers and offer basic functionalities such as insertion, deletion, and display of the list, accommodating both unsorted and sorted data structures.

Content preview

#include "UnsortedSorted.h"

UnsortedList::UnsortedList()
{
length = 0;
}
UnsortedList::~UnsortedList() {}

void UnsortedList::insertItem(int id)
{
idNumbers[length] = id;
length++;
}

void UnsortedList::deleteItem(int id)
{
int location = 0;

while (id != idNumbers[location] && location < length)
location++;

// if (location < length)
// {
// idNumbers[location] = idNumbers[length - 1];
// length--;
// }
if (idNumbers[length] == id) // both work
{
idNumbers[location] = idNumbers[length-1];
length--;
}
if (location == length)
{
cout << "The item is not in the list." << endl;
}
}

bool UnsortedList::isFull() const
{
return (length == SIZE);
}
bool UnsortedList::isEmpty() const
{
return (length == 0);
}
void UnsortedList::displayList() const
{
int location = 0;

while (location < length)
{
cout << idNumbers[location] << endl;
location++;
}
if (location > length)
{
cout << "The list is empty." << endl;
}
}

Document information

Uploaded on
November 6, 2025
Number of pages
3
Written in
2025/2026
Type
Other
Person
Unknown
$15.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
68
Last sold
-



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions