Advertisement
Guest User

Untitled

a guest
May 26th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node
  6. {
  7. int info;
  8. node *next;
  9. };
  10.  
  11. struct list
  12. {
  13. node * head;
  14. node * tail;
  15. };
  16.  
  17. node * creatnode(int x)
  18. {
  19. node *p=new node;
  20. p->info=x;
  21. p->next=NULL;
  22. return p;
  23. }
  24.  
  25. void creatlist(list & l)
  26. {
  27. l.head=l.tail=NULL;
  28. }
  29.  
  30. void themsau(list &l,node *p)
  31. {
  32. if (l.head==NULL)
  33. {
  34. l.head=p;
  35. l.tail=l.head;
  36. }
  37. else
  38. {
  39. l.tail->next=p;
  40. l.tail=p;
  41. }
  42. }
  43.  
  44. void taolist(list &l,int n)
  45. {
  46.  
  47. for (int i=1;i<=n;i++)
  48. {
  49. themsau(l,creatnode(i));
  50. }
  51. }
  52.  
  53. void xoadau(list & l)
  54. {
  55. node * p;
  56. if (l.head!=NULL)
  57. {
  58. p=l.head;
  59. l.head=p->next;
  60. delete p;
  61. p=NULL;
  62. }
  63. else
  64. l.tail=NULL;
  65. }
  66.  
  67. void xoa( list & l,node *&X)
  68. {
  69. if (l.head== X)
  70. {
  71.  
  72. xoadau(l);
  73. X=l.head;
  74. return;
  75. }
  76.  
  77. node *tr = l.head;
  78.  
  79. while (tr->next != X)
  80. tr = tr->next;
  81.  
  82. node *p = tr->next;
  83. tr->next = p->next;
  84.  
  85. if (p == l.tail)
  86. l.tail = tr;
  87.  
  88. X=tr->next;
  89.  
  90. if(X==NULL)
  91. X=l.head;
  92.  
  93. delete p;
  94. p=NULL;
  95. }
  96. void xuli(list & l,int n,int m)
  97. {
  98. node * p=new node;
  99. p=l.head;
  100. int k=1;
  101.  
  102. while (n>0)
  103. {
  104.  
  105. while (k%m!=0)
  106. {
  107.  
  108. p=p->next;
  109. if(p==NULL&&p!=l.head)
  110. p=l.head;
  111. k++;
  112. }
  113. k++;
  114. cout<< p->info;
  115. xoa(l,p);
  116. n--;
  117. }
  118. }
  119.  
  120. int main()
  121. {
  122. list l;
  123. int m,n;
  124. cout<<" nhap so nguoi : " ;
  125. cin>> n;
  126. cout<< " nhap thu tu nguoi bi giet : ";
  127. cin>>m;
  128. creatlist(l);
  129. taolist(l,n);
  130. xuli(l,n,m);
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement