help with double link list

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

Join Date: Aug 2008
Posts: 17
Reputation: bonnie1702 is on a distinguished road 
Solved Threads: 0
bonnie1702 bonnie1702 is offline Offline
Newbie Poster

help with double link list

 
0
  #1
Nov 23rd, 2008
Hi all,

I have a double link list program that compiles ok, and when I run it, I get a message box saying that windows needs to stop the program.
Here is the code:
  1. #include <iostream>
  2. #include <conio.h>
  3.  
  4. using namespace std;
  5.  
  6. class linklist
  7. {
  8. private:
  9. struct dnode
  10. {
  11. dnode *prev;
  12. int data;
  13. dnode *next;
  14. }*p;
  15. public:
  16. linklist();
  17. ~linklist();
  18. void d_addatbeg(int num);
  19. void d_append(int num);
  20. void d_addafter(int loc,int num);
  21. void d_delete(int num);
  22. };
  23.  
  24. linklist::linklist()
  25. {
  26. p = NULL;
  27. }
  28.  
  29. linklist::~linklist()
  30. {
  31. dnode *q;
  32. while (q->next != NULL)
  33. {
  34. q = p->next;
  35. delete p;
  36. p = q;
  37. }
  38. }
  39.  
  40. void linklist::d_addatbeg(int num)
  41. {
  42. dnode *q;
  43. q = new dnode;
  44. q->prev = NULL;
  45. q->data = num;
  46. q->next = p;
  47. p->prev = q;
  48. p = q;
  49. }
  50.  
  51. void linklist::d_append(int num)
  52. {
  53. dnode *q,*r;
  54. q = p;
  55. if (q == NULL)
  56. {
  57. q = new dnode;
  58. q->prev = NULL;
  59. q->data = num;
  60. q->next = NULL;
  61. p = q;
  62. }
  63. else
  64. {
  65. while (q->next != NULL)
  66. q = q->next;
  67. r = new dnode;
  68. r->data = num;
  69. r->next = NULL;
  70. r->prev = q;
  71. q->next = r;
  72. }
  73. }
  74.  
  75. void linklist::d_addafter(int loc,int num)
  76. {
  77. dnode *q;
  78. q = p;
  79. for (int i = 0;i < loc;i++)
  80. {
  81. q = q->next;
  82. if (q == NULL)
  83. {
  84. cout << "There are less than " << loc << " elements";
  85. return;
  86. }
  87. }
  88. q = q->prev;
  89. dnode *temp = new dnode;
  90. temp->data = num;
  91. temp->prev = q;
  92. temp->next = q->next;
  93. temp->next->prev = temp;
  94. q->next = temp;
  95. }
  96.  
  97. void linklist::d_delete(int num)
  98. {
  99. dnode *q = p;
  100. while (q != NULL)
  101. {
  102. if (q->data == num)
  103. if (q == p)
  104. {
  105. p = p->next;
  106. p->prev = NULL;
  107. }
  108. else
  109. {
  110. if (q->next = NULL)
  111. q->prev->next = NULL;
  112. else
  113. {
  114. q->prev->next = q->next;
  115. q->next->prev = q->prev;
  116. }
  117. delete q;
  118. }
  119. return;
  120. }
  121. q = q->next;
  122.  
  123. cout << "\n" << num << " not found";
  124. }
  125.  
  126. int main()
  127. {
  128. linklist P;
  129. cout << "Adding at beginning: " << endl;
  130. P.d_addatbeg(33);
  131. P.d_addatbeg(55);
  132. getch();
  133. return 0;
  134. }//end main

I am not sure where I have gone wrong and would appreciate it if someone could give some advice?
Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 66
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: help with double link list

 
0
  #2
Nov 23rd, 2008
Check you destructor
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: help with double link list

 
0
  #3
Nov 23rd, 2008
Not only destructor: d_addatbeg is wrong too (see empty list case)...

Please, use code tag correctly:
[code=cplusplus]
source
[/code]
Reply With Quote Quick reply to this message  
Reply

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




Views: 586 | Replies: 2
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC