Trevor Johnson
Kevin Smith
Allan Harris
I need to read that file into program and then turn it into a linked list. So on the list I can go Trevor, Kevin, Allan in a straight row but I can also call out there last name when I am on their first name in the list. Sorry if it doesn't make sense trying to explain best I can.
So far I have
Expand|Select|Wrap|Line Numbers
- // list.cpp
- // simple linked list program
- #include <STDLIB.H>
- #include <STRING>
- #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 {
- string data;
- Node* nextWep;
- Node* ammoForWep;
- };
- // implement a singly linked list
- class LinkedList {protected:
- Node* front; // pointer to the front of the linked list
- Node* back; // pointer to the last node in the linked list
- public:
- // constructs an empty list
- LinkedList() {
- front = back = NULL;
- }
- // deletes the list
- ~LinkedList() {
- // remove objects from the list as long as list is not empty
- while(Length() > 0) {
- RemoveFront();
- }
- }
- // inserts a node at the front of the list
- void InsertFrontWep(string newValue) {
- Node* newNode = new Node;
- newNode->data = newValue;
- if (front == NULL) {
- // list must be empty so make front & back point to new node
- front = back = newNode;
- newNode->nextWep= NULL;
- } else {
- // list is not empty so insert between front and first node
- newNode->nextWep = front;
- front = newNode;
- }
- }
- // search the list for a target value
- // return index if found or -1 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 list
- int RemoveFront() {
- int returnVal;
- Node *temp;
- if (front != NULL) {
- // list is not empty so remove & return first node
- returnVal = front->data;
- temp = front;
- front = front->link;
- delete temp;
- } else {
- // list is empty just 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 list until we find a null value
- for (p = front; p != NULL; p = p->link) {
- count++;
- }
- return count;
- }
- // outputs a string containing all the data values in the list
- void Output() {
- Node* p;
- // loop through each node in the list until we find a null value
- for (p = front; p != NULL; p = p->link) {
- cout << p->data << ", ";
- }
- }
- };
- // use inheritance to create a Set class from the LinkedList class
- class Set : public LinkedList {
- public:
- // insert a new value only if it is unique (not 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 -1;
- }
- void Insert(string newValue) {
- Node* newNode = new Node;
- newNode->data = newValue;
- if (front == NULL) {
- // list must be empty so make front & back point to new node
- front = back = newNode;
- newNode->link = NULL;
- } else {
- listSearch = Search(newValue);
- if(listSearch == -1)
- {
- // list is not empty so insert between front and first node
- newNode->link = front;
- front = newNode;
- }
- }
- }
- // make this the union of two sets
- void Union(Set& a, Set& b) {
- Node* p;
- int search = 0;
- for (p = a.front; p != NULL; p = p->link)
- {
- this->Insert(p->data);//u.Insert(p->data);
- }
- for (p = b.front; p != NULL; p= p->link)
- {
- search = this->Search(p->data);
- if (search == -1)
- //u.Insert(p->data);
- this->Insert(p->data);
- }
- }
- // make this the intersection of two sets
- void Intersection(Set& a, Set& b)
- {// Open Function
- Node* p;
- int search = 0;
- for(p = a.front; p != NULL; p = p->link) { //Open for statment
- search = b.Search(p->data);
- if(search != -1){ // Open if statement
- //i.Insert(p->data);
- this->Insert(p->data);
- }
- } // Close For statement
- } // Close 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('1');
- setA.Insert('1');
- setA.Insert('1');
- setA.Insert('1');
- string text_line;
- cout << "Contents of setA: ";
- setA.Output();
- cout << "\n\n";
- setB.Insert('1');
- setB.Insert('1');
- setB.Insert('1');
- cout << "Contents of setB: ";
- setB.Output();
- cout << "\n\n";
- 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\n";
- }