C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 1.00 average. Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 26th, 2012, 09:10 PM
lanza24 lanza24 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 lanza24 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 49 m 30 sec
Reputation 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

Reply With Quote
  #2  
Old December 27th, 2012, 01:22 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 9 m 51 sec
Reputation Power: 1774
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

Reply With Quote
  #3  
Old December 28th, 2012, 09:42 PM
lanza24 lanza24 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 lanza24 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 49 m 30 sec
Reputation Power: 0
Solved

Quote:
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Implementing Double Linked Lists as an Array of Pointers in C++

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap