#include <stdio.h> #include <stdlib.h> #define Elemtype int #define ERROR -1 typedef struct Node { Elemtype e; Node* next; }Node,*LinkList; void InitList(LinkList& pHead) { pHead = (Node*)malloc(sizeof(Node)); pHead->next = NULL; } void Visit(Elemtype e) { printf("%d ", e); } int ListTravel(LinkList pHead) { if (pHead == NULL) return false; Node* p; p = pHead->next; while (p!=NULL) { Visit(p->e); p = p->next; } return true; } void DestoryList(LinkList pHead) { Node* p = pHead->next; Node* q = NULL; while (p!=NULL) { q = p->next; free(p); p = q; } free(pHead); pHead = NULL; } void ClearList(LinkList pHead) { Node* p = pHead->next; Node* q; while (p != NULL) { q = p->next; free(p); p = q; } pHead->next = NULL; } int ListEmpty(LinkList pHead) { if (pHead == NULL) //排除链表不存在 { printf("链表不存在\n"); return ERROR; } if (pHead->next != NULL) return false; return true; } int ListLength(LinkList pHead) { if (pHead == NULL) { printf("链表不存在\n"); return ERROR; } Node* p; p = pHead->next; int count = 0; while (p) { ++count; p = p->next; } return count; } int GetElem(LinkList pHead, int i, Elemtype& e) { if (i < 0 || i >= ListLength(pHead)) { printf("不合法的下标\n"); return ERROR; } Node* p; p = pHead->next; int count = 0; while (p!=NULL) { if (count == i) { e = p->e; return true; } count++; p = p->next; } } Node* LocateElem(LinkList pHead, Elemtype x) { Node* p; if (pHead == NULL) { printf("链表不存在\n"); return NULL; } p = pHead->next; while (p) { if (p->e == x) return p; p = p->next; } return NULL; } Node* LocateSecondElem(LinkList pHead, Elemtype x) { // 查询第二次出现的元素位置 Node* p; int count = 0; if (pHead == NULL) { printf("链表不存在\n"); return NULL; } p = pHead->next; while (p) { if (p->e == x) { count++; if (count == 2) { return p; } } p = p->next; } return NULL; } int ListInsert(LinkList pHead, int i, Elemtype e) { if (pHead == NULL) { printf("链表不存在\n"); return ERROR; } if (i < 0 || i > ListLength(pHead)) { printf("不合法的下标\n"); return ERROR; } Node* p = pHead; Node* q = (Node*)malloc(sizeof(Node)); int count = 0; q->e = e; while (p) { if (count == i) { q->next = p->next; p->next = q; break; } p = p->next; ++count; } return 0; } int ListDelete(LinkList pHead, int i, Elemtype& e) { if (pHead == NULL) { printf("链表不存在\n"); return ERROR; } if (i < 0 || i >= ListLength(pHead)) { printf("不合法的下标\n"); return ERROR; } Node* p = pHead; Node* q = p->next; int count = 0; while (p) { if (count == i) { p->next = q->next; free(q); break; } p = p->next; q = p->next; ++count; } return 0; } int PriorElement(LinkList pHead, Elemtype cur, Elemtype& prior) { Node* p = LocateElem(pHead, cur); if (p == NULL) { printf("找不到这个元素\n"); return ERROR; } Node* q = pHead; if (q->next == p) { printf("第一个元素没有前驱\n"); return false; } while (q->next != p) { q = q->next; } prior = q->e; return true; } int NextElement(LinkList pHead, Elemtype cur, Elemtype& e) { Node* p = LocateElem(pHead, cur); if (p == NULL) { printf("找不到这个元素\n"); return ERROR; } if (p->next == NULL) { printf("最后一个元素没有后继\n"); return ERROR; } e = p->next->e; return true; } void InputList(LinkList pHead) { int length; int elem; printf("输入要初始化的集合元素个数:"); scanf_s("%d", &length); printf("分别输入所有元素\n"); for (int i = 0; i < length; i++) { scanf_s("%d", &elem); ListInsert(pHead, i, elem); } } int UnionList(LinkList La, LinkList Lb) { int length = ListLength(Lb); int e; for (int i = 0;i < length; ++i) { GetElem(Lb, i, e); if (LocateElem(La, e) == NULL) { ListInsert(La, 0, e); } } return 0; } int main() { LinkList La,Lb; InitList(La); InitList(Lb); InputList(La); InputList(Lb); UnionList(La, Lb); ListTravel(La); DestoryList(La); DestoryList(Lb); return 0; }