Thread: Printing out in wrong order

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    109

    Printing out in wrong order

    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;
    
    }
    Last edited by DarkDot; 05-03-2007 at 04:33 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think that one of these loops is probably wrong, because it looks like they're supposed to do the same thing:
    Code:
            //the for loop to walk to the end of the list
            for(i=1;i<=size;i++)
            {
                tailPtr = tailPtr->nextPtr;
            }
    Code:
            //walk through the list until it reaches the last node
            for(i=1;i<size;i++)
            {
                tailPtr = tailPtr->nextPtr;
    	    }
    Why not just use this or something similar?
    Code:
    for(tailPtr = headPtr; tailPtr->next != headPtr; tailPtr = tailPtr->next);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FFT returning values in wrong order
    By DavidP in forum C# Programming
    Replies: 3
    Last Post: 10-25-2007, 01:15 PM
  2. Problem with ascending order Program
    By cashmerelc in forum C Programming
    Replies: 4
    Last Post: 09-21-2007, 05:36 PM
  3. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM