C++ program that reads a textual data file and builds linked lists using the data therein.

/*
 * pshrestha_1.cpp
 *
 *  Created on: Oct 27, 2010
 *  Write a C++ program that reads a textual data file and builds linked lists using the data therein.
Each line of the file contains a letter (either I or F) following by a corresponding numeric value.
In each line, an integral value is preceded by character I and a floating-point value is preceded
by character F. An "I integer" line may or may not be followed by one or more "F float" lines.

Your program is to process each line of the file and build linked lists. An "integer" linked list
(nodes sorted ascendingly by integer value) will be created using the "I integer" lines. "F float"
lines will begin/continue another singly linked list "off of" the integer node corresponding to
the integer value and node processed immediately before (again, any "I integer" line may or may not
be followed by one or more "F float" lines). Any list of floating-point nodes must be ordered
ascendingly.

After the file is processed and the linked lists are built, traverse your lists and display the
address and value of each node. That is, as you traverse your list of integer values, display the
address and value of each node and, if an integer node has a corresponding list of floating-point
values, traverse that list and display the address and value of each of those nodes. (Of course,
label your output appropriately.)

Create and use your own data file.
 */

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;
// Named Constant for the input file.
const char INPUT_PATH_FILE[] = "C:\\Users\\Shiva\\workspace\\dataStructure\\pshrestha_1\\progAssignOne.txt";

// Structure for our float node
struct Float_Node {
float Value; // Holds value
struct Float_Node* Next; // Holds the address.
};

// Structure for our integer node
struct Integer_Node {
int Value;
struct Float_Node* Base_Float;
struct Integer_Node* Next;
};

int main(void) {

ifstream InFile;
InFile.open(INPUT_PATH_FILE);

if (InFile) { // If input file opened successfully the the following line of code executes.
char Input_Character; // Holds character.
float Input_Float;
int Input_Integer;
struct Integer_Node* base = NULL; // Making it point to nothing.
struct Integer_Node* p;
struct Integer_Node* q;
struct Integer_Node* r;
struct Integer_Node* Ptr_Last_Int_Node; // Optional
struct Float_Node* pf;
struct Float_Node* qf;
struct Float_Node* rf;
while (InFile >> Input_Character >> Input_Float) {
// Operates until the input file has values in it.
if (Input_Character == 'I' || Input_Character == 'i') {
// Checking for both "I" and "i".
Input_Integer = (int)Input_Float;
p = Ptr_Last_Int_Node = new struct Integer_Node;
p->Value = Input_Integer;
p->Base_Float = NULL;
p->Next = NULL;
if (base) { // If base is true.
q = r = base;
while (q && p->Value > q->Value) {
r = q;
q = q->Next;
}
if (q) {
if (q != base) {
r->Next = p;
p->Next = q;
} else {
base = p;
p->Next = q;
}
} else {
r->Next = p;
}
} else {
base = p;
}
} else if (Input_Character == 'F'|| Input_Character == 'f'){
// Checking for both "F" and "f".
if (! base) {
continue;
}
pf = new struct Float_Node;
pf->Value = Input_Float;
pf->Next = NULL;
// p is pointing at the integer node for this incoming
// floating-point node.
if (Ptr_Last_Int_Node->Base_Float) {
qf = rf = Ptr_Last_Int_Node->Base_Float;
while (qf && pf->Value > qf->Value) {
rf = qf;
qf = qf->Next;
}
if (qf) {
if (qf != Ptr_Last_Int_Node->Base_Float) {
rf->Next = pf;
pf->Next = qf;
} else {
Ptr_Last_Int_Node->Base_Float = pf;
pf->Next = qf;
}
} else {
rf->Next = pf;
}
} else {
Ptr_Last_Int_Node->Base_Float = pf;
}
}
}
InFile.close (); // Closing the opened file.
// Display linked lists (addresses and values of nodes).
cout << "ADDRESS" << setw (8) << "VALUE" << endl;
cout << "-------" << setw (8) << "-----" << endl;
if (base) {
q = base;
while (q) {
cout << (unsigned)q << setw (6) << q->Value << endl;
// Displaying address of the memory and the value residing on it
qf = q->Base_Float;
while (qf) {
cout << (unsigned)qf << setw (6) << qf->Value << endl;
qf = qf->Next;
}
q = q->Next;
}
} else {
cout << endl << "\a\aNo Linked Lists Created!";
}
} else {
cout << endl << "\aCannot Open Input File";
}
return 0;
}

Comments

Popular posts from this blog

Concept of recursive function to take user input of two numbers and performs an addition of those two numbers

C++ class that implements a “stack” data structure for storing floating-point values. Required methods: Push, Pop, isEmpty, and isFull.