#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Question HELP Implementing Double Linked Lists as an Array of Pointers in C++


    Hi Everyone,

    I am studying/writing/ code for a future Advanced Data Structure class in C++; however I am not suppose to use STL, Templates and etc (the way I just code "kinda" of resembles what I have to use).

    My application is suppose to read/analyze all integers contained in a text file of 5-digit integers (10000 99999).

    For simplicity I am using the following input:

    20007 20008 20009
    20010 20010
    20012 20012
    20013
    20014 20010
    20015
    20016

    10000 10009 10009 10003
    10004 10005
    10006 10002
    10008 10003
    10007
    10013

    20151 20151
    20152

    30734
    30735
    30736
    30736 30738
    30738
    30739
    30740
    30741
    30742
    30743 30744 30745 30746 30747

    30748

    So far, my code is not displaying/printing the lists separated by the first digit of these 5-digits integers. I am expecting it to display/print logically/similar to the following:

    Output:

    Results for input file numbers.txt:
    27 total integers read from file

    The 3 unique integers beginning with digit 1 were
    18399 17342 19948

    The 6 unique integers beginning with digit 3 were
    39485 34710 31298 38221 35893 32791

    The 4 unique integers beginning with digit 4 were
    43928 49238 45678 43210

    The 6 unique integers beginning with digit 6 were
    64545 62987 66221 61777 66666 65432

    The 2 unique integers beginning with digit 8 were
    88888 86861

    The 1 unique integer beginning with digit 9 was
    98765

    There were 22 unique 5-digit integers in the file.
    The highest unique count in one list was 6 integers.


    My code that will follow soon displays/prints only the LAST 5-digits "group" of integers (in this case the 5-digits starting with 3). I am not sure what's wrong with my code; perhaps I am not designing it correctly. May be my calls in it are on the wrong place or I have to write all integers and then traverse it and output it (if that's the case, I am not sure how).

    Please, any directions, any ideas will be greatly appreciated.

    My code follows:
    **********************************************
    Code:
    #include <iostream> 
    #include <fstream>
    #include <iomanip>
    
    using namespace std;  
    
    const int MAX_CELLS = 10;        
    const int UNIQUE_FIVE_DIGIT = 5; 
    
    struct node
    {
        node* prev;                                            
        node* next;                                        
        int   data;                                        
    };           
    /////////////////////////////////////////////////////////////////////
    int main(int argc, char* argv[]) 
    {
        ifstream inFile;                      
        node *lists[MAX_CELLS] = { NULL };      
        int count = -1;                       
        bool result = true;                     
    
        do 
        {
    
            if ( argc != 2 ) 
            {
                cout << "Command line arguments not valid!" << endl << endl;
                result = false;
            }   
    
            if ( !argv[1] ) 
            {
                cout << "The command line arguments does not specify any filename!" << endl; 
                result = false;
            }           
    
            inFile.open(argv[1]);
    
            if ( !inFile ) 
            {
                cout << "The input data file does not exist or cannot be opened!" << endl;
                result = false;
            }   
    
            if ( result )
            {
                cout << "Results for input file " << argv[1] << ":" << endl;
                ReadInFile(inFile, lists, count);   
    
                result = false;  
            }
    
        } while ( result );
    
        system("PAUSE");  
        return 0;
    }
    ///////////////////////////////////////////////////////////////////
    void ReadInFile(ifstream& inFile, node* arrayPtr[], int& count) 
    {
        int number = 0;
        int newNumber = 0;
        int countResult = 0;
        node* p = new node;
    
        while ( inFile )   
        {
    
            inFile >> number;
            newNumber = number / 10000; 
    
            if ( !isDuplicate(arrayPtr[newNumber], number) )
            {
                    arrayPtr[newNumber] = prepend( arrayPtr[newNumber], number );
                    p = arrayPtr[newNumber];
            }
    
            count++;
        }
    
        for (int count = 0; count < 10; count++)
        {    
            arrayPtr[count] = p;
            countResult = counting(p);
        }
    
        cout << setw(7) << count << " total integers read from file" << endl << endl;
    
        inFile.close();    
    
        cout << "The " << countResult << " unique integers beginning with digit " << newNumber <<" were " << endl;
    
        displayIt(p, countResult);
    }
    /////////////////////////////////////////////////////////////////////
    void displayIt(node* p, int count) 
    {
        cout << setw(14); 
        print_list(p);
        cout << endl;
    }
    /////////////////////////////////////////////////////////////////////
    bool isDuplicate(node* top, int number) 
    {
    
        while( top != NULL )
        {
    
            if( top->data==number )
                return true;
    
            top = top->next;
        } 
    
        return false;
    }
    /////////////////////////////////////////////////////////////////////
    node* prepend(node* beginning, int number) 
    {
        node* p = new node;
    
        p->data = number;
        p->prev = NULL;
        p->next = beginning;
    
        if( beginning != NULL )
        {
            beginning->prev = p;
        }
    
        return p;
    }
    /////////////////////////////////////////////////////////////////////
    void print_list( node* beginning ) 
    {    
    
        for( node *p=beginning; p != NULL; p=p->next )
        {
            std::cout << p->data;
    
            if( p->next != NULL )
            {
                std::cout << " ";
            }
    
        }      
    }
    /////////////////////////////////////////////////////////////////////
    int counting( node* beginning ) 
    {
        int count(0);
    
        for( node *p=beginning; p != NULL; p=p->next )
        {
            ++count;
        }
    
        return count;
    }
    **********************************************

    Big thanks in advance.

    Marco
    Last edited by lanza24; December 27th, 2012 at 09:52 AM. Reason: Change the Icon to One More Appropriated
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Please edit your post and put [code][/code] tags around the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Solved


    Originally Posted by salem
    Please edit your post and put [code][/code] tags around the code.
    Problem solved; now I just need to figure out how this program will produce the last line of the sample output such as "The highest unique count in one list was 6 integers"

    Thanks.

    Marco

IMN logo majestic logo threadwatch logo seochat tools logo