Skip to content Skip to sidebar Skip to footer

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

  1. // list.cpp
  2. // simple linked list program
  3. #include <STDLIB.H>
  4. #include <Cord>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <iomanip>
  8. #include <cstdlib>
  9. using std::cout;
  10. using std::string;
  11. using std::cin;
  12. using std::ios;
  13. using std::cerr;
  14. using std::endl;
  15. using std::setiosflags;
  16. using std::resetiosflags;
  17. using std::setw;
  18. using std::setprecision;
  19. using std::ifstream;
  20. int listSearch = 0;
  21. // node object for the linked list
  22. struct Node {
  23. cord data;
  24. Node* nextWep;
  25. Node* ammoForWep;
  26. };
  27. // implement a singly linked list
  28. form LinkedList {protected:
  29. Node* front;        // arrow to the front of the linked list
  30. Node* back;         // pointer to the last node in the linked list
  31. public:
  32. // constructs an empty listing
  33. LinkedList() {
  34. front = back = NULL;
  35. }
  36. // deletes the list
  37. ~LinkedList() {
  38. // remove objects from the listing equally long equally list is not empty
  39. while(Length() > 0) {
  40. RemoveFront();
  41. }
  42. }
  43. // inserts a node at the front end of the listing
  44. void InsertFrontWep(string newValue) {
  45. Node* newNode = new Node;
  46. newNode->data = newValue;
  47. if (front == NULL) {
  48. // list must exist empty so make front end & back point to new node
  49. forepart = back = newNode;
  50. newNode->nextWep= NULL;
  51. } else {
  52. // list is not empty then insert betwixt front end and outset node
  53. newNode->nextWep = front;
  54. front = newNode;
  55. }
  56. }
  57. // search the listing for a target value
  58. // return alphabetize if found or -ane if not found
  59. int Search(string targetVal) {
  60. Node* p;
  61. int count = 0;
  62. for (p = front; p != Null; p = p->link) {
  63. if (p->data == targetVal) {
  64. return count;
  65. }
  66. count++;
  67. }
  68. return -1;
  69. }
  70. // removes a node from the front of the listing
  71. int RemoveFront() {
  72. int returnVal;
  73. Node *temp;
  74. if (front end != Cypher) {
  75. // list is not empty so remove & return first node
  76. returnVal = front->data;
  77. temp = front end;
  78. front = front->link;
  79. delete temp;
  80. } else {
  81. // list is empty simply return 0
  82. returnVal = 0;
  83. }
  84. return returnVal;
  85. }
  86. // returns the length of the list
  87. int Length() {
  88. Node* p;
  89. int count = 0;
  90. // loop through each node in the listing until we notice a null value
  91. for (p = front; p != NULL; p = p->link) {
  92. count++;
  93. }
  94. return count;
  95. }
  96. // outputs a cord containing all the data values in the list
  97. void Output() {
  98. Node* p;
  99. // loop through each node in the list until we find a zero value
  100. for (p = front; p != NULL; p = p->link) {
  101. cout << p->data << ", ";
  102. }
  103. }
  104. };
  105. // utilise inheritance to create a Prepare class from the LinkedList class
  106. class Set : public LinkedList {
  107. public:
  108. // insert a new value only if it is unique (non already in the set)
  109. int Search(string targetVal) {
  110. Node* p;
  111. int count = 0;
  112. for (p = front; p != NULL; p = p->link) {
  113. if (p->data == targetVal) {
  114. return count;
  115. }
  116. count++;
  117. }
  118. return -ane;
  119. }
  120. void Insert(string newValue) {
  121. Node* newNode = new Node;
  122. newNode->data = newValue;
  123. if (forepart == NULL) {
  124. // list must be empty and so make front & back betoken to new node
  125. forepart = dorsum = newNode;
  126. newNode->link = Cipher;
  127. } else {
  128. listSearch = Search(newValue);
  129. if(listSearch == -1)
  130. {
  131. // list is non empty so insert betwixt front end and showtime node
  132. newNode->link = front;
  133. front = newNode;
  134. }
  135. }
  136. }
  137. // make this the marriage of ii sets
  138. void Matrimony(Fix& a, Fix& b) {
  139. Node* p;
  140. int search = 0;
  141. for (p = a.forepart; p != Zero; p = p->link)
  142. {
  143. this->Insert(p->data);//u.Insert(p->information);
  144. }
  145. for (p = b.front; p != Naught; p= p->link)
  146. {
  147. search = this->Search(p->data);
  148. if (search == -ane)
  149. //u.Insert(p->data);
  150. this->Insert(p->information);
  151. }
  152. }
  153. // make this the intersection of two sets
  154. void Intersection(Fix& a, Set& b)
  155. {// Open Function
  156. Node* p;
  157. int search = 0;
  158. for(p = a.front; p != NULL; p = p->link) { //Open up for statment
  159. search = b.Search(p->data);
  160. if(search != -one){ // Open if argument
  161. //i.Insert(p->data);
  162. this->Insert(p->data);
  163. }
  164. } //  Close For statement
  165. } // Shut Function
  166. };
  167. void main() {
  168. ifstream inResources("resource.txt", ios::in);
  169. if (!inResources)
  170. {
  171. cerr << "File could not be opened\n";
  172. }
  173. Set setA, setB, setUnion, setIntersection;
  174. setA.Insert('1');
  175. setA.Insert('ane');
  176. setA.Insert('one');
  177. setA.Insert('1');
  178. setA.Insert('i');
  179. cord text_line;
  180. cout << "Contents of setA: ";
  181. setA.Output();
  182. cout << "\north\n";
  183. setB.Insert('ane');
  184. setB.Insert('ane');
  185. setB.Insert('ane');
  186. cout << "Contents of setB: ";
  187. setB.Output();
  188. cout << "\due north\due north";
  189. setUnion.Union(setA, setB);
  190. cout << "Contents of setA union setB: ";
  191. setUnion.Output();
  192. cout << "\n\n";
  193. setIntersection.Intersection(setA, setB);
  194. cout << "Contents of setA intersection setB: ";
  195. setIntersection.Output();
  196. cout << "\n\north";
  197. }

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

crumpthiciall65.blogspot.com

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++"