欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

例 2-1 假设两个集合 A 和 B 分别用两个线性表 LA 和 LB 表示(即线性表中的数据元素就是集合的成员),现在需要一个新的集合 A = AUB,这就要求对线性表进行如下操作: 扩展线性表 LA。

最编程 2024-06-21 15:25:07
...
#include <stdio.h> #include <stdlib.h> //Function result status code #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASALBE -1 #define OVERFLOW -2 //Status is the type of function whose value is the function result status code typedef int Status; typedef struct{ int *data; int length; }List; void output(List *L); Status InitList(List *L); Status DestroyList(List *L); Status ClearList(List *L); Status ListEmpty(List *L); Status ListLength(List *L); Status GetElem(List *L, int i, int *e); Status compare(List *a, int b); Status LocateElem(List *L, int e, Status (*pfun)(List *ce, int num)); Status PriorElem(List *L, int cur_e, int *pre_e); Status NextElem(List *L, int cur_e, int *next_e); Status ListInsert(List *L, int i, int e); Status ListDelete(List *L, int i, int *e); Status visit(List *L); Status ListTraverse(List *L, Status (*pfun)(List *ce)); void UnionList(List *La, List *Lb); void output(List *L){ int i; printf("Result: "); for(i=0; i<ListLength(L); i++) printf("%d ", L->data[i]); printf("\n"); } Status InitList(List *L){ L->data=(int*)malloc(30*sizeof(int)); if(!L) exit(OVERFLOW); L->length=0; return OK; }//InitList Status DestroyList(List *L){ free(L); return OK; }//DestroyList Status ClearList(List *L){ int i; for(i=0; i<=ListLength(L); i++){ if(L->data[i]!=0) L->data[i]=0; L->length--; } L->length--; return OK; }//ClearList Status ListEmpty(List *L){ if(L->length!=0) return FALSE; else return TRUE; }//ListEmpty Status ListLength(List *L){ return L->length; }//ListLength Status GetElem(List *L, int i, int *e){ int j; for(j=0; j<ListLength(L); j++) if(j==i-1){ e=&L->data[j]; break; } return *e; }//GetElem Status compare(List *a, int b){ int i, judge; for(i=0, judge=0; i<ListLength(a); i++){ if(a->data[i]==b){ judge=1; return TRUE; break; } } if(judge==0) return FALSE; }//compare Status LocateElem(List *L, int e, Status (*pfun)(List *ce, int num)){ int i; for(i=0; i<ListLength(L); i++){ if(L->data[i]==e){ return i+1; break; } } if(!pfun(L, e)) return 0; }//LocateElem Status PriorElem(List *L, int cur_e, int *pre_e){ int i, judge; for(i=0, judge=0; i<ListLength(L); i++){ if(i!=0&&cur_e==L->data[i]){ judge=1; pre_e=&L->data[i-1]; break; } } if(i==0||judge==0) return ERROR; else return *pre_e; }//PriorElem Status NextElem(List *L, int cur_e, int *next_e){ int i, judge; for(i=0, judge=0; i<ListLength(L); i++) if(i!=ListLength(L)-1&&cur_e==L->data[i]){ judge=1; next_e=&L->data[i+1]; break; } if(i==ListLength(L)-1||judge==0) return ERROR; else return *next_e; }//NextElem Status ListInsert(List *L, int i, int e){ int j; if(i<0) return ERROR; else{ for(j=ListLength(L); j>=i; j--) L->data[j]=L->data[j-1]; L->length++; L->data[j]=e; return OK; } } Status ListDelete(List *L, int i, int *e){ int j, temp; if(i<0||i>ListLength(L)) return ERROR; temp=L->data[i]; for(j=i; j<ListLength(L)-1; j++) L->data[j]=L->data[j+1]; L->length--; e=&temp; return *e; }//ListDelete Status visit(List *L){ int i, judge; for(i=0, judge=0; i<ListLength(L); i++){ if(L->data[i]!=NULL) judge=1; else{ judge=0; break; } } if(judge==0) return ERROR; else return OK; }//visit Status ListTraverse(List *L, Status (*pfun)(List *ce)){ if(pfun(L)) return TRUE; else return FALSE; }//ListTraverse void UnionList(List *La, List *Lb){ La->length=ListLength(La); Lb->length=ListLength(Lb); int i, re, *el=NULL; Status (*pfun)(List *ce, int num); for(i=0; i<Lb->length; i++){ re=GetElem(Lb, i+1, el); if(!LocateElem(La, re, compare)) printf("ListInsert Status: %d\n", ListInsert(La, La->length+1, re)); } }//UnionList int main() { List *Li=(List*)malloc(sizeof(List)); printf("InitList Li Status: %d\n", InitList(Li)); int i, n, *r=NULL; printf("Enter List Li Length: "); scanf("%d", &n); printf("Enter %d numbers: ", n); for(i=0; i<n; i++){ scanf("%d", &Li->data[i]); Li->length++; } printf("ListLength: %d\n", ListLength(Li)); printf("ListEmpty Status: %d\n", ListEmpty(Li)); printf("GetElem: %d\n", GetElem(Li, 3, r)); printf("LocateElem: %d\n", LocateElem(Li, 13, compare)); printf("PriorElem %d\n", PriorElem(Li, 16, r)); printf("NextElem: %d\n", NextElem(Li, 2, r)); printf("ListInsert Status: %d\n", ListInsert(Li, 1, 15)); output(Li); printf("ListDelete Status: %d\n", ListDelete(Li, 2, r)); output(Li); printf("ListTraverse Status: %d\n", ListTraverse(Li, visit)); List *Lb=(List*)malloc(sizeof(int)); printf("InitList Lb Status: %d\n", InitList(Lb)); int m; printf("Enter List Lb length: "); scanf("%d", &m); printf("Enter %d numbers: ", m); for(i=0; i<m; i++){ scanf("%d", &Lb->data[i]); Lb->length++; } UnionList(Li, Lb); output(Li); printf("ClearList Status: %d\n", ClearList(Li)); printf("ListEmpty Status: %d\n", ListEmpty(Li)); printf("DestroyList Status: %d\n", DestroyList(Li)); system("pause"); return 0; }

推荐阅读