I have a singly linked list that stores strings of names. Then there is a skip number. When it does is go through the list this amount of times removing the node it lands on and then printing it out and goes until the list is empty. It is printing out the wrong order. First this is my infile and what should be printed out. Any idea on why its printing out wrong of if something else needs to be posted by me let me know but i've been trying to figure out why its printing out the wrong order and wrong output.
6
Gauss
Dirichlet
Euler
Newton
Pythagoras
Riemann
Euclid
Fourier
The output for this file should be:
Riemann
Newton
Euler
Pythagoras
Fourier
Euclid
Dirichlet
Survivor: Gauss
this is my main
Code:
#include "sclist.h"
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
//string that makes it easier to remember the name of the file to open
//to read from
string inFileName = "josephus.in";
//file read from
ifstream inFile;
//makes a singly linked circular list
sclist<string> joe;
//variable that holds the skip amount
int skip;
//variable used in loops
int i;
//variable that represents the names getting added into the list
string name;
//a pointer to the data in a node
slnode<string>* dataStored;
//opens inFile
inFile.open(inFileName.c_str());
if (!inFile)
{
// error if not inFilename entered when prompted
cout << "ERROR: Unable to open the inFile";
exit(1);
}
//here the file will place the names and
//skip number from the josephus.in file into the list
inFile >> skip >> name;
//this will put all the names in the list and the skip
//number into the skip variable
while(name != "END")
{
//after the name and skip list is read and put into the correct
//variables the name will be put into the list
joe.InsertAtHead(name);
//here the file will place the names and
//skip number from the josephus.in file into the list
inFile >> name;
}
//close the inFile
inFile.close();
//once all the names are put into the list the josephus problem
//will actually be run, the program will start at the head and
//advance the amount of times the skip number is. Each time it goes
//through the skip number of times that person is taken out of the
//list and printed out. This will go on until there is only one name
//left in the list at which point it will print "Survivor:" then the
//name that is left
while(!joe.IsEmpty())
{
//number
for(i=1;i<skip;i++)
{
joe.Advance();
}
//after it advances the correct amount of times that name
//is deleted from the list and prints it out. It checks to see
//where the data is located. If its at the head it will use the
//remove from head function if its at the tail is uses the
//remove from tail funcition
dataStored = joe.RemoveFromHead();
if(joe.IsEmpty())
{
cout << "Survivor: ";
}
dataStored->Print();
cout << endl;
}
//signals ok to the os
return 0;
};
this is what is getting print out
Code:
Euler
Pythagoras
Euclid
Fourier
Riemann
Euler
Newton
Survivor: Gauss
and these are the functions in my template file that are used in the main
Code:
template <class T>
void sclist<T>::InsertAtHead(const T& data)
{
//variable used as counter in loop
int i;
//this is a temp pointer that points to the current node
slnode<T>* currentPtr = new slnode<T>(data);
slnode<T>* tailPtr;
//if there is nothing in the list
if(headPtr == NULL)
{
//sets the headPtr of the list to the newly created node
headPtr = currentPtr;
//since its a circular list the only nodes nextPtr must point to
//itself
currentPtr->nextPtr = headPtr;
}
//if there is something in the list
else
{
//set tailPtr equal to the head of the list
tailPtr = headPtr;
//walk through the list until it reaches the last node
for(i=1;i<size;i++)
{
tailPtr = tailPtr->nextPtr;
}
//have tail node point to new head node
tailPtr->nextPtr = currentPtr;
//have new node point to previous head node
currentPtr->nextPtr = headPtr;
//have the head pointer point to the new head node
headPtr = currentPtr;
}
//increments the size
size++;
}
//returns a pointer to the node removed from the head of the list
template <class T>
slnode<T>* sclist<T>::RemoveFromHead()
{
//taliPtr
slnode<T>* tailPtr;
slnode<T>* currentPtr;
int i;
//going to have the currentPtr set equal to the headPtr.
// Then set the headPtr
//equal to the nextPtr of the node we are removing from the head.
// Then delete
//the currentPtr because it will be pointing to the now unliked node
//if there is nothing in the list
if(headPtr == NULL)
{
return NULL;
}
else if(size == 1)
{
currentPtr = headPtr;
headPtr = NULL;
size--;
}
else
{
currentPtr = headPtr;
tailPtr = headPtr;
headPtr = headPtr->nextPtr;
//the for loop to walk to the end of the list
for(i=1;i<=size;i++)
{
tailPtr = tailPtr->nextPtr;
}
tailPtr->nextPtr = headPtr;
size--;
}
return currentPtr;
}
//moves headPtr one node forward in the list.
template <class T>
void sclist<T>::Advance()
{
//if list is empty
if(headPtr == NULL)
{
return;
}
//is just advancing one spot so just set headPtr equal to
//the nextPtr
headPtr = headPtr->nextPtr;
}