Escrito por estudiantes que aprobaron Inmediatamente disponible después del pago Leer en línea o como PDF ¿Documento equivocado? Cámbialo gratis 4,6 TrustPilot
logo-home
Document preview thumbnail
Vista previa 1 fuera de 3 páginas
Otro

Sorted and Unsorted List CPP File

Document preview thumbnail
Vista previa 1 fuera de 3 páginas

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.

Vista previa del contenido

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

Información del documento

Subido en
6 de noviembre de 2025
Número de páginas
3
Escrito en
2025/2026
Tipo
Otro
Personaje
Desconocido
$15.99

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Vendido
0
Seguidores
0
Artículos
68
Última venta
-



Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes