GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Dec-2009, 20:01
cplusplus+- cplusplus+- is offline
New Member
 
Join Date: Dec 2009
Posts: 1
cplusplus+- is on a distinguished road

doubly linked list


Hi guys!
I am wondering how can one perform a sorted merge between the calling object list and the the passed in list?
Here is what I have got so far :
Code:
1. #include<iostream> 2. 3. using namespace std; 4. 5. #define NULL 0 6. 7. template <typename T> 8. class DoublyLinkedList; 9. 10. template <typename T> 11. class Node 12. { 13. friend class DoublyLinkedList<T>; 14. public: 15. Node() 16. {next = this; prev = this;} 17. Node(const T& data, Node<T> *next = NULL, Node<T> *prev = NULL) 18. {this ->data = data; this ->next = next; 19. this ->prev = prev;} 20. private: 21. T data; 22. Node<T> *next; 23. Node<T> *prev; 24. }; 25. 26. template <typename T> 27. class DoublyLinkedList 28. { 29. public: 30. DoublyLinkedList(); 31. DoublyLinkedList(T arr[], T arrSize); 32. ~DoublyLinkedList(); 33. Node<T> *insert(Node<T> *insertionPoint, const T& data); 34. void addSortedList(const DoublyLinkedList<T>& list2); 35. void display(); 36. private: 37. Node<T> *header; 38. int size; // number of data nodes in list 39. }; 40. 41. template<typename T> 42. DoublyLinkedList<T>::DoublyLinkedList() 43. { 44. header = new Node<T>(); 45. size = 0; 46. } 47. 48. template<typename T> 49. DoublyLinkedList<T>::DoublyLinkedList(T arr[], T arrSize) 50. { 51. header = new Node<T>(); 52. size = arrSize; 53. for (int i = 0; i < size; i++) 54. { 55. insert(header->prev, arr[i]); 56. } 57. } 58. 59. template<typename T> 60. DoublyLinkedList<T>::~DoublyLinkedList() 61. { 62. while (header->next != header) 63. { 64. Node<T>* next = header ->next; 65. header = next; 66. } 67. delete header; 68. } 69. 70. template<typename T> 71. Node<T> *DoublyLinkedList<T>::insert(Node<T> *insertionPoint, const T& data) 72. { 73. Node<T> *prevNodePtr; 74. Node<T> *newNodePtr; 75. prevNodePtr = insertionPoint ->prev; 76. newNodePtr = new Node<T>(data, insertionPoint, prevNodePtr); 77. size++; 78. insertionPoint ->prev = prevNodePtr->next = newNodePtr; 79. return newNodePtr; 80. } 81. 82. template<typename T> 83. void DoublyLinkedList<T>::addSortedList(const DoublyLinkedList<T> &list2) 84. { 85. DoublyLinkedList<T> current = list2; 86. DoublyLinkedList<T> *next; 87. while(header->next != header) 88. { 89. if (current.header->next < list2.header-> next) 90. { 91. header->next = current.header; 92. current.header = current.header ->next; 93. } 94. else 95. { 96. header->next = list2.header; 97. //list2.header = list2.header ->next; 98. } 99. 100. } 101. } 102. 103. template<typename T> 104. void DoublyLinkedList<T>::display() 105. { 106. if(header->next == header) 107. { 108. cout<<"the list is empty"<<endl; 109. } 110. else 111. { 112. Node<T> *next = header ->next; 113. header = next; 114. cout<<header; 115. } 116. }

my driver :
Code:
1. #include "DoublyLinkedList.h" 2. 3. int main() 4. { 5. int arrA[] = {30,90,95}; 6. int arrB[] = {10,20,40,95, 100}; 7. int arrASize = sizeof(arrA)/sizeof(int); 8. int arrBSize = sizeof(arrB)/sizeof(int); 9. 10. DoublyLinkedList<int> listA(arrA, arrASize); 11. DoublyLinkedList<int> listB(arrB, arrBSize); 12. DoublyLinkedList<int> listC; 13. 14. listC.display(); 15. listA.display(); 16. listA.addSortedList(listB); 17. listA.display(); 18. listB.display(); 19. return 0; 20. }

This should print

Empty linked list
30 90 95
10 20 30 40 90 95 100
30 90 95

I need help in addSortedList function, I think.

Thank you,
  #2  
Old 02-Dec-2009, 23:20
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 840
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: doubly linked list


I guess first you will need to be able to create the linked list.
CPP / C++ / C Code:
int main()
{
  int arrA[] = {30,90,95};
  int arrB[] = {10,20,40,95, 100};
  int arrASize = sizeof(arrA)/sizeof(int);
  int arrBSize = sizeof(arrB)/sizeof(int);

  cout << "dude0 " << endl;
  DoublyLinkedList<int> listA(arrA, arrASize);
  cout << "dude1 " << endl;
...
Code:
forums> ./091202_dllist.2 dude0 Segmentation fault
How could you get so far along without testing?
Moving on... and check this out:
CPP / C++ / C Code:
template<typename T>
DoublyLinkedList<T>::DoublyLinkedList(T arr[], T arrSize)
{
  header = new Node<T>();
  size = arrSize;
  cout << "dudex1  arrSize=" << arrSize << "  size=" << size << endl;
  for (int i = 0; i < size; i++)
  {
    cout << "dudex2  i=" << i << "  size=" << size << endl;
    insert(header->prev, arr[i]);
    if (i > 10) break;    // I added this too
  }
}
shows us that size is being incremented somehow:
Code:
forums> ./091202_dllist.2 dude0 dudex1 arrSize=3 size=3 dudex2 i=0 size=3 dudex2 i=1 size=4 dudex2 i=2 size=5 dudex2 i=3 size=6 dudex2 i=4 size=7 dudex2 i=5 size=8 dudex2 i=6 size=9 dudex2 i=7 size=10 dudex2 i=8 size=11 dudex2 i=9 size=12 dudex2 i=10 size=13 dudex2 i=11 size=14 dude1 dudex1 arrSize=5 size=5 dudex2 i=0 size=5 dudex2 i=1 size=6 dudex2 i=2 size=7 dudex2 i=3 size=8 dudex2 i=4 size=9 dudex2 i=5 size=10 dudex2 i=6 size=11 dudex2 i=7 size=12 dudex2 i=8 size=13 dudex2 i=9 size=14 dudex2 i=10 size=15 dudex2 i=11 size=16 dude2 dude3 the list is empty
So there's the cause of the segfault a runaway loop.
The question is why is size being incremented?
What do you think?
Last edited by Howard_L : 03-Dec-2009 at 00:31.
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Str_Misaligned in Double Link List Peter_APIIT C Programming Language 1 29-Feb-2008 21:50
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Code Snipet For Doubly Linked List Danny C Programming Language 8 19-Feb-2008 12:37
Doubly linked list KK2202 C++ Forum 2 13-Feb-2008 22:49

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:16.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.