fork download
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. class doubled
  5. {
  6. private:
  7. class sublink
  8. {
  9. private:
  10. char data[30];
  11. sublink *next_ptr;
  12. sublink *previous_ptr;
  13. friend doubled;
  14. };
  15.  
  16. public:
  17. sublink *first_ptr;
  18. doubled(){first_ptr = NULL;};
  19. void add_item(char *item);
  20. void rm_item(char *item);
  21. };
  22.  
  23. void doubled::add_item(char *item)
  24. {
  25. sublink *new_data;
  26. new_data = new sublink;
  27.  
  28. strcpy(new_data->data, item);
  29.  
  30. // empty list case
  31. if(first_ptr == NULL)
  32. {
  33. // Only item in the list, I have no next or previous element
  34. new_data->previous_ptr = NULL;
  35. new_data->next_ptr = NULL;
  36.  
  37. // Make the list point to this element
  38. first_ptr = new_data;
  39. }
  40. else
  41. {
  42. sublink* iter;
  43. iter = first_ptr;
  44.  
  45. // 1 element list
  46. if(iter->next_ptr == NULL)
  47. {
  48. // I'm after the first and only node
  49. if(strcmp(iter->data, new_data->data) <= 0)
  50. {
  51. iter->next_ptr = new_data;
  52. new_data->previous_ptr = iter;
  53. new_data->next_ptr = NULL;
  54. }
  55. // I'm before the first and only node and thefore I become the new first
  56. else
  57. {
  58. iter->previous_ptr = new_data;
  59. new_data->next_ptr = iter;
  60. first_ptr = iter;
  61. }
  62. }
  63. // 2+ element list
  64. else
  65. {
  66. // this is never null the first time because empty list case is take care of above
  67. while(iter != NULL)
  68. {
  69. // Should I be inserted before the current node?
  70. if(strcmp(iter->data, new_data->data) >= 0)
  71. {
  72. // first node case
  73. if(iter->previous_ptr == NULL)
  74. {
  75. new_data->previous_ptr = NULL;
  76. new_data->next_ptr = iter;
  77. iter->previous_ptr = new_data;
  78. first_ptr = new_data;
  79.  
  80. }
  81. // intermediate node case
  82. else if(iter->next_ptr != NULL)
  83. {
  84. iter->previous_ptr->next_ptr = new_data;
  85. new_data->previous_ptr = iter->previous_ptr;
  86. new_data->next_ptr = iter;
  87. iter->previous_ptr = new_data;
  88. }
  89. // last node case
  90. else
  91. {
  92. iter->next_ptr = new_data;
  93. new_data->previous_ptr = iter;
  94. new_data->next_ptr = NULL;
  95. }
  96.  
  97. break;
  98. }
  99.  
  100. // Move to next node
  101. iter = iter->next_ptr;
  102. }
  103. }
  104. }
  105.  
  106. // Print the list
  107. std::cout << "List: " << std::endl;
  108. sublink* printer = first_ptr;
  109. while(printer != NULL)
  110. {
  111. std::cout << '\t' << printer->data << std::endl;
  112.  
  113. printer = printer->next_ptr;
  114. }
  115. std::cout << std::endl;
  116. }
  117.  
  118. int main(int argc, char* argv[])
  119. {
  120. doubled d;
  121.  
  122. char item[30] = "bla bla bla\0";
  123. char item2[30] = "meh\0";
  124. char item3[30] = "ahhhhhhhh\0";
  125. char item4[30] = "dayummmmm\0";
  126. d.add_item(item);
  127. d.add_item(item2);
  128. d.add_item(item3);
  129. d.add_item(item4);
  130.  
  131. std::cin.get();
  132. return 0;
  133. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
List: 
	bla bla bla

List: 
	bla bla bla
	meh

List: 
	ahhhhhhhh
	bla bla bla
	meh

List: 
	ahhhhhhhh
	bla bla bla
	meh
	dayummmmm