fork download
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. struct Node {
  7. int info;
  8. struct Node *next;
  9. struct Node *prev;
  10. };
  11.  
  12. //globals
  13. Node *front = NULL, *back = NULL;
  14.  
  15. void add_to_right(int);
  16. void add_after(int,int);
  17. void add_before(int,int);
  18. void remove_node(int);
  19. void display_from_left_to_right();
  20. void display_from_right_to_left();
  21.  
  22. int main() {
  23. int whichnode, whatvalue;
  24. int removenode;
  25.  
  26. for(int i=1;i<=9;i++)
  27. add_to_right( i );
  28.  
  29. display_from_left_to_right();
  30. display_from_right_to_left();
  31.  
  32.  
  33. cout<<endl;
  34. cout<<"insert after node=";
  35. cin>>whichnode;
  36. cout<<"which one value=";
  37. cin>>whatvalue;
  38. add_after(whichnode, whatvalue);
  39. display_from_left_to_right();
  40.  
  41.  
  42. cout<<endl;
  43. cout<<"insert before node=";
  44. cin>>whichnode;
  45. cout<<"which one value=";
  46. cin>>whatvalue;
  47. add_before(whichnode, whatvalue);
  48. display_from_left_to_right();
  49.  
  50. cout<<endl;
  51. cout<<"Node to remove=";
  52. cin>>removenode;
  53. remove_node(removenode);
  54. display_from_left_to_right();
  55.  
  56.  
  57. return (0);
  58. };
  59.  
  60. void add_to_right(int whatvalue) {
  61.  
  62. struct Node *c;
  63.  
  64. if(front == NULL) {
  65.  
  66. c = new Node;
  67. c->info = whatvalue;
  68. c->next = c->prev = NULL;
  69. front = back = c;
  70.  
  71. } else {
  72.  
  73. c = new Node;
  74. c->info = whatvalue;
  75. c->next = NULL;
  76. c->prev = back;
  77. back->next = c;
  78. back = c;
  79. }
  80.  
  81. };
  82.  
  83. void add_after(int whichone, int whatvalue) {
  84.  
  85. struct Node *c = front, *p;
  86.  
  87. if(back->info == whichone) {
  88.  
  89. p = new Node;
  90. p->info = whatvalue;
  91. p->next = NULL;
  92. p->prev = back;
  93. back->next = p;
  94. back = p;
  95.  
  96. } else {
  97.  
  98. while(c->info != whichone) c = c->next;
  99.  
  100. p = new Node;
  101. p->info = whatvalue;
  102. p->next = c->next;
  103. p->prev = c;
  104. c->next->prev = p;
  105. c->next = p;
  106. }
  107. };
  108.  
  109. void remove_node(int removenode) {
  110.  
  111. struct Node *removal, *c;
  112.  
  113.  
  114. if(front->info == removenode) {
  115.  
  116. removal = front;
  117.  
  118. front = front->next;
  119. front->prev = 0;
  120.  
  121. delete removal;
  122.  
  123. } else if(front->info != removenode && back->info != removenode){
  124.  
  125. c = front;
  126.  
  127. while(c->info != removenode) c = c->next;
  128.  
  129. c->next->prev = c->prev;
  130. c->prev->next = c->next;
  131.  
  132. delete c;
  133.  
  134. } else if(back->info == removenode) {
  135.  
  136. removal = back;
  137.  
  138. back = back->prev;
  139. back->next = NULL;
  140.  
  141. delete removal;
  142. }
  143. };
  144.  
  145. void display_from_left_to_right() {
  146.  
  147. struct Node *c;
  148.  
  149. cout<<endl;
  150.  
  151. for(c = front; c; c = c->next)
  152.  
  153. cout<<c->info<<" ";
  154. };
  155.  
  156. void display_from_right_to_left() {
  157.  
  158. struct Node *c = back;
  159.  
  160. cout<<endl;
  161.  
  162. for(c = back; c; c = c->prev)
  163.  
  164. cout<<c->info<<" ";
  165. };
  166.  
  167. void add_before(int whichnode,int whatvalue) {
  168.  
  169. struct Node *p,*c;
  170.  
  171. if(front->info == whichnode) {
  172.  
  173. p = new Node;
  174. p->info = whatvalue;
  175. p->next = front;
  176. front->prev = p;
  177. front = p;
  178.  
  179. } else {
  180.  
  181. c = front;
  182.  
  183. while(c->next->info != whichnode) c = c->next;
  184.  
  185. p = new Node;
  186. p->info = whatvalue;
  187. p->next = c->next;
  188. p->prev = c;
  189. c->next->prev = p;
  190. c->next = p;
  191. }
  192. }
Success #stdin #stdout 0.01s 5296KB
stdin
3
9999
8
88888
5
stdout
1 2 3 4 5 6 7 8 9 
9 8 7 6 5 4 3 2 1 
insert after node=which one value=
1 2 3 9999 4 5 6 7 8 9 
insert before node=which one value=
1 2 3 9999 4 5 6 7 88888 8 9 
Node to remove=
1 2 3 9999 4 6 7 88888 8 9