943,986 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1620
  • C++ RSS
Nov 7th, 2006
0

Sorted linked list help

Expand Post »
I have a C++ program that reads from an infile, determines by a char if the line should be Deleted or Added. From there the program uses pointers to insert data into a sorted list. The issue I am having is printing the data out properly

If you notice, I have to determine if the item is valid or not so i pass it through a validation function. It prints the invalid file in the proper place, but also prints lines that arent wanted under invalid. It then prints the lines out under where they should be, without the invalid file like it should, but it repeats the lines over and over.

I have been trying to figure it out, I believe my problem is in how I use my while statements... Any ideas??

A second issue I am having (one less important than the 1st) is when I put D in the infile to delete and ./a.out, it gives me a segmentation error. I must have some logic wrong with my coding for the delete portion... hmm...

Here is the code provided by the prof (somewhat edited to add what changes i needed to make)

.h and .cxx (driver below)

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. struct ItemType
  8. {
  9. int id;
  10. float gpa;
  11. char major[2];
  12.  
  13. };
  14.  
  15. struct NodeType
  16. {
  17. ItemType info;
  18. NodeType* next;
  19. };
  20.  
  21. class SortedType
  22. {
  23. public:
  24. SortedType();
  25. // Constructor
  26. // Postcondition: Empty list is created
  27.  
  28. ~SortedType();
  29. // Destructor
  30. // Postcondition: List is destroyed
  31.  
  32. SortedType(const SortedType& otherList);
  33. // Copy-constructor
  34. // Postcondition: List is created as a duplicate of otherList
  35.  
  36. bool IsFull() const;
  37. // Determines whether list is full.
  38. // Post: Function value = (list is full)
  39.  
  40.  
  41. bool invalidItem(ItemType item) const;
  42.  
  43.  
  44. int LengthIs() const;
  45. // Determines the number of elements in list.
  46. // Post: Function value = number of elements in list.
  47.  
  48. void MakeEmpty();
  49. // Initializes list to empty state.
  50. // Post: List is empty.
  51.  
  52. void RetrieveItem(ItemType& item, bool& found);
  53. // Retrieves list element whose key matches item's key
  54. // (if present).
  55. // Pre: Key member of item is initialized.
  56. // Post: If there is an element someItem whose key matches
  57. // item's key, then found = true and item is a copy
  58. // of someItem; otherwise found = false and item is
  59. // unchanged.
  60. // List is unchanged.
  61.  
  62. void InsertItem(ItemType item);
  63. // Adds item to list.
  64. // Pre: List is not full.
  65. // item is not in list.
  66. // Post: item is in list
  67. // (added) // list order is preserved
  68.  
  69. void DeleteItem(ItemType item);
  70. // Deletes the element whose key matches item's key.
  71. // Pre: Key member of item is initialized.
  72. // One and only one element in list has a key matching
  73. // item's key.
  74. // Post: No element in list has a key matching item's key.
  75.  
  76. void ResetList();
  77. // Initializes current position to end of list for iteration
  78. // through list.
  79. // Post: Current position is at end of list.
  80.  
  81. void GetNextItem(ItemType& item);
  82. // Gets the next element in list.
  83. // Pre:
  84. // Post: If Current position is at end, currentPos points to head of list,
  85. // else item is copy of element at current position.
  86. // Advance current position.
  87.  
  88. void Print(ofstream&);
  89.  
  90. private:
  91. NodeType* listData;
  92. int length;
  93. NodeType* currentPos;
  94. };
  95.  
  96. void GetNextItem(ItemType& item);
  97. // Gets the next element in list.
  98. // Pre:
  99. // Post: If Current position is at end, currentPos points to head of list,
  100. // else item is copy of element at current position.
  101. // Advance current position.
  102.  
  103. void Print(ofstream&);
  104.  
  105. private:
  106. NodeType* listData;
  107. int length;
  108. NodeType* currentPos;
  109. };
  110.  
  111. // SortedType.cxx
  112.  
  113. SortedType::SortedType() // Class constructor
  114. {
  115. length = 0;
  116. listData = NULL;
  117. }
  118.  
  119. SortedType::~SortedType()
  120. // Post: List is empty; all items have been deallocated.
  121. {
  122. MakeEmpty();
  123. }
  124.  
  125. SortedType::SortedType(const SortedType& otherList)
  126. // Copy-constructor
  127. // Postcondition:
  128. // IF otherList.listData == NULL
  129. // listData == NULL
  130. // ELSE
  131. // listData points to a new linked list that is a copy of
  132. // the linked list pointed to by otherList.listData
  133. {
  134. NodeType* fromPtr; // Pointer into list being copied from
  135. NodeType* toPtr; // Pointer into new list being built
  136. if(otherList.listData == NULL)
  137. {
  138. listData = NULL;
  139. return;
  140. }
  141. // Copy first node
  142. fromPtr = otherList.listData;
  143. listData = new NodeType;
  144. listData->info = fromPtr->info;
  145.  
  146. // Copy remaining nodes
  147.  
  148. toPtr = listData;
  149. fromPtr = fromPtr->next;
  150. while (fromPtr != NULL)
  151. {
  152. toPtr->next = new NodeType;
  153. toPtr = toPtr->next;
  154. toPtr->info = fromPtr->info;
  155. fromPtr = fromPtr->next;
  156. }
  157. toPtr->next = NULL;
  158. }
  159.  
  160. /* bool SortedType::IsFull() const
  161. // Returns true if there is no room for another ItemType
  162. // on the free store; false otherwise.
  163. {
  164.   NodeType* location;
  165.   try
  166.   {
  167.   location = new NodeType;
  168.   delete location;
  169.   return false;
  170.   }
  171.   catch(bad_alloc exception)
  172.   {
  173.   return true;
  174.   }
  175. } */
  176.  
  177. bool SortedType::invalidItem(ItemType item) const
  178. //**********************************************************************
  179. //Purpose: Determines whether the data assigned is valid given the parameters
  180. //Input: item.id, gpa, major
  181. //Pre: Variables must be set and have valid numbers
  182. //output: bool, true or false
  183. //post: so long as function runs, the data will be processed and determined
  184. // as invalid or valid data
  185. //note: None
  186. //**********************************************************************
  187. {
  188. return(item.id >= 111 && item.id <= 999 &&
  189. item.gpa >= 0.0 && item.gpa <= 4.0 &&
  190. item.major[0] == 'C' || item.major[0] == 'I' &&
  191. item.major[1] == 'S');
  192. }
  193.  
  194.  
  195.  
  196. bool SortedType::IsFull() const
  197. // Returns true if there is no room for another ItemType
  198. // object on the free store; false otherwise.
  199. {
  200. NodeType* ptr;
  201.  
  202. ptr = new NodeType;
  203. if (ptr == NULL)
  204. return true;
  205. else
  206. {
  207. delete ptr;
  208. return false;
  209. }
  210. }
  211.  
  212.  
  213. int SortedType::LengthIs() const
  214. // Post: Number of items in the list is returned.
  215. {
  216. return length;
  217. }
  218.  
  219.  
  220. void SortedType::MakeEmpty()
  221. // Post: List is empty; all items have been deallocated.
  222. {
  223. NodeType*
  224. tempPtr;
  225.  
  226. while (listData != NULL) // traverse list, deallocating each node in turn
  227. {
  228. tempPtr = listData;
  229. listData = listData->next;
  230. delete tempPtr;
  231. }
  232. length = 0; // to agree with the fact that all nodes are deallocated
  233. }
  234.  
  235.  
  236. void SortedType::DeleteItem(ItemType item)
  237. // Pre: item's key has been initialized.
  238. // An element in the list has a key that matches item's.
  239. // Post: No element in the list has a key that matches item's.
  240. {
  241. NodeType* location = listData;
  242. NodeType* tempLocation;
  243.  
  244. // Locate node to be deleted.
  245. if (item.id == listData->info.id)
  246. {
  247. tempLocation = location;
  248. listData = listData->next; // Delete first node.
  249. }
  250. else
  251. {
  252. while (!(item.id==(location->next)->info.id))
  253. location = location->next;
  254.  
  255. // Delete node at location->next
  256. tempLocation = location->next;
  257. location->next = (location->next)->next;
  258. }
  259. delete tempLocation;
  260. length--;
  261. }
  262.  
  263.  
  264. void SortedType::ResetList()
  265. // Post: Current position has been initialized to AtEnd
  266. {
  267. currentPos = NULL;
  268. }
  269.  
  270. void SortedType::RetrieveItem(ItemType& item, bool& found)
  271. {
  272. bool moreToSearch;
  273. NodeType* location;
  274. location = listData;
  275. found = false;
  276. moreToSearch = (location != NULL);
  277. while (moreToSearch && !found)
  278. {
  279. if (location->info.id < item.id)
  280. {
  281. location = location->next;
  282. moreToSearch = (location != NULL);
  283. }
  284. else if (item.id == location->info.id)
  285. {
  286. found = true;
  287. item = location->info;
  288. }
  289. else
  290. moreToSearch = false;
  291. }
  292. }
  293.  
  294.  
  295. void SortedType::InsertItem(ItemType item)
  296. {
  297. NodeType* newNode; // pointer to node being inserted
  298. NodeType* predLoc; // trailing pointer
  299. NodeType* location; // traveling pointer
  300. bool moreToSearch;
  301.  
  302. location = listData;
  303. predLoc = NULL;
  304. moreToSearch = (location != NULL);
  305.  
  306. // Find insertion point.
  307. while (moreToSearch)
  308. {
  309. if (location->info.id < item.id)
  310. {
  311. predLoc = location;
  312. location = location->next;
  313. moreToSearch = (location != NULL);
  314. }
  315. else
  316. moreToSearch = false;
  317. // Prepare node for insertion
  318. newNode = new NodeType;
  319. newNode->info = item;
  320.  
  321. // Insert node into list.
  322. if (predLoc == NULL) // Insert as first
  323. {
  324. newNode->next = listData;
  325. listData = newNode;
  326. }
  327. else
  328. {
  329. newNode->next = location;
  330. predLoc->next = newNode;
  331. }
  332. length++;
  333. }
  334.  
  335.  
  336.  
  337. void SortedType::GetNextItem(ItemType& item)
  338. // Pre: Current position is defined.
  339. // Element at current position is not last in list.
  340. // Post: Current position has been updated; item is current item.
  341. {
  342. if (currentPos == NULL) //Wrap at end of list
  343. currentPos = listData;
  344.  
  345. item = currentPos->info;
  346. currentPos = currentPos->next;
  347. }
  348.  
  349. void SortedType::Print(ofstream& outFile)
  350. {
  351. currentPos = listData;
  352.  
  353. while(currentPos != NULL)
  354. {
  355. outFile << currentPos->info.id << " " << currentPos->info.gpa << currentPos->info.major[0] << currentPos->info.major[1]
  356. currentPos = currentPos->next;
  357. }
  358. }

Here is the driver for the above

C++ Syntax (Toggle Plain Text)
  1. #include "SortedLinkedList.h"
  2.  
  3. int main()
  4. {
  5. ifstream inFile;
  6. ofstream outFile;
  7. char lister;
  8. int length = 0;
  9.  
  10. inFile.open("in.data");
  11. outFile.open("out.data");
  12. if(inFile.fail() || outFile.fail())
  13. {
  14. cout << "Input or output file opening failed" << endl;
  15. }
  16.  
  17. SortedType ReadList;
  18. ItemType item;
  19.  
  20. outFile << "<~~~~~~~ Grade Report ~~~~~~~>" << endl;
  21. ReadList.MakeEmpty();
  22. inFile >> lister;
  23.  
  24. while(inFile)
  25. {
  26. if(lister == 'A')
  27. {
  28. inFile >> item.id >> item.gpa >> item.major[0] >> item.major[1];
  29. ReadList.InsertItem(item);
  30. }
  31. else if(lister == 'D')
  32. {
  33. inFile >> item.id;
  34. ReadList.DeleteItem(item);
  35. }
  36. inFile >> lister;
  37. }
  38. ReadList.MakeEmpty();
  39. inFile >> lister;
  40.  
  41. while(inFile)
  42. {
  43. if(lister == 'A')
  44. {
  45. inFile >> item.id >> item.gpa >> item.major[0] >> item.major[1];
  46. ReadList.InsertItem(item);
  47. }
  48. else if(lister == 'D')
  49. {
  50. inFile >> item.id;
  51. ReadList.DeleteItem(item);
  52. }
  53. inFile >> lister;
  54. }
  55.  
  56. ReadList.ResetList();
  57. length = ReadList.LengthIs();
  58.  
  59. for(int count = 1; count <= length; count++)
  60. {
  61. ReadList.GetNextItem(item);
  62.  
  63. if(!ReadList.invalidItem(item))
  64. {
  65. //ReadList.Print(outFile);
  66. //outFile << item.id << setw(20) << item.gpa << setw(20) << item.major[0] << item.major[1];
  67. outFile << " **** invalid line" << endl << endl;
  68. ReadList.DeleteItem(item);
  69. }
  70. }
  71.  
  72. ReadList.ResetList();
  73. length = ReadList.LengthIs();
  74. outFile << "student id" << setw(12) << "GPA" << setw(22) << "Major" << endl;
  75. outFile << "----------" << setw(12) << "---" << setw(22) <<
  76. "-----" << endl;
  77. for(int count = 1; count <= length; count++)
  78. {
  79. ReadList.GetNextItem(item);
  80. ReadList.Print(outFile);
  81. //outFile << item.id << setw(20) << item.gpa << setw(20) << item.major[0] << item.major[1];
  82. //outFile << endl;
  83. }
  84. outFile << "> > > end < < <" << endl;
  85.  
  86. return 0;
  87. }

The driver has some things commented out as I was troubleshooting my problem and trying different things. So bear with me.

Thanks in advance for any advice or help on this problem!!!

Also, if I have any other large mistakes, point them out to me please so I can go take a look at them and make changes.

Here is a sample input file:
A 666 3.23 IS A 100 3.11 CS A 555 3.85 IS A 777 3.99 CS A 888 3.99 IS



Here is a sample output of what I get with the above code:
<~~~~~~~ Grade Report ~~~~~~~>
100 3.11CS
555 3.85IS
666 3.23IS
777 3.99CS
888 3.99IS
**** invalid line

student id GPA Major
---------- --- -----
555 3.85IS
666 3.23IS
777 3.99CS
888 3.99IS
555 3.85IS
666 3.23IS
777 3.99CS
888 3.99IS
555 3.85IS
666 3.23IS
777 3.99CS
888 3.99IS
555 3.85IS
666 3.23IS
777 3.99CS
888 3.99IS
> > > end < < <
Last edited by dabobrow; Nov 7th, 2006 at 6:32 pm. Reason: added more
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dabobrow is offline Offline
7 posts
since Nov 2006
Nov 7th, 2006
0

Re: Sorted linked list help

Nevermind, I believe I solved my issue. Stepped away from the desk for a bit, then came back with a fresh mind. That seems to help.

Anyone with any code changes though, still post. Maybe I have some stuff wrong but it still displays as I want it to. Thanks ahead of time to anyone who takes a look.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dabobrow is offline Offline
7 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Array of pointers to objects
Next Thread in C++ Forum Timeline: Controls on Activex Controls (Datagrid,Tab) in Win32 C (Dev C++ )...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC