Read Data From File to Linked List C++
1
Ok I take a file in it is a record of a persons first and last name. Format is like:
Trevor Johnson
Kevin Smith
Allan Harris
I need to read that file into program and so turn it into a linked list. So on the listing I can go Trevor, Kevin, Allan in a straight row but I can besides call out there terminal name when I am on their first proper noun in the listing. Lamentable if it doesn't make sense trying to explain all-time I tin.
So far I have
- // list.cpp
- // simple linked list program
- #include <STDLIB.H>
- #include <Cord>
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <cstdlib>
- using std::cout;
- using std::string;
- using std::cin;
- using std::ios;
- using std::cerr;
- using std::endl;
- using std::setiosflags;
- using std::resetiosflags;
- using std::setw;
- using std::setprecision;
- using std::ifstream;
- int listSearch = 0;
- // node object for the linked list
- struct Node {
- cord data;
- Node* nextWep;
- Node* ammoForWep;
- };
- // implement a singly linked list
- form LinkedList {protected:
- Node* front; // arrow to the front of the linked list
- Node* back; // pointer to the last node in the linked list
- public:
- // constructs an empty listing
- LinkedList() {
- front = back = NULL;
- }
- // deletes the list
- ~LinkedList() {
- // remove objects from the listing equally long equally list is not empty
- while(Length() > 0) {
- RemoveFront();
- }
- }
- // inserts a node at the front end of the listing
- void InsertFrontWep(string newValue) {
- Node* newNode = new Node;
- newNode->data = newValue;
- if (front == NULL) {
- // list must exist empty so make front end & back point to new node
- forepart = back = newNode;
- newNode->nextWep= NULL;
- } else {
- // list is not empty then insert betwixt front end and outset node
- newNode->nextWep = front;
- front = newNode;
- }
- }
- // search the listing for a target value
- // return alphabetize if found or -ane if not found
- int Search(string targetVal) {
- Node* p;
- int count = 0;
- for (p = front; p != Null; p = p->link) {
- if (p->data == targetVal) {
- return count;
- }
- count++;
- }
- return -1;
- }
- // removes a node from the front of the listing
- int RemoveFront() {
- int returnVal;
- Node *temp;
- if (front end != Cypher) {
- // list is not empty so remove & return first node
- returnVal = front->data;
- temp = front end;
- front = front->link;
- delete temp;
- } else {
- // list is empty simply return 0
- returnVal = 0;
- }
- return returnVal;
- }
- // returns the length of the list
- int Length() {
- Node* p;
- int count = 0;
- // loop through each node in the listing until we notice a null value
- for (p = front; p != NULL; p = p->link) {
- count++;
- }
- return count;
- }
- // outputs a cord containing all the data values in the list
- void Output() {
- Node* p;
- // loop through each node in the list until we find a zero value
- for (p = front; p != NULL; p = p->link) {
- cout << p->data << ", ";
- }
- }
- };
- // utilise inheritance to create a Prepare class from the LinkedList class
- class Set : public LinkedList {
- public:
- // insert a new value only if it is unique (non already in the set)
- int Search(string targetVal) {
- Node* p;
- int count = 0;
- for (p = front; p != NULL; p = p->link) {
- if (p->data == targetVal) {
- return count;
- }
- count++;
- }
- return -ane;
- }
- void Insert(string newValue) {
- Node* newNode = new Node;
- newNode->data = newValue;
- if (forepart == NULL) {
- // list must be empty and so make front & back betoken to new node
- forepart = dorsum = newNode;
- newNode->link = Cipher;
- } else {
- listSearch = Search(newValue);
- if(listSearch == -1)
- {
- // list is non empty so insert betwixt front end and showtime node
- newNode->link = front;
- front = newNode;
- }
- }
- }
- // make this the marriage of ii sets
- void Matrimony(Fix& a, Fix& b) {
- Node* p;
- int search = 0;
- for (p = a.forepart; p != Zero; p = p->link)
- {
- this->Insert(p->data);//u.Insert(p->information);
- }
- for (p = b.front; p != Naught; p= p->link)
- {
- search = this->Search(p->data);
- if (search == -ane)
- //u.Insert(p->data);
- this->Insert(p->information);
- }
- }
- // make this the intersection of two sets
- void Intersection(Fix& a, Set& b)
- {// Open Function
- Node* p;
- int search = 0;
- for(p = a.front; p != NULL; p = p->link) { //Open up for statment
- search = b.Search(p->data);
- if(search != -one){ // Open if argument
- //i.Insert(p->data);
- this->Insert(p->data);
- }
- } // Close For statement
- } // Shut Function
- };
- void main() {
- ifstream inResources("resource.txt", ios::in);
- if (!inResources)
- {
- cerr << "File could not be opened\n";
- }
- Set setA, setB, setUnion, setIntersection;
- setA.Insert('1');
- setA.Insert('ane');
- setA.Insert('one');
- setA.Insert('1');
- setA.Insert('i');
- cord text_line;
- cout << "Contents of setA: ";
- setA.Output();
- cout << "\north\n";
- setB.Insert('ane');
- setB.Insert('ane');
- setB.Insert('ane');
- cout << "Contents of setB: ";
- setB.Output();
- cout << "\due north\due north";
- setUnion.Union(setA, setB);
- cout << "Contents of setA union setB: ";
- setUnion.Output();
- cout << "\n\n";
- setIntersection.Intersection(setA, setB);
- cout << "Contents of setA intersection setB: ";
- setIntersection.Output();
- cout << "\n\north";
- }
Don't listen the principal I only was testing the list before hand. I don't want help with the whole consignment just looking for a push frontward in the right direction with that linked list.
Aug 10 '07 #1
Source: https://bytes.com/topic/c/answers/690762-reading-file-into-linked-list
Post a Comment for "Read Data From File to Linked List C++"