"doubly linked list" question

Reply

Join Date: Sep 2005
Posts: 14
Reputation: kyakobi84 is an unknown quantity at this point 
Solved Threads: 0
kyakobi84 kyakobi84 is offline Offline
Newbie Poster

"doubly linked list" question

 
0
  #1
Oct 21st, 2005
I did a "single linked list" header & Implementation & Main files & everything worked very good. But now i must Implementation for the "doubly linked list" & i just need to know what i should add to my functions so they will work good, should i change all the functions Implementation ???

i have these function,
addToHead(int)
addToTail(int)
deleteFromHead()
deleteFromTail()
deleteNode(int)
isInList(int)
printForward()
printBackward()
clear()

anyone can help, plz ??? :!:
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 14
Reputation: kyakobi84 is an unknown quantity at this point 
Solved Threads: 0
kyakobi84 kyakobi84 is offline Offline
Newbie Poster

Re: "doubly linked list" question

 
0
  #2
Oct 21st, 2005
This is what I wrote:


  1. doublyLinkedList::doublyLinkedList()
  2. {
  3. head = tail = 0;
  4. }
  5.  
  6.  
  7.  
  8. doublyLinkedList::~doublyLinkedList()
  9. {
  10. node *temp;
  11.  
  12. while (head != 0)
  13. {
  14. head->prev = 0;
  15. temp = head->next;
  16. delete head;
  17. head = temp;
  18. }
  19. }
  20.  
  21.  
  22.  
  23. void doublyLinkedList::addToHead(int item)
  24. {
  25. node *temp;
  26. temp = new node;
  27.  
  28. temp->info = item;
  29. temp->next = head;
  30. temp->prev = 0;
  31.  
  32. head = temp;
  33.  
  34. if (tail == 0)
  35. tail = head;
  36. }
  37.  
  38.  
  39.  
  40. void doublyLinkedList::addToTail(int item)
  41. {
  42. node *temp;
  43. temp = new node;
  44.  
  45. temp->info = item;
  46. temp->prev = tail;
  47. temp->next = 0;
  48.  
  49. tail = temp;
  50.  
  51. if (head == 0)
  52. head = tail;
  53. }
  54.  
  55.  
  56.  
  57. void doublyLinkedList::deleteFromHead()
  58. {
  59. if(head != 0)
  60. {
  61. int item = head->info;
  62. node *temp = head;
  63.  
  64. if(head == tail)
  65. head = tail = 0;
  66. else
  67. head = head->next;
  68.  
  69.  
  70. delete temp;
  71. cout << ">> " << item << " has been deleted from head";
  72. }
  73. else
  74. cout << "** The list is empty";
  75. }
  76.  
  77.  
  78.  
  79. void doublyLinkedList::deleteFromTail()
  80. {
  81. if(tail != 0)
  82. {
  83. int item = tail->info;
  84. node *temp = tail;
  85.  
  86. if(tail == head)
  87. tail = head = 0;
  88. else
  89. tail = tail->prev;
  90.  
  91.  
  92. delete temp;
  93. cout << ">> " << item << " has been deleted from tail";
  94. }
  95. else
  96. cout << "** The list is empty";
  97. }
  98.  
  99.  
  100.  
  101. void doublyLinkedList::deleteNode(int item)
  102. {
  103. if(head != 0)
  104. {
  105. if(item == head->info)
  106. {
  107. cout <<"\n";
  108. deleteFromHead();
  109. }
  110.  
  111. else
  112. {
  113. node *temp = head->next, *pred = head;
  114.  
  115. while(temp != 0 && !(temp->info == item))
  116. pred = pred->next, temp = temp->next;
  117.  
  118. if(temp != 0)
  119. {
  120. pred->next = temp->next;
  121.  
  122. if(temp == tail)
  123. tail = pred;
  124.  
  125. delete temp;
  126.  
  127. cout << "\n>> " << item << " has been deleted";
  128. }
  129. else
  130. cout << "\n>> " << item << " is not in the list";
  131. }
  132.  
  133. }
  134. else
  135. cout << "\n** The list is empty";
  136.  
  137. }
  138.  
  139.  
  140.  
  141. bool doublyLinkedList::isInList(int item)
  142. {
  143. node *temp;
  144. temp = head;
  145.  
  146. while (temp != 0)
  147. {
  148. if(temp->info == item)
  149. return true;
  150. temp = temp->next;
  151. }
  152.  
  153. return false;
  154.  
  155. }
  156.  
  157.  
  158.  
  159. void doublyLinkedList::printForward() const
  160. {
  161. node *temp = head;
  162.  
  163. cout << "{HEAD}---> | ";
  164. while(temp != 0)
  165. {
  166. cout << temp->info << " | ";
  167. temp = temp->next;
  168. }
  169. cout << " <---{TAIL}";
  170.  
  171. }
  172.  
  173.  
  174.  
  175. void doublyLinkedList::printBackward() const
  176. {
  177. node *temp = tail;
  178.  
  179. cout << "{HEAD}---> | ";
  180. while(temp != 0)
  181. {
  182. cout << temp->info << " | ";
  183. temp = temp->prev;
  184. }
  185. cout << " <---{TAIL}";
  186. }
  187.  
  188.  
  189.  
  190. void doublyLinkedList::clear()
  191. {
  192. while(head != 0)
  193. {
  194. cout << "\n";
  195. deleteFromHead();
  196. }
  197. cout << "\n";
  198. }
<< moderator edit: added [code][/code] tags >>
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



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

©2003 - 2009 DaniWeb® LLC