Insert and Reverse Nodes

/*
 * midtermPraj.cpp
 *
 *  Created on: Nov 16, 2010 *
 *  
 *      Description:
 *      Write a C++ class that establishes a data structure for storing floating-point values. Implement
 *   the data structure using a singly linked list. Required methods:
 * Insert_Nodes, Display_Nodes, and Reverse_Nodes.
 */

#include <iostream>
#include <fstream>

using namespace std;
const char INPUT_PATH_FILE[] = "C:\\Users\\Shiva\\workspace\\midtermPraj\\floatingPoints.txt";

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

class linkedList {
private:
Float_Node* base;
Float_Node* p;
Float_Node* q;
//Float_Node* r;

public:
linkedList();
void insertNodes(double);
void displayNodes();
void reverseNodes();
~linkedList();
};

linkedList::linkedList() {
// Default Constructor
base = NULL;
}

linkedList::~linkedList() {
//Destructor
while(base){
p = base;
base = base->Next;
delete p;
}
}
void linkedList::insertNodes(double input) {
// Method to insert the nodes in the linked list created.
p = new struct Float_Node;
p->Value = input;
p->Next = NULL;
if(base){
q = base;
while(q->Next != NULL) {
q = q->Next;
}
q ->Next = p;
} else {
base = p;
}
}

void linkedList::displayNodes() {
// This method displays the linked list we created.
if(base){
q = base;
while(q){
cout << q->Value << endl;
q = q->Next;
}
} else {
cout << endl << "\aNo link list found. "<< endl;
}

}

void linkedList::reverseNodes() {
// This method reverses the nodes in the link list we created.
//p = new struct Float_Node;
if(base) {
q = base;
base = NULL;
while(q) {
p = new struct Float_Node;
p->Value = q->Value;
q = q->Next;
p->Next = NULL;
if (base) {
p->Next = base;
base = p;
} else {
base = p;
}
}
}
}

int main() {
linkedList obj; // Instance of the class linkedList.
double input; // Variable to hold the input from the input file "floatingPoints.txt".

ifstream InFile;
InFile.open(INPUT_PATH_FILE); // Reading the input file data.

if (InFile) {
// Executes only when input file is read successfully.
while (InFile >> input) {
// Executes until the file has data which is stored in variable input.
obj.insertNodes(input); // using the method to insert nodes creating a linked list.
}
InFile.close();
// Closing the input file as it is not necessary from this point.
cout << endl << "Inserting the nodes in the linked list......." << endl;
obj.displayNodes(); // Using the function to display the nodes that were inserted.

obj.reverseNodes(); // Now, using the function to reverse the nodes inserted.
cout << endl << "Reversing the nodes in the linked list......." << endl;
obj.displayNodes(); // Using the function to display the reversed nodes in the linked list.

} else { // Executes when the file in not opened.
cout << endl << "Error!! The file couldn't be read! Please check the path where your input file resides.";
}
return 0;
}

Advertisement: 

Contact us for Software Development, AI, and SEO Marketing - I Code Mind LLC

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.

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