fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. typedef struct _NODE{
  7. int Data;
  8. _NODE* Left;
  9. _NODE* Right;
  10. }NODE;
  11.  
  12. int n, k;
  13. NODE* arr[5001];
  14.  
  15. NODE* createNode(int );
  16. void linkedNodes();
  17. void displayList();
  18. void removeList(NODE*);
  19.  
  20. int main(void){
  21. cin >> n >> k;
  22. for(int i = 1; i <= n; i++){
  23. arr[i] = createNode(i);
  24. }
  25. linkedNodes();
  26. displayList();
  27. //removeList(arr[1]);
  28. }
  29.  
  30. NODE* createNode(int data){
  31. NODE* temp = (NODE*)malloc(sizeof(NODE));
  32. temp -> Data = data;
  33. temp -> Left = NULL;
  34. temp -> Right = NULL;
  35. return temp;
  36. }
  37.  
  38. void linkedNodes(){
  39. arr[1] -> Left = arr[n];
  40. arr[n] -> Right = arr[1];
  41. for(int i = 1; i <= n; i++){
  42. if(i == 1){
  43. arr[i] -> Right = arr[i+1];
  44. arr[i] -> Right -> Left = arr[i];
  45. }else if(i == n){
  46. arr[i] -> Right = arr[1];
  47. arr[i] -> Left = arr[i - 1];
  48. arr[i] -> Right -> Left = arr[i];
  49. arr[i] -> Left -> Right = arr[i];
  50. }else{
  51. arr[i] -> Right = arr[i+1];
  52. arr[i] -> Left = arr[i-1];
  53. arr[i] -> Right -> Left = arr[i];
  54. arr[i] -> Left -> Right = arr[i];
  55. }
  56. }
  57. }
  58.  
  59. void displayList(){
  60.  
  61. int cnt = 1;
  62. int c = 0;
  63. NODE* tmp = arr[1];
  64. cout <<"<";
  65. while(tmp != NULL){
  66. if(cnt % k == 0){ // cnt가 k로 나머지로 나눴을 때 0이 되면 출력!
  67. // 출력을 하고 해당 노드를 삭제를 한 후에 양 옆 노드를 연결!
  68. NODE* curr = tmp;
  69. tmp -> Left -> Right = tmp -> Right;
  70. tmp -> Right -> Left = tmp -> Left;
  71. c++;
  72. free(curr);
  73. if(c == n){ // 총 n개 만큼만 출력하면 되므로 c가 n일때 break;
  74. cout << tmp -> Data << ">" << endl;
  75. break;
  76. }else{
  77. cout << tmp -> Data << ", ";
  78. }
  79. }
  80. tmp = tmp -> Right;
  81. cnt++;
  82. }
  83. }
  84.  
  85.  
  86. /*
  87. void removeList(NODE* arr){
  88.   NODE* curr = arr -> Right;
  89.   while(curr != NULL){
  90.   NODE* next = curr -> Right;
  91.   free(curr);
  92.   curr = next;
  93.   }
  94.   free(arr);
  95.   }
  96. */
Success #stdin #stdout 0s 15272KB
stdin
7 3
stdout
<0, 952327264, 952327360, 952327232, 952327392, 952327328, 952327200>