Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node node;
- struct node {
- int value;
- node* link;
- };
- node* pushBack(node* tail, int value) {
- node* newNode = (node*)calloc(1,sizeof(struct node));
- newNode->value = value;
- newNode->link = newNode;
- if (!tail) {
- tail = newNode;
- }
- else {
- node* next = tail->link;
- tail->link = newNode;
- newNode->link = next;
- tail = newNode;
- }
- return tail;
- }
- void printCircularList(node* tail) {
- if (!tail) return;
- node* p = tail->link;
- do {
- printf("%d ", p->value);
- p = p->link;
- } while (p != tail->link);
- }
- node* deleteNode(node * tail, int value) {
- if (!tail) return NULL;
- node* prev = tail;
- node* cur = tail->link;
- if (prev == cur) {
- if (cur->value == value) {
- free(cur);
- tail = NULL;
- }
- return tail;
- }
- do {
- if (cur->value == value) {
- node* del = cur;
- prev->link = cur->link;
- if (cur == tail)tail = prev;
- cur = cur->link;
- free(del);
- }
- else {
- prev = cur;
- cur = cur->link;
- }
- } while (cur != tail->link);
- return tail;
- }
- node* concatenateCircularList(node * tailA, node * tailB) {
- if (!tailA && !tailB) return NULL;
- if (!tailA) return tailB;
- if (!tailB) return tailA;
- node* headA = tailA->link;
- tailA->link = tailB->link;
- tailB->link = headA;
- return tailB;
- }
- int main() {
- node* a = NULL, * b = NULL;
- FILE* fp = fopen("A.txt", "r");
- for (int t; ~fscanf(fp, "%d", &t); )
- a = pushBack(a, t);
- fclose(fp);
- fp = fopen("B.txt", "r");
- for (int t; ~fscanf(fp, "%d", &t); )
- b = pushBack(b, t);
- fclose(fp);
- node * d = concatenateCircularList(a, b);
- printCircularList(d);
- puts("");
- fp = fopen("C.txt", "r");
- for (int t; ~fscanf(fp, "%d", &t); ) {
- d = deleteNode(d, t);
- }
- fclose(fp);
- printCircularList(d);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement