This is what i have to do but i am kind of stuck, someone please help me out. "You are to write a program that will create a singly-linked, circular list to hold the names in the data file by appending each name to the end of the list as it is read. Once all names have been read and the list is complete, create a circular list by having the last node “point to” the first node in the list. Now, your program should begin eliminating names from the list as follows:
tart counting, clockwise, around the circle at the person with the shortest name. Once the number N, is reached (this is the integer on the first line of the data file), delete that node from the list and print the name contained in the deleted node. Now, starting with the next node, count around the circle until you reach the number N, and delete that node, again printing the name contained in the node. Continue deleting nodes from your list, in the same manner, until there is only one node left. Finally, print a message stating the name of the last person in the list – the one who gets the horse!"
// Specification file for the CircleNumList class
//Circular Linked List
#ifndef CIRCLENUMLIST_H
#define CIRCLENUMLIST_H
#include<iostream>
usingnamespace std;
template<class T>
class CircleNumList
{
private:
// Declare a structure for the list
struct ListNode
{
T value; // The value in this node
struct ListNode *next; // To point to the next node
};
ListNode *head; // List head pointer
public:
// Constructor
CircleNumList()
{ head = 0; }
// Destructor
~CircleNumList();
// Linked list operations
void appendNode(T);
void insertNode(T);
void deleteNode(T);
void displayList() const;
};
template<class T>
void CircleNumList<T>::appendNode(T num)
{
ListNode *newNode; // To point to a new node
ListNode *nodePtr; // To move through the list
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->value = num;
// If there are no nodes in the list
// make newNode the first node.
if (!head)
{
head = newNode;
newNode->next = head;
}
else // Otherwise, insert newNode at end.
{
// Initialize nodePtr to head of list.
nodePtr = head;
//cout<<nodePtr->value<<endl;
// Find the last node in the list.
while (nodePtr->next!=head)
{
nodePtr = nodePtr->next;
}
// Insert newNode as the last node.
nodePtr->next = newNode;
newNode->next = head;
}
}
//**************************************************
// displayList shows the value *
// stored in each node of the linked list *
// pointed to by head. *
//**************************************************
template<class T>
void CircleNumList<T>::displayList() const
{
ListNode *nodePtr; // To move through the list
// Position nodePtr at the head of the list.
nodePtr = head;
if(!head)
cout<<"Empty list\n";
else
{
// While nodePtr points to a node, traverse
// the list.
do
{
// Display the value in this node.
cout<<nodePtr->value<<endl;
// Move to the next node.
nodePtr = nodePtr->next;
}
while (nodePtr!=head);
}
}
//**************************************************
// The insertNode function inserts a node with *
// num copied to its value member. *
//**************************************************
template<class T>
void CircleNumList<T>::insertNode(T num)
{
ListNode *newNode; // A new node
ListNode *nodePtr; // To traverse the list
ListNode *previousNode = 0; // The previous node
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->value = num;
// If there are no nodes in the list
// make newNode the first node
if(head==0)
{
head = newNode;
head->next = head;
}
else // Otherwise, insert newNode
{
// Position nodePtr at the head of list.
nodePtr = head;
// Initialize previousNode to NULL.
previousNode = 0;
// If the new node is to be the 1st in the list,
// insert it before all other nodes.
if(newNode->value < head->value)
{
//traverse to end
while(nodePtr->next!=head)
{
nodePtr = nodePtr->next;
}
//point the last node->next to newnode
nodePtr->next = newNode;
//point newNode->next to head
newNode->next = head;
//head = newNode
head = newNode;
cout<<head->value<<endl;
}
else
{
// Skip all nodes whose value is less than num.
while (nodePtr->value < num && nodePtr->next!=head)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if(nodePtr->value < num && nodePtr->next == head)
{
nodePtr->next = newNode;
newNode->next = head;
}
//if(nodePtr->next == head && num < previousNode->value)
//{
//nodePtr->next = newNode;
// newNode->next = head;
//}
else
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
cout<<"head = "<<head->value<<endl;
}
//**************************************************
// The deleteNode function searches for a node *
// with num as its value. The node, if found, is *
// deleted from the list and from memory. *
//**************************************************
template<class T>
void CircleNumList<T>::deleteNode(T num)
{
ListNode *nodePtr; // To traverse the list
ListNode *previousNode; // To point to the previous node
ListNode *tempNode;
// If the list is empty, do nothing.
if (!head)
return;
//if there is only one node
if(head->value == num && head->next == head)
{
cout<<"Deleting the head...\n";
delete head->next;
head = 0;
}
// Determine if the first node is the one.
elseif (head->value == num)
{ tempNode = head;
//find the last node
do
{
tempNode = tempNode->next;
}
while(tempNode->next!=head);
nodePtr = head->next;
tempNode->next = head->next;
delete head;
head = nodePtr;
}
else
{
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is
// not equal to num.
while (nodePtr->next != head && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
//if at end
if(nodePtr->next == head && nodePtr->value == num)
{
previousNode->next=head;
delete nodePtr;
}
// If nodePtr is not at the end of the list,
// link the previous node to the node after
// nodePtr, then delete nodePtr.
else
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
if(head)
cout<<"head = "<<head->value<<endl;
}
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
template<class T>
CircleNumList<T>::~CircleNumList()
{
ListNode *nodePtr; // To traverse the list
ListNode *nextNode; // To point to the next node
cout<<"In the destructor...\n";
if(head)
{
// Position nodePtr at the head of the list.
nodePtr = head->next;
// While nodePtr is not at the end of the list...
while (nodePtr->next!=head)
{
// Save a pointer to the next node.
nextNode = nodePtr->next;
// Delete the current node.
delete nodePtr;
// Position nodePtr at the next node.
nodePtr = nextNode;
}
delete head;
}
}
#endif
, make another vector that has the std::string::sizes(if the names are strings) of the corresponding array elements, and get the std::min_element of the new vector, thatwill give you the iterator to the smallest value in that vector(the iterator to the element n the new vector that is at the same position, relative to the biginning of the vector as the value with the smallest size itself), and by getting the difference of that iterator and the std::vector::begin of the vector, you can find out what is the position of the sshortest value in the list.