Void-voiD

Untitled

Dec 22nd, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Elem {
  5. struct Elem *prev, *next;
  6. int v;
  7. };
  8.  
  9. struct Elem *InitDoubleLinkedList()
  10. {
  11. struct Elem *x = (struct Elem*)malloc(sizeof(struct Elem));
  12. x->v = -777777777;
  13. x->prev = x;
  14. x->next = x;
  15. return x;
  16. }
  17.  
  18. void InsertSort(struct Elem *l)
  19. {
  20.  
  21. }
  22.  
  23. int main()
  24. {
  25. int n, i, x, j;
  26. scanf("%d\n", &n);
  27. if (n == 1) {
  28. scanf("%d", &i);
  29. printf("%d\n", i);
  30. }
  31. else {
  32. struct Elem *l = InitDoubleLinkedList();
  33. struct Elem *now = (struct Elem*)malloc(sizeof(struct Elem));
  34. for (i = 0; i < n; i++) {
  35. if (i == 0) {
  36. struct Elem *cur = (struct Elem*)malloc(sizeof(struct Elem));
  37. scanf("%d ", &x);
  38. cur->v = x;
  39. cur->prev = l;
  40. now = cur;
  41. }
  42. if (i < n - 1 && i != 0) {
  43. struct Elem *cur = (struct Elem*)malloc(sizeof(struct Elem));
  44. scanf("%d ", &x);
  45. cur->v = x;
  46. cur->prev = now;
  47. now->next = cur;
  48. now = cur;
  49.  
  50. }
  51. if (i == n - 1) {
  52. struct Elem *cur = (struct Elem*)malloc(sizeof(struct Elem));
  53. scanf("%d", &x);
  54. cur->v = x;
  55. cur->prev = now;
  56. now->next = cur;
  57. cur->next = l;
  58. }
  59. }
  60. free(now);
  61. struct Elem *f, *g;
  62. f = l->next;
  63. for (i = 0; i < n; i++) {
  64. printf("%d ", f->v);
  65. f = f->next;
  66. }
  67. printf("\n");
  68. f = l->next;
  69. while (f->v != l->v) {
  70. g = f->next;
  71. free(f);
  72. f = g;
  73. }
  74. free(l);
  75. }
  76. return 0;
  77. }
Add Comment
Please, Sign In to add comment