i neeed ur help .. find error in data structure program

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2007
Posts: 2
Reputation: norah87 is an unknown quantity at this point 
Solved Threads: 0
norah87 norah87 is offline Offline
Newbie Poster

i neeed ur help .. find error in data structure program

 
0
  #1
May 2nd, 2007
hello everyone,, first i wanna tell that i am a new membor and i heard alot about this usefull forum.. and today i want ur help guys...
in this question i dont know the causes of the error : the topic is doubly link list which is soted.. the type of the data is a structure.. i did it but can someone check it out for me???
The Address Book


Develop a program to represent a personal address book. The program should maintain a list of contacts, and each contact has the person’s name, address and telephone number. The list should be implemented as a Doubly Linked List.

Write a menu driven program to allow the user to select one of the following options:

1. Add new contact
2. Delete a contact by name.
3. Search by name.
4. Print all contacts (A-Z).
5. Print all contacts (Z-A)
6. Exit.

The list must be sorted according to persons’ name. The search by name should print all the contacts that have the same name as the entered by the user. The deletion process must be based on the name. Once the contact is found the user should be asked first if he\she is sure about the deletion. If there was more than one contact with same name then only the first occurred name shall be deleted.

the solution :
  1. #include <iostream>
  2. #include <cassert>
  3. #include<cstring>
  4. using namespace std;
  5. struct person
  6. {
  7. string name;
  8. string address;
  9. long phone;
  10. };
  11. template <class Type>
  12. struct nodeType
  13. {
  14. Type info;
  15. nodeType<Type> *next;
  16. nodeType<Type> *back;
  17. };
  18.  
  19.  
  20. template <class Type>
  21. class doublyLinkedList
  22. {
  23. friend ostream& operator<<(ostream&,
  24. const doublyLinkedList<Type>&);
  25. public:
  26. const doublyLinkedList<Type>& operator=
  27. (const doublyLinkedList<Type> &);
  28. void initializeList();
  29. bool isEmptyList();
  30. void destroy();
  31. void normalPrint();
  32. void reversePrint();
  33. int length();
  34. Type front();
  35. Type back();
  36. void search(const Type& searchItem);
  37. void insertNode(const Type& insertItem);
  38. void deleteNode(const Type& deleteItem);
  39. doublyLinkedList();
  40. doublyLinkedList(const doublyLinkedList<Type>& otherList);
  41. ~doublyLinkedList();
  42.  
  43. protected:
  44. int count;
  45. nodeType<Type> *first;
  46. nodeType<Type> *last;
  47. private:
  48. void copyList(const doublyLinkedList<Type>& otherList);
  49. };
  50.  
  51. template<class Type>
  52. doublyLinkedList<Type>::doublyLinkedList()
  53. {
  54. first= NULL;
  55. last = NULL;
  56. count = 0;
  57. }
  58. template<class Type>
  59. bool doublyLinkedList<Type>::isEmptyList()
  60. {
  61. return(first == NULL);
  62. }
  63. template<class Type>
  64. void doublyLinkedList<Type>::destroy()
  65. {
  66. nodeType<Type> *temp;
  67. while(first != NULL)
  68. {
  69. temp = first;
  70. first = first->next;
  71. delete temp;
  72. }
  73. last = NULL;
  74. count = 0;
  75. }
  76. template<class Type>
  77. void doublyLinkedList<Type>::initializeList()
  78. {
  79. destroy();
  80. }
  81. template<class Type>
  82. int doublyLinkedList<Type>::length()
  83. {
  84. return count;
  85. }
  86. template<class Type>
  87. ostream& operator<<(ostream& osObject,
  88. const doublyLinkedList<Type>& list)
  89. {
  90. nodeType<Type> *current;
  91. current = list.first;
  92. while(current != NULL)
  93. {
  94. cout<<current->info.name<<" "<<current->info.address<<" "<<current->ifo.phone;
  95. current = current->next;
  96. }
  97. return osObject;
  98. }
  99. template<class Type>
  100. void doublyLinkedList<Type>::normalPrint()
  101. {
  102. nodeType<Type> *current;
  103. current = first;
  104. while(current != NULL)
  105. {
  106. cout<<current->info.name<<" "<<current->info.address<<" "<<current->info.phone;
  107. current = current->next;
  108. }
  109. }
  110.  
  111. template<class Type>
  112. void doublyLinkedList<Type>::reversePrint()
  113. {
  114. nodeType<Type> *current;
  115. current = last;
  116. while(current != NULL)
  117. {
  118. cout<<current->info.name<<" "<<current->info.address<<" "<<current->info.phone;
  119. current = current->back;
  120. }
  121. }
  122. template<class Type>
  123. void doublyLinkedList<Type>::search(const Type& searchItem)
  124. {
  125. nodeType<Type> *current;
  126. current = first;
  127. while(current != NULL)
  128. if(current->info.name==deleteItem.name)
  129. cout<<current->info.name<<" "<<current->info.address<<" "<<current->info.phone<<endl;
  130. current = current->next;
  131. }
  132. template<class Type>
  133. Type doublyLinkedList<Type>::front()
  134. {
  135. assert(first != NULL);
  136. return first->info;
  137. }
  138. template<class Type>
  139. Type doublyLinkedList<Type>::back()
  140. {
  141. assert(last != NULL);
  142. return last->info;
  143. }
  144. template<class Type>
  145. void doublyLinkedList<Type>::insertNode(const Type& insertItem)
  146. {
  147. nodeType<Type> *current;
  148. nodeType<Type> *trailCurrent;
  149. nodeType<Type> *newNode;
  150. bool found;
  151. newNode = new nodeType<Type>;
  152. assert(newNode != NULL);
  153. newNode->info.name==insertItem.name;
  154. newNode->info.address==insertItem.address;
  155. newNode->info.phone == insertItem.phone;
  156. newNode->next = NULL;
  157. newNode->back = NULL;
  158. if(first == NULL)
  159. {
  160. first = newNode;
  161. last = newNode;
  162. count++;
  163. }
  164. else
  165. {
  166. found = false;
  167. current = first;
  168. while(current != NULL && current!found)
  169. if(current->info.name==deleteItem.name)
  170. found = true;
  171. else
  172. {
  173. trailCurrent = current;
  174. current = current->next;
  175. }
  176. if(current == first)
  177. {
  178. first->back = newNode;
  179. newNode->next = first;
  180. first = newNode;
  181. count++;
  182. }
  183. else
  184. {
  185. if(current != NULL)
  186. {
  187. trailCurrent->next = newNode;
  188. newNode->back = trailCurrent;
  189. newNode->next = current;
  190. current->back = newNode;
  191. }
  192. else
  193. {
  194. trailCurrent->next = newNode;
  195. newNode->back = trailCurrent;
  196. last = newNode;
  197. }
  198. count++;
  199. }
  200. }
  201. }
  202.  
  203. template<class Type>
  204. void doublyLinkedList<Type>::deleteNode(const Type& deleteItem)
  205. {
  206. nodeType<Type> *current;
  207. nodeType<Type> *trailCurrent;
  208. bool found;
  209. if(first == NULL)
  210. cerr<<"Cannot delete from an empty list"<<endl;
  211. else
  212. if(first->info.name == deleteItem.name)
  213. { int n;
  214. cout<<"If you are sure to delete press 1";
  215. cin>>n;
  216. if (n==1){
  217. current = first;
  218. first = first->next;
  219. if(first != NULL)
  220. first->back = NULL;
  221. else
  222. last = NULL;
  223. count--;
  224. delete current;
  225. }
  226. else
  227. {
  228. found = false;
  229. current = first;
  230. while(current != NULL && !found)
  231. if(current->info.name == deleteItem.name)
  232. found = true;
  233. else
  234. current = current->next;
  235. if(current == NULL)
  236. cout<<"The item to be deleted is not in the list"
  237. <<endl;
  238. else
  239. if(current->info.name==deleteItem.name)
  240. {
  241. trailCurrent = current->back;
  242. trailCurrent->next = current->next;
  243. if(current->next != NULL)
  244. current->next->back = trailCurrent;
  245. if(current == last)
  246. last = trailCurrent;
  247. count--;
  248. delete current;
  249. }
  250. else
  251. cout<<"The item to be deleted is not in list."
  252. <<endl;
  253. }
  254. }
  255. }
  256. template<class Type>
  257. void doublyLinkedList<Type>::copyList(const doublyLinkedList<Type>&
  258. otherList)
  259. {
  260. nodeType<Type> *current;
  261. current=otherList.first;
  262. if(first != NULL)
  263. destroyList();
  264. if(otherList.first == NULL)
  265. {
  266. first = NULL;
  267. last = NULL;
  268. count = 0;
  269. }
  270. else
  271. {
  272. while(current !=NULL)
  273. { insertNode(current->info);
  274. current=currnet->next;
  275. }
  276. }
  277. }
  278.  
  279. template<class Type>
  280. doublyLinkedList<Type>::doublyLinkedList(const doublyLinkedList<Type>&
  281. otherList)
  282. {
  283. first = NULL;
  284. copyList(otherList);
  285.  
  286. }
  287. template<class Type>
  288. const doublyLinkedList<Type>& doublyLinkedList<Type>::operator=
  289. (const doublyLinkedList<Type> &otherList)
  290. {
  291. if(this != &otherList)
  292. copyList(otherList);
  293. return *this;
  294. }
  295. template<class Type>
  296. doublyLinkedList<Type>::~doublyLinkedList()
  297. {
  298. destroy();
  299. }
  300. void main()
  301. {
  302. doublyLinkedList<person> L;
  303. person p1;
  304. p1.name="noora";
  305. p1.address="house no 55,block no 3232";
  306. p1.phone= 39898988;
  307. L.insertNode(p1);
  308. person p2;
  309. p2.name="mariam Ali";
  310. p2.address="house no 63,block no 126";
  311. p2.phone=39656569;
  312. L.insertNode(p2);
  313.  
  314. person p3;
  315. p3.name="maryiam Hassan";
  316. p3.address="house no 87, block no 555";
  317. p3.phone=39595959;
  318. L.insertNode(p3);
  319.  
  320. person p4;
  321.  
  322. cout<<"Enter Name"<<endl;
  323. cin>>p4.name;
  324. cout<<"Enter address"<<endl;
  325. cin>>p4.address;
  326. cout<<"Enter phone number";
  327. cin>>p4.phone;
  328. L.insertNode(p4);
  329. L.normalPrint();
  330. person p5;
  331. p5.name="mariam";
  332. person p6;
  333. p6.name="noora";
  334. person p7;
  335. p7.name="Fatima";
  336. L.deleteNode(p5);
  337. L.search(p6);
  338. L.search(p7);
  339. L.normalPrint();
  340. L.reversePrint();
  341. }
Last edited by WaltP; May 2nd, 2007 at 5:23 am. Reason: Removed COLOR tags and Please learn to use CODE tags -- read the announcements at the top of the forum
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: i neeed ur help .. find error in data structure program

 
0
  #2
May 2nd, 2007
Originally Posted by norah87 View Post
in this question i dont know the causes of the error : the topic is doubly link list which is soted.. the type of the data is a structure.. i did it but can someone check it out for me???
Neither do we, since we don't have a clue what type of error you're talking about. Seems the DaniWeb Psychic Plugin isn't working at the moment.

Please be
1) less vague
2) less code (341 lines to find an unknown error as a little much to ask)
3) more proactive -- read the announcements at the top of the forum
4) more helpful -- format your code so it can be understood
Last edited by WaltP; May 2nd, 2007 at 5:30 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 2
Reputation: norah87 is an unknown quantity at this point 
Solved Threads: 0
norah87 norah87 is offline Offline
Newbie Poster

Re: i neeed ur help .. find error in data structure program

 
0
  #3
May 2nd, 2007
thaaaaaaanx for these lovely advices .. sorry... for that...
forgive me ( i am a new member)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC