help with this code of link list

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

Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

help with this code of link list

 
0
  #1
Apr 2nd, 2006
Write a menu driven program that manipulates a list of students and has the following options:
1- Build list from file
2- Build list from keyboard
3- Print list
Use the standard list ADT to build the student list. Student information are (name, level, ID) information


please help with this quistion i try to write the code there is 0 error 0 warnings
but there is aproblem with two function :
1-buildList()
2-printList()

the code:

  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4.  
  5. // Node Declaration
  6. template <class TYPE>
  7. struct NODE
  8. {
  9. TYPE data;
  10. NODE *link;
  11. }; // End of Node Declaration
  12.  
  13. // List Class Declaration
  14.  
  15. template <class TYPE, class KTYPE>
  16. class List
  17. {
  18. private:
  19. NODE<TYPE> *head;
  20. NODE<TYPE> *pos;
  21. NODE<TYPE> *rear;
  22. int count;
  23.  
  24. // Function Declarations
  25. bool _insert (NODE<TYPE> *pPre,
  26. TYPE dataIn);
  27. void _delete (NODE<TYPE> *pPre,
  28. NODE<TYPE> *pLoc,
  29. TYPE *dataOutPtr);
  30. bool _search (NODE<TYPE> **pPre,
  31. NODE<TYPE> **pLoc,
  32. KTYPE key);
  33.  
  34. public:
  35. List (void);
  36. ~List (void);
  37. int addNode (TYPE dataIn);
  38. bool removeNode (KTYPE key,
  39. TYPE *dataOutPtr);
  40. bool retrieveNode (KTYPE Argu,
  41. TYPE& dataOut);
  42. bool getNext (int fromWhere,
  43. TYPE& dataOut);
  44. int listCount (void);
  45. bool emptyList (void);
  46. bool fullList (void);
  47.  
  48. }; // class List
  49.  
  50. // End of List Class Declaration
  51.  
  52. /* =============== List Constructor ==============
  53. Initialize the list.
  54. Pre Class is being instantiated
  55. Post Class instantiated and initialized
  56. */
  57. template <class TYPE, class KTYPE>
  58. List<TYPE, KTYPE> :: List (void)
  59. {
  60. // Statements
  61. head = NULL;
  62. pos = NULL;
  63. rear = NULL;
  64. count = 0;
  65. } // List Constructor
  66.  
  67. /* ==================== addNode ===================
  68. Inserts data into linked list.
  69. Pre dataIn contains data to be inserted
  70. Post Data inserted or error
  71. Return -1 if overflow,
  72. 0 if successful,
  73. 1 if duplicate key
  74. */
  75. template <class TYPE, class KTYPE>
  76. int List<TYPE, KTYPE> :: addNode (TYPE dataIn)
  77. {
  78. // Local Definitions
  79. bool found;
  80. bool success;
  81.  
  82. NODE<TYPE> *pPre;
  83. NODE<TYPE> *pLoc;
  84.  
  85. // Statements
  86. found = _search (&pPre, &pLoc, dataIn.key);
  87. if (found)
  88. // Duplicate keys not allowed
  89. return (+1);
  90.  
  91. success = _insert (pPre, dataIn);
  92. if (!success)
  93. // Overflow
  94. return (-1);
  95. return (0);
  96. } // addNode
  97.  
  98. /* ===================== _insert ====================
  99. Inserts data into a new node in the linked list.
  100. Pre Insertion location identified by pPre
  101. dataIn contains data to be inserted
  102. Post data inserted in linked list or overflow
  103. Return true if successful, false if overflow
  104. */
  105. template <class TYPE, class KTYPE>
  106. bool List<TYPE, KTYPE> :: _insert (NODE<TYPE> *pPre,
  107. TYPE dataIn)
  108. {
  109. // Local Definitions
  110. NODE <TYPE> *pNew;
  111.  
  112. // Statements
  113. if (! (pNew = new NODE<TYPE>))
  114. return false;
  115.  
  116. pNew->data = dataIn;
  117. pNew->link = NULL;
  118.  
  119. if (pPre == NULL)
  120. {
  121. // Adding before first node or to empty list.
  122. pNew->link = head;
  123. head = pNew;
  124. } // if pPre
  125. else
  126. {
  127. // Adding in middle or at end
  128. pNew->link = pPre->link;
  129. pPre->link = pNew;
  130. } // if else
  131.  
  132. // Now check for add at end of list
  133. if (pNew->link == NULL)
  134. // Adding to empty list. Set rear
  135. rear = pNew;
  136.  
  137. count++;
  138.  
  139. return true;
  140. } // _insert
  141.  
  142. /* ================== removeNode ==================
  143. Removes data from linked list.
  144. Pre dltkey is identifier of node to be deleted
  145. pDataOut is pointer to data variable to
  146. receive a copy of the deleted data
  147. Post data copied to output variable and node
  148. deleted or not found
  149. Return false if not found
  150. true if deleted
  151. */
  152. template <class TYPE, class KTYPE>
  153. bool List<TYPE, KTYPE> ::
  154. removeNode (KTYPE dltkey, TYPE *pDataOut)
  155. {
  156. // Local Definitions
  157. bool found;
  158. NODE<TYPE> *pPre;
  159. NODE<TYPE> *pLoc;
  160.  
  161. // Statements
  162. found = _search (&pPre, &pLoc, dltkey);
  163. if (found)
  164. _delete (pPre, pLoc, pDataOut);
  165. return found;
  166. } // removeNode
  167.  
  168. /* =================== _delete ==================
  169. Deletes data from a linked list and returns
  170. data to calling module.
  171. Pre pPre is a pointer to predecessor node
  172. pLoc is a pointer to target node
  173. pDataOut is pointer to output data area
  174. Post Data have been deleted and returned
  175. Data memory has been recycled
  176. */
  177. template <class TYPE, class KTYPE>
  178. void List<TYPE, KTYPE> :: _delete (NODE<TYPE> *pPre,
  179. NODE<TYPE> *pLoc,
  180. TYPE *pDataOut)
  181. {
  182. // Statements
  183. *pDataOut = pLoc->data;
  184. if (pPre == NULL)
  185. // Deleting first node
  186. head = pLoc->link;
  187. else
  188. // Deleting any other node
  189. pPre->link = pLoc->link;
  190.  
  191. // Test for deleting last node
  192. if (pLoc->link == NULL)
  193. rear = pPre;
  194.  
  195. count--;
  196. delete pLoc;
  197.  
  198. return;
  199. } // _delete
  200.  
  201. /* =================== retrieveNode ==================
  202. Interface to search function.
  203. Pre key is the search argument
  204. dataOut is variable to receive data
  205. Post dataOut contains located data if found
  206. if not found, contents are unchanged
  207. Return true if successful, false if not found
  208. */
  209.  
  210. template <class TYPE, class KTYPE>
  211. bool List<TYPE, KTYPE>
  212. :: retrieveNode (KTYPE key, TYPE& dataOut)
  213. {
  214. // Local Definitions
  215. bool found;
  216. NODE <TYPE> *pPre;
  217. NODE <TYPE> *pLoc;
  218.  
  219. // Statements
  220. found = _search (&pPre, &pLoc, key);
  221. if (found)
  222. dataOut = pLoc->data;
  223. return found;
  224. } // retrieveNode
  225.  
  226. /* ==================== _search ===================
  227. Searches list and passes back address of node
  228. containing target and its logical predecessor.
  229. Pre pPre is pointer variable for predecessor
  230. pLoc is pointer variable for found node
  231. key is search argument
  232. Post pLoc points to first node equal/greater key
  233. -or- null if target > key of last node
  234. pPre points to largest node smaller than key
  235. -or- null if target < key of first node
  236. Return true if successful, false if not found
  237. */
  238. template <class TYPE, class KTYPE>
  239. bool List<TYPE, KTYPE> :: _search (NODE<TYPE> **pPre,
  240. NODE<TYPE> **pLoc,
  241. KTYPE key)
  242. {
  243. // Statements
  244. *pPre = NULL;
  245. *pLoc = head;
  246. if (count == 0)
  247. return false;
  248.  
  249. // Test for argument > last node in list
  250. if (key > rear->data.key)
  251. {
  252. *pPre = rear;
  253. *pLoc = NULL;
  254. return false;
  255. } // if
  256.  
  257. while (key > (*pLoc)->data.key)
  258. {
  259. // Have not found search argument location
  260. *pPre = *pLoc;
  261. *pLoc = (*pLoc)->link;
  262. } // while
  263.  
  264. if (key == (*pLoc)->data.key)
  265. // argument found--success
  266. return true;
  267. else
  268. return false;
  269. } // _search
  270.  
  271. /* =============== emptyList ==============
  272. Returns Boolean indicating whether the
  273. list is empty.
  274. Pre Nothing
  275. Return true if empty, false if list has data
  276. */
  277. template<class TYPE, class KTYPE>
  278. bool List<TYPE, KTYPE> :: emptyList (void)
  279. {
  280. // Statements
  281. return (count == 0);
  282. } // emptyList
  283.  
  284. /* =================== fullList ==================
  285. Returns Boolean indicating whether the list is full
  286. or has room for more data.
  287. Pre Nothing
  288. Return true if full, false if room for another node
  289. */
  290. template <class TYPE, class KTYPE>
  291. bool List<TYPE, KTYPE> :: fullList (void)
  292. {
  293. // Local Definitions
  294. NODE<TYPE> *temp;
  295.  
  296. // Statements
  297. if (temp = new NODE<TYPE>)
  298. {
  299. delete temp;
  300. return false;
  301. } // if
  302.  
  303. // Dynamic memory full
  304. return true;
  305. } // fullList
  306.  
  307. /* ==================== listCount ====================
  308. Returns integer representing number of nodes in list.
  309. Pre Nothing
  310. Return count for number of nodes in list
  311. */
  312. template <class TYPE, class KTYPE>
  313. int List<TYPE, KTYPE> :: listCount(void)
  314. {
  315. // Statements
  316. return count;
  317. } // listCount
  318.  
  319. /* ====================== getNext =====================
  320. getNext traverses a linked list. Each call either starts
  321. at the beginning of the list or returns the location of
  322. the element in the list that was last returned.
  323. Pre fromWhere is 0 to start at the first element
  324. dataOut is reference to data variable
  325. Post if another element, address placed in output area
  326. Return true if another element located,
  327. false if end of list
  328. */
  329. template <class TYPE, class KTYPE>
  330. bool List<TYPE, KTYPE> :: getNext (int fromWhere,
  331. TYPE& dataOut)
  332. {
  333. // Local Definitions
  334. bool success;
  335.  
  336. // Statements
  337. if (fromWhere == 0)
  338. {
  339. // Start from first node
  340. if (count == 0)
  341. success = false;
  342. else
  343. {
  344. pos = head;
  345. dataOut = pos->data;
  346. success = true;
  347. } // if else
  348. } // if fromwhere is zero
  349. else
  350. {
  351. // Continue from current position
  352. if (pos->link == NULL)
  353. success = false;
  354. else
  355. {
  356. pos = pos->link;
  357. dataOut = pos->data;
  358. success = true;
  359. } // if else
  360. } // if fromWhere else
  361.  
  362. return success;
  363. } // getNext
  364.  
  365. /* =============== Destructor ==============
  366. Deletes all data in list and recycles memory
  367. Pre List is being deleted
  368. Post Data and class structure have been deleted
  369. */
  370. template<class TYPE, class KTYPE>
  371. List<TYPE, KTYPE > :: ~List (void)
  372. {
  373. // Local Definitions
  374. NODE<TYPE> *deletePtr;
  375.  
  376. // Statements
  377. if (head)
  378. {
  379. while (count > 0)
  380. {
  381. deletePtr = head;
  382. head = head->link;
  383. count--;
  384. delete deletePtr;
  385. } // while
  386. } // if
  387. } // Destructor
  388. const int STR_MAX=100;
  389. struct STUDENT
  390. {
  391. int key;
  392. char name[STR_MAX];
  393. char level[STR_MAX];
  394. };
  395. /* =============== instr ============== */
  396. void instr(List<STUDENT,int>&list)
  397. {
  398. STUDENT std;
  399. char ch;
  400. cout<<"DO you want to enter a new student(y/n)\n";
  401. cin>>ch;
  402. while (ch=='y')
  403. {
  404. cout<<"Enter the ID :"<<" ";
  405. cin>>std.key;
  406. cout<<"Enter the name:"<<" ";
  407. cin>>std.name;
  408. cout<<"Enter the level:"<<" ";
  409. cin>>std.level;
  410. cout<<"DO you want to enter another student(y/n)\n";
  411. cin>>ch;
  412. }
  413. return;
  414. }
  415.  
  416. /* =============== buildList ============== */
  417.  
  418. void buildList (List<STUDENT,int>&list)
  419. {
  420. //local definitions
  421. ifstream Data;
  422. int addResult;
  423. STUDENT std;
  424. //statement
  425. Data.open("student.dat");
  426. if(!Data)
  427. {
  428. cerr<<"Error opening input file\n";
  429. exit(1);
  430. }//end if
  431. while(Data>>std.key)
  432. {
  433. //insert into list
  434. addResult=list.addNode (std);
  435. if(addResult!= 0)
  436. {
  437. if(addResult== -1)
  438. {
  439. cout<<"memory over flow \n";
  440. exit(-1);
  441. }
  442. else
  443. cout<<"ID:"
  444. <<std.key
  445. <<"not added\n";
  446. }}
  447. //end while
  448. cout<<endl;
  449. return;
  450. }//buildList
  451.  
  452.  
  453. /* =============== printList ==============
  454. PRINTS THE LIST
  455. Pre list has been created
  456. Post list printed
  457. */
  458. void printList (List<STUDENT,int>&list)
  459. {
  460. STUDENT std;
  461. if(list.listCount()==0)
  462.  
  463. cout<<"THE LIST IS EMPTY\n";
  464. else
  465. {
  466. list.getNext(0,std);
  467. do
  468. {
  469. cout<<std.key<<" "
  470. <<std.name<<" "
  471. <<std.level<<endl;
  472. }while (list.getNext(1,std));
  473. cout<<"end of the list"<<endl;
  474. }//else
  475. return;
  476. }
  477.  
  478.  
  479.  
  480. /* =============== getChoice ==============
  481. PRINTS THE MENU OF CHOICES
  482. Pre nothing
  483. Post menu printed and choice returned
  484. */
  485. int getChoice()
  486. {
  487. int choice;
  488. bool valid;
  489.  
  490. cout<<"***************MENU**********************"<<endl;
  491. cout<<" 1- Build list from file\t"
  492. <<"2- Build list from keyboard\t"
  493. <<"3- Print list\n"
  494. <<"4- exit"<<endl;
  495.  
  496. cout<<"ENTER YOUR CHOICE"<<endl;
  497. do{
  498.  
  499. cin>>choice;
  500. switch(choice)
  501. {
  502.  
  503. case 1:
  504. case 2:
  505. case 3:
  506. case 4:
  507. valid=true;
  508. break;
  509. default:
  510. valid=false;
  511. cout<<"INVALID CHOICE TRY AGAIN"<<endl;
  512. break;
  513. }//switch
  514. }while(!valid);
  515. return choice;
  516. }//getChoice
  517.  
  518. /* =============== process ==============
  519. PRINTS THE MENU OF CHOICES
  520. Pre list has been created
  521. Post all of user's choices executed
  522. */
  523. void process (List<STUDENT,int>&list)
  524. {
  525.  
  526. int choice;
  527. do
  528. {
  529. choice=getChoice();
  530. switch(choice)
  531. {
  532. case 1:
  533. buildList(list);
  534. break;
  535. case 2:
  536. instr(list);
  537. break;
  538. case 3:
  539. printList(list);
  540. break;
  541. case 4:
  542. break;
  543. }//switch
  544. }while(choice!=4);
  545. return;
  546. }//process
  547.  
  548. //prototype declarations
  549. void instr (List<STUDENT,int>&list);
  550. void printList(List<STUDENT,int>&list);
  551. int getChoice (void);
  552. void buildList (List<STUDENT,int>&list);
  553. void process (List<STUDENT,int>&list);
  554.  
  555. int main()
  556. {
  557. //local definitions
  558. List<STUDENT,int>list;
  559. //statement
  560.  
  561. process(list);
  562. buildList(list);
  563. instr(list);
  564. printList(list);
  565. return 0;
  566. }//MAIN
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

Re: help with this code of link list

 
0
  #2
Apr 2nd, 2006
thanks
every 1 who think to help me I have know where was my mistake
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