Advertisement
Guest User

Untitled

a guest
May 25th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct node node;
  4. struct node {
  5. int value;
  6. node* link;
  7. };
  8. node* pushBack(node* tail, int value) {
  9. node* newNode = (node*)calloc(1,sizeof(struct node));
  10. newNode->value = value;
  11. newNode->link = newNode;
  12. if (!tail) {
  13. tail = newNode;
  14. }
  15. else {
  16. node* next = tail->link;
  17. tail->link = newNode;
  18. newNode->link = next;
  19. tail = newNode;
  20. }
  21. return tail;
  22. }
  23. void printCircularList(node* tail) {
  24. if (!tail) return;
  25. node* p = tail->link;
  26. do {
  27. printf("%d ", p->value);
  28. p = p->link;
  29. } while (p != tail->link);
  30. }
  31.  
  32. node* deleteNode(node * tail, int value) {
  33. if (!tail) return NULL;
  34. node* prev = tail;
  35. node* cur = tail->link;
  36. if (prev == cur) {
  37. if (cur->value == value) {
  38. free(cur);
  39. tail = NULL;
  40. }
  41. return tail;
  42. }
  43. do {
  44. if (cur->value == value) {
  45. node* del = cur;
  46. prev->link = cur->link;
  47. if (cur == tail)tail = prev;
  48. cur = cur->link;
  49. free(del);
  50. }
  51. else {
  52. prev = cur;
  53. cur = cur->link;
  54. }
  55. } while (cur != tail->link);
  56. return tail;
  57. }
  58. node* concatenateCircularList(node * tailA, node * tailB) {
  59. if (!tailA && !tailB) return NULL;
  60. if (!tailA) return tailB;
  61. if (!tailB) return tailA;
  62. node* headA = tailA->link;
  63. tailA->link = tailB->link;
  64. tailB->link = headA;
  65. return tailB;
  66. }
  67. int main() {
  68. node* a = NULL, * b = NULL;
  69. FILE* fp = fopen("A.txt", "r");
  70. for (int t; ~fscanf(fp, "%d", &t); )
  71. a = pushBack(a, t);
  72. fclose(fp);
  73. fp = fopen("B.txt", "r");
  74. for (int t; ~fscanf(fp, "%d", &t); )
  75. b = pushBack(b, t);
  76. fclose(fp);
  77.  
  78. node * d = concatenateCircularList(a, b);
  79. printCircularList(d);
  80. puts("");
  81. fp = fopen("C.txt", "r");
  82. for (int t; ~fscanf(fp, "%d", &t); ) {
  83. d = deleteNode(d, t);
  84. }
  85. fclose(fp);
  86. printCircularList(d);
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement