合并两个线性表
2011-08-30 17:45 javaspring 阅读(546) 评论(0) 编辑 收藏 举报
严蔚敏版数据结构,算法2.1
#include <iostream> using namespace std; typedef int element; #define maxsize 100 typedef struct { element aa[maxsize]; int last; }sqlist; int compare(sqlist *la,int x) { int flag=0; int i; for(i=0;i<=la->last;++i) { if(la->aa[i]==x) {flag=1;break;} } return flag; } void uunion(sqlist *la,sqlist *lb) { int x=la->last; int y=0,i,count=0; for(i=0;i<=lb->last;++i) { if(!compare(la,lb->aa[i])) { la->aa[++x]=lb->aa[i]; count++; } } la->last=la->last+count; } int main() { sqlist *la,*lb; int len,i; la=(sqlist*)malloc(sizeof(sqlist)); cout<<"输入链表a的长度"<<endl; cin>>len; la->last=len-1; cout<<"输入元素"<<endl; for(i=0;i<len;++i) cin>>la->aa[i]; lb=(sqlist*)malloc(sizeof(sqlist)); cout<<"输入链表b的长度"<<endl; cin>>len; lb->last=len-1; cout<<"输入元素"<<endl; for(i=0;i<len;++i) cin>>lb->aa[i]; uunion(la,lb); for(i=0;i<=la->last;++i) cout<<la->aa[i]<<" "; cout<<endl; return 0; }