3.1主要涉及循环单链表的初始化、尾插建表和合并带头指针的循环单链表 *** 作,代码实现如下:
#includetypedef char ElemType; typedef struct CircullinkList { ElemType data; struct CircullinkList* next; }Node, * ClinkList; void InitList(ClinkList* CL); void PrintList(ClinkList CL); void CreateClinkList(ClinkList CL); ClinkList Merge_1(ClinkList CL1, ClinkList CL2); //ClinkList Merge_2(ClinkList CL1, ClinkList CL2); void InitList(ClinkList* CL) { *CL = (ClinkList)malloc(sizeof(Node)); (*CL)->next = *CL; } void PrintList(ClinkList CL) { Node* cur = CL->next; while (cur != CL) { printf("%c ", cur->data); cur = cur->next; } printf("n"); } void CreateClinkList(ClinkList CL) { Node* rear = CL; Node* s; char c; int flag = 1; while (flag) { c = getchar(); if (c != '$') { s = (Node*)malloc(sizeof(Node)); s->data = c; rear->next = s; s->next = CL; rear = rear->next; } else { flag = 0; } } rewind(stdin); } ClinkList Merge_1(ClinkList CL1, ClinkList CL2) { Node* rear1 = CL1->next; Node* rear2 = CL2->next; while (rear1->next != CL1) { rear1 = rear1->next; } while (rear2->next != CL2) { rear2 = rear2->next; } rear1->next = CL2->next; rear2->next = CL1; free(CL2); return CL1; } //ClinkList Merge_2(ClinkList CL1, ClinkList CL2) //{ // Node* h; // h = CL1->next; // CL1->next = CL2->next->next; // free(CL2->next); // CL2->next = h; // // return CL2; //} void TestClinkList() { ClinkList CL1; ClinkList CL2; InitList(&CL1); InitList(&CL2); CreateClinkList(CL1); CreateClinkList(CL2); PrintList(Merge_1(CL1, CL2)); } int main() { TestClinkList(); return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)