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

Assignment 4

The document defines a template class called listType that implements a dynamic array-based list. The class contains functions to check if the list is empty/full, get the length/max size, sort, print, insert, remove, search. It also defines a constructor, destructor and includes example usage in main.

Uploaded by

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

Assignment 4

The document defines a template class called listType that implements a dynamic array-based list. The class contains functions to check if the list is empty/full, get the length/max size, sort, print, insert, remove, search. It also defines a constructor, destructor and includes example usage in main.

Uploaded by

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

#pragma once

#pragma once
#include <iostream>

#include <cassert>

using namespace std;

template <class elemType>

class listType

public:

bool isEmpty() const;

//Function to determine whether the list is empty.


//Postcondition: Returns true if the list is empty,
// otherwise it returns false.

bool isFull() const;


//Function to determine whether the list is full.
//Postcondition: Returns true if the list is full,
// otherwise it returns false.

int getLength() const;


//Function to return the number of elements in the list.
//Postcondition: The value of length is returned.

int getMaxSize() const;


//Function to return the maximum number of elements
//that can be stored in the list.
//Postcondition: The value of maxSize is returned.

void sort();
//Function to sort the list.
//Postcondition: The list elements are in ascending order.

void print() const;


//Outputs the elements of the list.

void insertAt(const elemType& item, int position);


//Function to insert item in the list at the location

//specified by position.

//Postcondition: list[position] = item; length++;

// If position is out of range, the program


// is aborted.
void remove();
void search();
listType(int listSize = 50);
//Constructor
//Creates an array of the size specified by the
//parameter listSize; the default array size is 50.
//Postcondition: list contains the base address of the
// array; length = 0; maxsize = listSize;
~listType();
//Destructor
//Deletes all the elements of the list.
//Postcondition: The array list is deleted.

private:
int maxSize; //variable to store the maximum size
//of the list
int length; //variable to store the number of elements in
// the list
elemType* list; //pointer to the array that holds the
//list elements
};
template <class elemType>
bool listType<elemType>::isEmpty()const
{
return (length == 0);
}

template <class elemType>


bool listType<elemType>::isFull() const
{
return (length == maxSize);
}

template <class elemType>


int listType<elemType>::getLength() const
{
return length;

template <class elemType>


int listType<elemType>::getMaxSize() const
{

return maxSize;

//Constructor; the default array size is 50


template <class elemType>
listType<elemType>::listType(int listSize)

maxSize = listSize;
length = 0;

list = new elemType[maxSize];


}

template <class elemType>


listType<elemType>::~listType() //destructor
{
delete[] list;
}

template <class elemType>


void listType<elemType>::sort() //selection sort
{

int min;
elemType temp;

for (int i = 0; i < length; i++)


{
min = i;
for (int j = i + 1; j < length; ++j)
if (list[j] < list[min])
min = j;
temp = list[i];
list[i] = list[min];
list[min] = temp;
}
//end for
}//end sort

template <class elemType>


void listType<elemType>::remove()
{
int index;

cout << "Enter index to be removed: ";


cin >> index;

if (index < 0 || index >= maxSize)


{
cout << "Wrong index entered" << endl;
cout << "Array: ";
for (int i = 0; i < (maxSize); i++)
{
cout << list[i] << " ";
}
}
else
{
for (int i = index; i <= maxSize - 1; i++)
{
list[i] = list[i + 1];
}
list[maxSize - 1] = {};

cout << "Array after element removed: ";


for (int i = 0; i < (maxSize - 1); i++)
{
cout << list[i] << " ";
}
}
}
template <class elemType>
void listType<elemType>::search()
{
int element, flag = 0;
cout << "Enter number to search in the array:";
cin >> element;

for (int i = 0; i < maxSize; i++)


{
if (list[i] == element)
{

cout << "Number is at index :" << i << endl;


flag = 1;
}
}
if (flag==0)
{
cout << "Number not found in the array." << endl;

}
template<class elemType>
void listType<elemType>::print() const
{

int i;

for (i = 0; i < length; ++i)

cout << list[i] << " ";

cout << endl;

}//end print

template <class elemType>


void listType<elemType>::insertAt(const elemType& item, int position)
{
assert(position >= 0 && position < maxSize);
list[position] = item;
length++;

}
-----------------------------------------------------------------------------------
------------------------------------------------------------
#include <iostream>
#include "listType.h"

using namespace std;

int main()
{
listType<int>intList(100);
int index;
int number;
cout << "-----------------------------------------------" << endl;
cout << "Enter 5 integers: " << endl;
for (index = 0; index < 5; index++)
{
cin >> number;
intList.insertAt(number, index);
}
cout << endl;
cout << "-----------------------------------------------" << endl;
cout << "intList: ";
intList.print();
cout << "-----------------------------------------------" << endl;
//Sort intList
intList.sort();
cout << " After sorting, intList: ";
intList.print();
cout << "-----------------------------------------------" << endl;
int intListSize;
cout << "Enter the size of the integer " << "list: ";
cin >> intListSize;
cout << endl;
listType<int> intList2(intListSize);
cout << "Enter " << intListSize << " integers: " << endl;
for (index = 0; index < intListSize; index++)
{
cin >> number;

intList2.insertAt(number, index);
}
cout << endl;
cout << "intList2: ";
intList2.print();
cout << "Length of intList2: " << intList2.getLength() << endl;
cout << "Maximum size of intList2: " << intList2.getMaxSize() << endl;
cout << "-----------------------------------------------" << endl;
//Sort intList2
intList2.sort();
cout << "After sorting, intList2: ";
intList2.print();
cout << "-----------------------------------------------" << endl;
intList2.search();
cout << endl;
cout << "-----------------------------------------------" << endl;
intList2.remove();
cout << endl;
cout << "-----------------------------------------------" << endl;
system("pause");
return 0;
}

You might also like