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 (or please, check the attached):
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;
}
Bookmarks