User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,921 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,688 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Dec 14th, 2004
Views: 3,298
A singly (sp) and a Doubly Linked list class to create linked lists. Can be used to make dynamic arrays and could also be templated to work with any data type (this is old code and i didnt know how to at the time). Has been extremely useful to me even in its bare form like this.
cplusplus Syntax | 5 stars
  1. #include <stdio.h>
  2. #include <iostream.h>
  3.  
  4. struct item
  5. {
  6. long data;
  7. item *next;
  8. item *previous;
  9.  
  10. item(void)
  11. {
  12. data = 0;
  13. next = previous = NULL;
  14. }
  15. };
  16.  
  17. /* Single Linked List
  18.  *
  19.  * Structure: head -> next -> next .... -> tail (null)
  20. */
  21.  
  22. struct SLLIST
  23. {
  24. item *head;
  25. item *tail;
  26. item *current;
  27. unsigned int listcount;
  28.  
  29. SLLIST(void)
  30. {
  31. head = new item;
  32. tail = current = head;
  33. listcount = 1;
  34. }
  35. ~SLLIST(void)
  36. {
  37. item *temp = head;
  38. current = head;
  39. while (current != NULL)
  40. {
  41. current = current->next;
  42. delete temp;
  43. temp = current;
  44. }
  45. }
  46. int operator[](int index) /* index list like a normal array */
  47. {
  48. if(index >= listcount)
  49. index %= listcount; // wrap array
  50. item *temp = head;
  51. for(int i = 0; i < index; i++)
  52. temp = temp->next;
  53. current = temp;
  54. return temp->data;
  55. }
  56. void addnode(int data)
  57. {
  58. tail->next = new item;
  59. tail = tail->next;
  60. tail->data = data;
  61. listcount++;
  62. }
  63. void PrintToConsole(void)
  64. {
  65. item *temp = head;
  66. current = head;
  67. while (current != NULL)
  68. {
  69. current = current->next;
  70. cout << temp->data << "\n";
  71. temp = current;
  72. }
  73. }
  74. };
  75.  
  76. /* Doubly linked list
  77.  *
  78.  * Structure: Head <-> next <-> next .... <-> tail (null)
  79. */
  80.  
  81. struct DLLIST
  82. {
  83. item *head;
  84. item *tail;
  85. item *current;
  86. unsigned int listcount;
  87.  
  88. DLLIST(void)
  89. {
  90. head = new item;
  91. tail = current = head;
  92. listcount = 1;
  93. }
  94. ~DLLIST(void)
  95. {
  96. item *temp = head;
  97. current = head;
  98. while (current != NULL)
  99. {
  100. current = current->next;
  101. delete temp;
  102. temp = current;
  103. }
  104. }
  105. int operator[](int index) /* index list like a normal array */
  106. {
  107. if(index >= listcount)
  108. index %= listcount; // wrap array
  109. item *temp = head;
  110. for(int i = 0; i < index; i++)
  111. temp = temp->next;
  112. current = temp;
  113. return temp->data;
  114. }
  115. void advance(void)
  116. {
  117. if(current->next != NULL)
  118. current = current->next;
  119. }
  120. void rewind(void)
  121. {
  122. if(current->previous != NULL)
  123. current = current->previous;
  124. }
  125. void addnode(int data)
  126. {
  127. tail->next = new item;
  128. tail->next->previous = tail; // link new back to old tail
  129. tail = tail->next;
  130. tail->data = data;
  131. listcount++;
  132. }
  133. void PrintToConsole(void)
  134. {
  135. item *temp = head;
  136. current = head;
  137. while (current != NULL)
  138. {
  139. current = current->next;
  140. cout << temp->data << "\n";
  141. temp = current;
  142. }
  143. }
  144. };
Comments (Newest First)
1o0oBhP | Posting Pro in Training | Dec 15th, 2004
Cheers! Err, looks like i havent pasted the whole lot! it looks a bit short if theres any more list functions anyone wants put a comment up and i will have a go at adding it in
cscgal | The Queen of DaniWeb | Dec 14th, 2004
Thank you
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 8:10 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC