How do i make a c++ file from a header file thats linked list with a sort function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 11
Reputation: cpsc1892 is an unknown quantity at this point 
Solved Threads: 0
cpsc1892 cpsc1892 is offline Offline
Newbie Poster

How do i make a c++ file from a header file thats linked list with a sort function

 
0
  #1
Mar 23rd, 2009
I'm studying for one of my exam's and this is my first c++ class and my professor gave one of these questions for our study guide... and I'm having trouble with it. And since our final is open book and open notes he's not giving us the answers, and i've check on the internet and my book and theres nothing thats helping me :-(

The following code is for the header file, i have to make a cpp file and with this make a sorting function and in the cpp file and for the user to pick a random 10 numbers and to make them in order. Using the linkedListType class I have to add a selection sort function for linked lists. Then i have to write a program that tests the selection sort function.
  1. #ifndef H_LinkedListType
  2. #define H_LinkedListType
  3.  
  4. #include <iostream>
  5. #include <cassert>
  6. using namespace std;
  7.  
  8. template <class Type>
  9. struct nodeType
  10. {
  11. Type info;
  12. nodeType<Type> *link;
  13. };
  14.  
  15. template<class Type>
  16. class linkedListType
  17. {
  18. template<class Type>
  19. friend ostream& operator<<(ostream&, const linkedListType<Type>&);
  20.  
  21. public:
  22. const linkedListType<Type>& operator=
  23. (const linkedListType<Type>&);
  24. //Overload the assignment operator.
  25. void initializeList();
  26. //Initializes the list to an empty state.
  27. //Postcondition: first = NULL, last = NULL,
  28. // count = 0
  29. bool isEmptyList();
  30. //Function to determine whether the list is empty.
  31. //Postcondition: Returns true if the list is empty;
  32. // otherwise, returns false.
  33.  
  34. int length();
  35. //Function to return the number of nodes in the
  36. //list.
  37. //Postcondition: The value of count is returned.
  38. void destroyList();
  39. //Function to delete all the nodes from the list.
  40. //Postcondition: first = NULL, last = NULL,
  41. // count = 0
  42. Type front();
  43. //Function to return the first element of the list.
  44. //Precondition: The list must exist and must not be
  45. //empty.
  46. //Postcondition: If the list is empty, then the
  47. // program terminates; otherwise,
  48. // the first element of the list is
  49. // returned.
  50. Type back();
  51. //Function to return the last element of the
  52. //list.
  53. //Precondition: The list must exist and must not
  54. //be empty.
  55. //Postcondition: If the list is empty, then the
  56. // program terminates; otherwise,
  57. // the last element of the list is
  58. // returned.
  59.  
  60. bool search(const Type& searchItem);
  61. //Function to determine whether searchItem is in
  62. //the list.
  63. //Postcondition: Returns true if searchItem is found
  64. // in the list; otherwise, it returns
  65. // false.
  66.  
  67. void insertFirst(const Type& newItem);
  68. //Function to insert newItem in the list.
  69. //Postcondition: first points to the new list
  70. // and newItem is inserted at the
  71. // beginning of the list.
  72.  
  73. void insertLast(const Type& newItem);
  74. //Function to return newItem at the end of the
  75. //list.
  76. //Postcondition: first points to the new list,
  77. // newItem is inserted at the end
  78. // of the list, and last points to
  79. // the last node in the list.
  80.  
  81. void deleteNode(const Type& deleteItem);
  82. //Function to delete deleteItem from the list.
  83. //Postcondition: If found, the node containing
  84. // deleteItem is deleted from the
  85. // list, first points to the first
  86. // node, and last points to the last
  87. // node of the updated list.
  88.  
  89. void deleteSmallest();
  90.  
  91. linkedListType();
  92. //default constructor
  93. //Initializes the list to an empty state.
  94. //Postcondition: first = NULL, last = NULL,
  95. // count = 0
  96.  
  97. linkedListType(const linkedListType<Type>& otherList);
  98. //copy constructor
  99.  
  100. ~linkedListType();
  101. //destructor
  102. //Deletes all the nodes from the list.
  103. //Postcondition: The list object is destroyed.
  104.  
  105. protected:
  106. int count; //variable to store the number of
  107. //elements in the list
  108. nodeType<Type> *first; //pointer to the first node of
  109. //the list
  110. nodeType<Type> *last; //pointer to the last node of
  111. //the list
  112. private:
  113. void copyList(const linkedListType<Type>& otherList);
  114. //Function to make a copy of otherList.
  115. //Postcondition: A copy of otherList is created
  116. // and assigned to this list.
  117. };
  118.  
  119. template<class Type>
  120. void linkedListType<Type>::deleteSmallest()
  121. {
  122. Type smallest;
  123. //find the smallest value
  124. deleteNode(smallest);
  125. }
  126.  
  127. template<class Type>
  128. bool linkedListType<Type>::isEmptyList()
  129. {
  130. return(first == NULL);
  131. }
  132.  
  133. template<class Type>
  134. linkedListType<Type>::linkedListType() // default constructor
  135. {
  136. first = NULL;
  137. last = NULL;
  138. count = 0;
  139. }
  140.  
  141. template<class Type>
  142. void linkedListType<Type>::destroyList()
  143. {
  144. nodeType<Type> *temp; //pointer to deallocate the memory
  145. //occupied by the node
  146. while(first != NULL) //while there are nodes in the list
  147. {
  148. temp = first; //set temp to the current node
  149. first = first->link; //advance first to the next node
  150. delete temp; //deallocate memory occupied by temp
  151. }
  152.  
  153. last = NULL; //initialize last to NULL; first has already
  154. //been set to NULL by the while loop
  155. count = 0;
  156. }
  157.  
  158.  
  159. template<class Type>
  160. void linkedListType<Type>::initializeList()
  161. {
  162. destroyList(); //if the list has any nodes, delete them
  163. }
  164.  
  165. template<class Type>
  166. int linkedListType<Type>::length()
  167. {
  168. return count;
  169. } // end length
  170.  
  171. template<class Type>
  172. Type linkedListType<Type>::front()
  173. {
  174. assert(first != NULL);
  175. return first->info; //return the info of the first node
  176. }//end front
  177.  
  178.  
  179. template<class Type>
  180. Type linkedListType<Type>::back()
  181. {
  182. assert(last != NULL);
  183. return last->info; //return the info of the first node
  184. }//end back
  185.  
  186. template<class Type>
  187. bool linkedListType<Type>::search(const Type& searchItem)
  188. {
  189. nodeType<Type> *current; //pointer to traverse the list
  190. bool found;
  191.  
  192. current = first; //set current to point to the
  193. //first node in the list
  194. found = false; //set found to false
  195.  
  196. while(current != NULL && !found) //search the list
  197. if(current->info == searchItem) //the item is found
  198. found = true;
  199. else
  200. current = current->link; //make current point
  201. //to the next node
  202.  
  203. return found;
  204. }//end search
  205.  
  206. template<class Type>
  207. void linkedListType<Type>::insertFirst(const Type& newItem)
  208. {
  209. nodeType<Type> *newNode; //pointer to create the new node
  210.  
  211. newNode = new nodeType<Type>; //create the new node
  212.  
  213. assert(newNode != NULL); //If unable to allocate memory,
  214. //terminate the program
  215.  
  216. newNode->info = newItem; //store the new item in the node
  217. newNode->link = first; //insert newNode before first
  218. first = newNode; //make first point to the
  219. //actual first node
  220. count++; //increment count
  221.  
  222. if(last == NULL) //if the list was empty, newNode is also
  223. //the last node in the list
  224. last = newNode;
  225. }
  226.  
  227. template<class Type>
  228. void linkedListType<Type>::insertLast(const Type& newItem)
  229. {
  230. nodeType<Type> *newNode; //pointer to create the new node
  231.  
  232. newNode = new nodeType<Type>; //create the new node
  233.  
  234. assert(newNode != NULL); //If unable to allocate memory,
  235. //terminate the program
  236.  
  237. newNode->info = newItem; //store the new item in the node
  238. newNode->link = NULL; //set the link field of newNode
  239. //to NULL
  240.  
  241. if(first == NULL) //if the list is empty, newNode is
  242. //both the first and last node
  243. {
  244. first = newNode;
  245. last = newNode;
  246. count++; //increment count
  247. }
  248. else //the list is not empty, insert newNode after last
  249. {
  250. last->link = newNode; //insert newNode after last
  251. last = newNode; //make last point to the actual last node
  252. count++; //increment count
  253. }
  254. }//end insertLast
  255.  
  256. template<class Type>
  257. void linkedListType<Type>::deleteNode(const Type& deleteItem)
  258. {
  259. nodeType<Type> *current; //pointer to traverse the list
  260. nodeType<Type> *trailCurrent; //pointer just before current
  261. bool found;
  262.  
  263. if(first == NULL) //Case 1; list is empty.
  264. cerr<<"Can not delete from an empty list.\n";
  265. else
  266. {
  267. if(first->info == deleteItem) //Case 2
  268. {
  269. current = first;
  270. first = first->link;
  271. count--;
  272. if(first == NULL) //list has only one node
  273. last = NULL;
  274. delete current;
  275. }
  276. else //search the list for the node with the given info
  277. {
  278. found = false;
  279. trailCurrent = first; //set trailCurrent to point to
  280. //the first node
  281. current = first->link; //set current to point to the
  282. //second node
  283.  
  284. while(current != NULL && !found)
  285. {
  286. if(current->info != deleteItem)
  287. {
  288. trailCurrent = current;
  289. current = current->link;
  290. }
  291. else
  292. found = true;
  293. } // end while
  294.  
  295. if(found) //Case 3; if found, delete the node
  296. {
  297. trailCurrent->link = current->link;
  298. count--;
  299.  
  300. if(last == current) //node to be deleted was
  301. //the last node
  302. last = trailCurrent; //update the value of last
  303.  
  304. delete current; //delete the node from the list
  305. }
  306. else
  307. cout<<"Item to be deleted is not in the list."<<endl;
  308. } //end else
  309. } //end else
  310. } //end deleteNode
  311.  
  312.  
  313. //Overloading the stream insertion operator
  314. template<class Type>
  315. ostream& operator<<(ostream& osObject, const linkedListType<Type>& list)
  316. {
  317. nodeType<Type> *current; //pointer to traverse the list
  318.  
  319. current = list.first; //set current so that it points to
  320. //the first node
  321. while(current != NULL) //while more data to print
  322. {
  323. osObject<<current->info<<" ";
  324. current = current->link;
  325. }
  326.  
  327. return osObject;
  328. }
  329.  
  330. template<class Type>
  331. linkedListType<Type>::~linkedListType() // destructor
  332. {
  333. destroyList();
  334. }//end destructor
  335.  
  336.  
  337. template<class Type>
  338. void linkedListType<Type>::copyList
  339. (const linkedListType<Type>& otherList)
  340. {
  341. nodeType<Type> *newNode; //pointer to create a node
  342. nodeType<Type> *current; //pointer to traverse the list
  343.  
  344. if(first != NULL) //if the list is nonempty, make it empty
  345. destroyList();
  346.  
  347. if(otherList.first == NULL) //otherList is empty
  348. {
  349. first = NULL;
  350. last = NULL;
  351. count = 0;
  352. }
  353. else
  354. {
  355. current = otherList.first; //current points to the
  356. //list to be copied
  357. count = otherList.count;
  358.  
  359. //copy the first node
  360. first = new nodeType<Type>; //create the node
  361.  
  362. assert(first != NULL);
  363.  
  364. first->info = current->info; //copy the info
  365. first->link = NULL; //set the link field of
  366. //the node to NULL
  367. last = first; //make last point to the
  368. //first node
  369. current = current->link; //make current point to
  370. //the next node
  371.  
  372. //copy the remaining list
  373. while(current != NULL)
  374. {
  375. newNode = new nodeType<Type>; //create a node
  376.  
  377. assert(newNode!= NULL);
  378.  
  379. newNode->info = current->info; //copy the info
  380. newNode->link = NULL; //set the link of
  381. //newNode to NULL
  382. last->link = newNode; //attach newNode after last
  383. last = newNode; //make last point to
  384. //the actual last node
  385. current = current->link; //make current point to
  386. //the next node
  387. }//end while
  388. }//end else
  389. }//end copyList
  390.  
  391.  
  392. //copy constructor
  393. template<class Type>
  394. linkedListType<Type>::linkedListType
  395. (const linkedListType<Type>& otherList)
  396. {
  397. first = NULL;
  398.  
  399. copyList(otherList);
  400.  
  401. }//end copy constructor
  402.  
  403. //overload the assignment operator
  404. template<class Type>
  405. const linkedListType<Type>& linkedListType<Type>::operator=
  406. (const linkedListType<Type>& otherList)
  407. {
  408. if(this != &otherList) //avoid self-copy
  409. copyList(otherList);
  410.  
  411. return *this;
  412. }
  413.  
  414. #endif

Basiaclly my question is... am I doing this correctly and can someone guide me into the right direction... also, i need to figure out how the user can actually put in the 10 numbers... what i have so far is
  1. using namespace std;
  2.  
  3. #include "linkedList.h"
  4.  
  5.  
  6. int main()
  7. {
  8. linkedListType<int> list;
  9. int num;
  10.  
  11. cout<<"Pleas enter 10 numbers ==> \n"
  12. <<endl;
  13. cin>>num;
  14.  
  15. while(num >= 0)
  16. {
  17. list.insert(num);
  18. cin>>num;
  19. }
  20.  
  21. cout<<"The list before sorting:"<<endl;
  22. list.print();
  23. cout<<endl;
  24. list.selectionSort();
  25. cout<<"The list after sorting:"<<endl;
  26. list.print();
  27. cout<<endl;
  28.  
  29. return 0;
  30. }

This would mean so much for anyone who could help me!!!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How do i make a c++ file from a header file thats linked list with a sort function

 
0
  #2
Mar 23rd, 2009
you need to create a loop and ask the the 10 numbers one at a time
  1. for(int i = 0; i < 10; i++)
  2. {
  3. cout << "Enter number #" << i+1 << "\n";
  4. cin >> num;
  5. list.push_back(num);
  6. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 11
Reputation: cpsc1892 is an unknown quantity at this point 
Solved Threads: 0
cpsc1892 cpsc1892 is offline Offline
Newbie Poster

Re: How do i make a c++ file from a header file thats linked list with a sort function

 
0
  #3
Mar 23rd, 2009
thanks Ancient Dragon, that helps but would you know if i wanted to find these sorted numbers would i have to make the sort function or is that already included in the header file, and if so, how am i able to attach those numbers so they are able to work along with the header file and when i try to compile my prgram it keeps giving me an error saying it cannot open linkedList.h, but what am i doing wrong or is this something that has to do with my settings?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How do i make a c++ file from a header file thats linked list with a sort function

 
0
  #4
Mar 23rd, 2009
Probably due to settings. What compiler and os are you using? Make sure the file "LinkedList.h" actually resides on your hard drive in the same directory as the *.cpp file you are trying to compile, and check the file's spelling. It doesn't have to be in the same directory, but makes life a lot easier if it is. As you gain more experience you will learn not to put header files in other directories.
Last edited by Ancient Dragon; Mar 23rd, 2009 at 10:24 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 11
Reputation: cpsc1892 is an unknown quantity at this point 
Solved Threads: 0
cpsc1892 cpsc1892 is offline Offline
Newbie Poster

Re: How do i make a c++ file from a header file thats linked list with a sort function

 
0
  #5
Mar 23rd, 2009
i'm using windows... microsoft visual studio 2008
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 109
Reputation: SeeTheLite is an unknown quantity at this point 
Solved Threads: 12
SeeTheLite SeeTheLite is offline Offline
Junior Poster

Re: How do i make a c++ file from a header file thats linked list with a sort function

 
0
  #6
Mar 23rd, 2009
Well you can make a sort function, alternatively you can sort your inputs prior to pushing them into an array with a sort() function included in <algorithm.h>, but I doubt this is what your professor wants you to do. I suggest using a recursive function for sorting.

oh and although I'm not quite familiar with visual studio 2008, mabe you should have #include "LinkedListType.h" depending on how you saved your header file.
Last edited by SeeTheLite; Mar 23rd, 2009 at 10:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 11
Reputation: cpsc1892 is an unknown quantity at this point 
Solved Threads: 0
cpsc1892 cpsc1892 is offline Offline
Newbie Poster

Re: How do i make a c++ file from a header file thats linked list with a sort function

 
0
  #7
Mar 24th, 2009
I resolved the header file issue... i played around with the settings and saved them both in the same file...
He said that he wants it in linked list not array but i'm still having problem with the sort function... i found an example online as follows:
  1. template <typename Iterator>
  2. void selection_sort(Iterator begin, Iterator end)
  3. {
  4. Iterator min;
  5. while (begin != end)
  6. {
  7. min = std::min_element(begin, end);
  8. std::iter_swap(begin, min);
  9. ++begin;
  10. }
  11. }

But i cant interpret that for what i'm working on... am i at least looking up the right information???
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 11
Reputation: cpsc1892 is an unknown quantity at this point 
Solved Threads: 0
cpsc1892 cpsc1892 is offline Offline
Newbie Poster

Re: How do i make a c++ file from a header file thats linked list with a sort function

 
0
  #8
Mar 24th, 2009
Can someone help me how to be able to add the numbers into some kind of memory spot where i can swap the values... i also found this online and i think this can help me but i dont know how to use it... help anyone??
  1. Type temp = current->info; // save value
  2. current->info = minnode->info;
  3. minnode->info = temp ;
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 11
Reputation: cpsc1892 is an unknown quantity at this point 
Solved Threads: 0
cpsc1892 cpsc1892 is offline Offline
Newbie Poster

Re: How do i make a c++ file from a header file thats linked list with a sort function

 
0
  #9
Mar 24th, 2009
also heres another question (i hope i dont sound stupid) But if I have to write up the numbers to be put into by the user, then also the coding for the selection sort, and also to swap the values to change the order... Isn't that the gist of the entire program?? what is the point of the header file?? How is helping me for the program as a whole??
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 10
Reputation: lilkid is an unknown quantity at this point 
Solved Threads: 0
lilkid lilkid is offline Offline
Newbie Poster

Re: How do i make a c++ file from a header file thats linked list with a sort function

 
0
  #10
Mar 24th, 2009
im interpreting the question as you have

write a selection sort function which goes in the header.
write a cpp file that gets in ten numbers and calls functions to store the numbers into a linked list and sort it.
compile them together.

if im right then the header file is usefull as it contains functions for the linked list for use

all the c++ file should do is get the ten numbers most of the work is done by the header function calls
Last edited by lilkid; Mar 24th, 2009 at 4:17 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC