判别两个集合A和B是否相等
#include <stdio.h>#include <stdlib.h>typedef struct LNode { int data; struct LNode *next;}*LinkList;int ListLength(LinkList &L){ int i; LinkList p; p = L -> next; for (i = 0; p != NULL; ++i) { p = p -> next; } return i;}int InitList(LinkList &L) { if (!L) { return 0; } L = (LinkList) malloc(sizeof(LNode)); L -> next = NULL; return 1;}int GetElem(LinkList &L, int i, int e) { LinkList p; int j; p = L -> next; for (j = 1; p && (j<i); ++j) { p = p -> next; } if(!p || (j>i)) { return 0; } e = p -> data; //取第i个元素 return e;}int ListInsert(LinkList &L, int i, int e){ LinkList p, q; int j; p = L; //把头指针地址赋给p //寻找第i-1个结点,即寻找i的前一结点 for (j = 0; p && (j < i-1); ++j) { p = p -> next; } //如果插入位置正确,p指向 i-1 if((!p) || (j > i-1)) { return 0; //插入位置错误 } q = (LinkList) malloc(sizeof(LNode)); q -> data = e; q -> next = p -> next; //把p指向i的地址赋给q p -> next = q; //把q的地址赋给p指向下一结点,即p指向 i+1 return 1;}int LocateElem(LinkList &L, int e) { LinkList p; int i; p = L -> next; for (i = 1; (p != NULL) && ((p -> data) != e); ++i) { p = p -> next; } if (p != NULL) { return i; } else { return 0; }}int ListDelete(LinkList &L, int i, int &f) { LinkList p, q; int j; p = L; //寻找第i个结点,并让p指向其前驱,(p -> next)判断是否到链尾 for (j = 0; (p -> next) && (j < i-1); ++j) { p = p -> next; } //删除位置不合理 if(!(p -> next) || (j > i-1)) { return 0; } //删除并释放结点 q = p -> next; p -> next = q -> next; q -> next = NULL; f = q -> data; free(q); return f;}int ListEmpty(LinkList &L) { if((L -> next) == NULL) { return 1; } else { return 0; }}int DestroyList(LinkList &L) { LinkList p; p = L -> next; //遍历单链表,依次释放每个结点 while(p != NULL) { L = p -> next; free(p); //释放当前结点 p = L -> next; } free(L); return 1;}int isequal(LinkList L1, LinkList L2){ int k, e, i; int found; int L1_len, L2_len; LinkList L3; found = true; L1_len = ListLength(L1); L2_len = ListLength(L2); L3 = (LinkList) malloc(sizeof(LNode)); if (L1_len != L2_len) { return false; } else { InitList(L3); for (k = 1; k <= L1_len; ++k) { GetElem(L1, k, e); ListInsert(L3, k, e); } for (k = 1; (k<=L2_len) && found; ++k) { GetElem(L2, k, e); i = LocateElem(L3, e); if (i == 0) { found = false; } else { ListDelete(L3, i, e); } } if (found && ListEmpty(L3)) { return true; } else { return false; } DestroyList(L3); }}int main(void){ int i, j; int m, n, e, f; LinkList L1, L2; L1 = (LinkList) malloc(sizeof(LNode)); L2 = (LinkList) malloc(sizeof(LNode)); if (InitList(L1) && InitList(L2)) { printf("链表1, 2初始化成功\n\n"); } printf("请输入A集合元素个数:"); scanf("%d", &m); printf("请输入结点元素:"); for (i = 1; i <= m; ++i) { scanf("%d", &e); ListInsert(L1, i, e); } putchar('\n'); printf("请输入B集合元素个数:"); scanf("%d", &n); printf("请输入结点元素:"); for (j = 1; j <= n; ++j) { scanf("%d", &f); ListInsert(L2, j, f); } putchar('\n'); if (isequal(L1, L2)) { printf("A,B相等\n\n"); } else { printf("A,B不等\n\n"); } return 0;}
if (L1_len != L2_len) { return false; } else { InitList(L3); for (k = 1; k <= L1_len; ++k) { GetElem(L1, k, e);//这个函数你是通过他的返回值来获得链表的值,然后插入L3中,所以 //但是你传递的变量e并未获得链表的值,如果要获得的话,你可以在//将函数定义改为GetElem(L1, k, *e), //然后传参可以写GetElem(L1, k, &e),但依据你这里做法//你只能e = GetElem(L1,k,e)然后再插入数//据,否则e将会是随机 值,也就是说,L1与L2表的数据是永远不会相同的。 ListInsert(L3, k, e); } for (k = 1; (k<=L2_len) && found; ++k) { GetElem(L2, k, e); i = LocateElem(L3, e); if (i == 0) { found = false; } else { ListDelete(L3, i, e); } }
[解决办法]
O(∩_∩)O哈哈~,我找到了,编译成功,
int LinkList &L, int i, int &e) {}
就在函数GetElem(加了&在e前面,,,其他都没改,,运行去吧