Link List Help!

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!"

.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Specification file for the CircleNumList class
//Circular Linked List
#ifndef CIRCLENUMLIST_H
#define CIRCLENUMLIST_H
#include<iostream>
using namespace 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.
   else if (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 


.cpp main file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include"CircleNumList.h"
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
  CircleNumList <string> list;
  
  int count = 0;
  string array[100];

  ifstream file;

  file.open("Josephus.txt");
  
  if(file)
    {
      while(count < 100 && file >> array[count])
	count++;
    }
  
  else
    cout << "file doesnt exist." << endl;

  file.close();
  for(int i =0; i< count; i++)
    list.appendNode(array[i]);

  
  
  
  return 0;
}


¿what's the problem?
read the second paragraph above
That's the description of your assignment. ¿what is giving you trouble?
This is not a homework service.
how do i find out which element of the list is the shortest? and is it best to do that in the .h file or in the main file?
you should make a new function, "getlist", or something like that, like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template<class T>
void CircleNumList<T>::getList(vector<T>& array) const
{
   ListNode *nodePtr;
   nodePtr = head;
   if(!head)
   	cout<<"Empty list\n";
   else
   {
  do
   {
     array.push_back(nodePtr->value);
     nodePtr = nodePtr->next;
   }
  while (nodePtr!=head);
}
}
, 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.
Topic archived. No new replies allowed.