C++ Splitlists( listType& list1, listType& list2,ItemType item) Function: Divides list into two lists according to the key of item. Precondition: 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 key. List2 contains all the items of list whose keys are greater than items key. driver (.cpp) file in which you declare two objects of class Unsorted List one of which contains integers into info part and the other one contains characters into info part. Both objects should not have less than 20 nodes. a. Implement splitLists as a member function of the Unsorted List ADT. #include #ifndef UNSORT_H #define UNSORT_H template
C++
Splitlists( listType& list1, listType& list2,ItemType item) Function: Divides list into two lists according to the key of item. Precondition: 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 key. List2 contains all the items of list whose keys are greater than items key.
driver (.cpp) file in which you declare two objects of class Unsorted List one of which contains
integers into info part and the other one contains characters into info part. Both objects should not have
less than 20 nodes.
a. Implement splitLists as a member function of the Unsorted List ADT.
#include<iostream>
#ifndef UNSORT_H
#define UNSORT_H
template <class ItemType>
struct NodeType
{
ItemType info;
NodeType* next;
};
template <class ItemType>
class UnsortedType
{
public:
UnsortedType();
~UnsortedType();
bool full() const;
int lengthIs() const;
void makeEmpty();
void retrieveItem(ItemType& item, bool& found);
void insertItem(const ItemType& item);
void deleteItem(const ItemType& item);
void resetList();
void getNextItem(ItemType& item);
bool atEnd();
private:
NodeType<ItemType>* listData;
int length;
NodeType<ItemType>* currentPos;
};
template <class ItemType>
UnsortedType<ItemType>::UnsortedType()
{
length = 0;
listData = NULL;
}
template <class ItemType>
UnsortedType<ItemType>:: ~UnsortedType()
{
NodeType<ItemType>* location;
while (listData != NULL)
{
location = listData;
listData = listData->next;
delete location;
}
length = 0;
}
template <class ItemType>
bool UnsortedType<ItemType>::full() const
{
NodeType<ItemType>* ptr;
ptr = new NodeType<ItemType>;
if (ptr == NULL)
return true;
else
{
delete ptr;
return false;
}
}
template <class ItemType>
int UnsortedType<ItemType>::lengthIs() const
{
return length;
}
template <class ItemType>
void UnsortedType<ItemType>::makeEmpty()
{
NodeType<ItemType>* location;
while (listData != NULL)
{
location = listData;
listData = listData->next;
delete location;
}
length = 0;
}
template <class ItemType>
void UnsortedType<ItemType>::retrieveItem(ItemType& item,
bool& found)
{
bool moreToSearch;
NodeType<ItemType>* location;
location = listData;
found = false;
moreToSearch = (location != NULL);
while (moreToSearch && !found)
{
if (item == location->info)
{
found = true;
item = location->info;
}
else
{
location = location->next;
moreToSearch = (location != NULL);
}
}
}
template <class ItemType>
void UnsortedType<ItemType>::insertItem(const ItemType & item)
{
NodeType<ItemType>* location;
location = new NodeType<ItemType>;
location->info = item;
location->next = listData;
listData = location;
length++;
}
template <class ItemType>
void UnsortedType<ItemType>::deleteItem(const ItemType& item)
{
NodeType<ItemType>* location = listData;
NodeType<ItemType>* tempLocation;
if (item == listData->info)
{
tempLocation = location;
listData = listData->next;
}
else
{
while (!(item == (location->next)->info))
location = location->next;
tempLocation = location->next;
location->next = (location->next)->next;
}
delete tempLocation;
length--;
}
template <class ItemType>
void UnsortedType<ItemType>::resetList()
{
currentPos = NULL;
}
template <class ItemType>
void UnsortedType<ItemType>::getNextItem(ItemType& item)
{
if (currentPos == NULL)
currentPos = listData;
else
item = currentPos->info;
currentPos = currentPos->next;
}
template <class ItemType>
bool UnsortedType<ItemType>::atEnd()
{
return currentPos == NULL;
}
#endif
Trending now
This is a popular solution!
Step by step
Solved in 3 steps