fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct node
  5. {
  6. node *next,*back;
  7. int value;
  8. };
  9.  
  10. struct list
  11. {
  12. node *head,*tail;
  13. };
  14.  
  15. node *make_node(int value,node *next,node *back)
  16. {
  17. node *tmp=new node;
  18. tmp->value=value;
  19. tmp->next=next;
  20. tmp->back=back;
  21. return tmp;
  22. }
  23.  
  24. void add_start(list &L,int value)
  25. {
  26. L.head=(L.head?L.head->back:L.tail)=make_node(value,L.head,0);
  27. }
  28.  
  29. void show_list(const list &L)
  30. {
  31. for(node *i=L.head;i;i=i->next) cout<<i->value<<"\t";
  32. cout<<endl;
  33. }
  34.  
  35. void sort(list &L)
  36. {
  37. if((L.head)&&(L.head!=L.tail))
  38. {
  39. for(bool more=true;more;)
  40. {
  41. more=false;
  42. for(node *b=L.head,*n=b->next;n;b=n,n=b->next)
  43. {
  44. if(b->value>n->value)
  45. {
  46. (b->back?b->back->next:L.head)=n;
  47. (n->next?n->next->back:L.tail)=b;
  48. n->back=b->back;
  49. b->next=n->next;
  50. n->next=b;
  51. b->back=n;
  52. more=true;
  53. }
  54. }
  55. }
  56. }
  57. }
  58.  
  59. int main()
  60. {
  61. list L={0,0};
  62. add_start(L,1);
  63. add_start(L,2);
  64. add_start(L,3);
  65. show_list(L);
  66. sort(L);
  67. show_list(L);
  68. return 0;
  69. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
3	2	1	
1	2	3