2009-09-15 一、查找表 #include<stdio.h> #include<stdlib.h> #include<iostream.h> #define MAX 5 typedef int KeyType; typedef int ElemType; typedef struct{ //顺序结构数据类型 KeyType key; }SeqElemType; typedef struct{ SeqElemType r[MAX+1]; int length; }SSTable; typedef struct BiTNode{ //二叉排序树的结点结构 ElemType data; struct BiTNode *lchild,*rchild; }BiTNode,*BiTree; SSTable createTable(){ //建立线性表 SSTable ST; KeyType k; int n=1; ST.length=0; cout<<"请输入第1个数:"; cin>>k; while(k!=0){ ST.length++; ST.r[ST.length].key=k; cout<<"请输入第"<<++n<<"个数:"; cin>>k; } return ST; } void InsertBiT(BiTree &T,int x){ //插入元素 BiTNode *s; if(T==NULL) { s=(BiTNode*)malloc(sizeof(BiTNode)); s->data=x; s->lchild=NULL; s->rchild=NULL; T=s; } else if(x < T->data) InsertBiT(T->lchild,x); else if(x > T->data) InsertBiT(T->rchild,x); } BiTree createBiT(BiTree &T){ //创建二叉排序树 int x,i=1; T=NULL; cout<<"请输入二叉排序树的元素:"; cin>>x; while(x!=0) { InsertBiT(T,x); cin>>x; } return T; } void Search_Seq(SSTable ST,KeyType key){ //顺序查找 int i; cout<<"请输入要查找的关键字:"; cin>>key; ST.r[0].key=key; i=ST.length; while(ST.r[i].key!=key) i--; if(i==0) cout<<"------查找失败------"<<endl; else cout<<"要查找的关键字"<<key<<"在表中的位置为:"<<i<<endl; } void Search_Bin(SSTable ST,KeyType key){ //二分查找 int j=0,i=0; int low=1,high=ST.length,mid; cout<<"请输入要查找的关键字:"; cin>>key; while(low<=high) { mid=(low+high)/2; if(ST.r[mid].key==key){ i = mid; break; } else if(key<ST.r[mid].key) high=mid-1; else low=mid +1; } if(i!=0) cout<<"要查找的关键字"<<key<<"在表中的位置为:"<<i<<endl; else cout<<"------查找失败------"<<endl; } void SearchBiT(BiTree T,int x){ //二叉排序树的查找 if(!T) cout<<"元素 "<<x<<" ,查找失败"<<endl; else if(T->data==x) cout<<"元素 "<<x<<" ,查找成功"<<endl; else if(T->data > x) SearchBiT(T->lchild,x); else SearchBiT(T->rchild,x); } void main(){ SSTable ST1,ST2; KeyType key=0; BiTree T; cout<<"/n创建顺序查找表,输入0则结束(可不按顺序输入)"<<endl; ST1=createTable(); Search_Seq(ST1,key); cout<<"/n创建二分查找表,输入0则结束(按递增顺序输入)"<<endl; ST2=createTable(); Search_Bin(ST2,key); cout<<"/n创建二叉排序树,输入0则结束"<<endl; T=createBiT(T); SearchBiT(T,12); } 二、递归 #include<stdio.h> #include<iostream.h> int S(int a1, int d, int n){ if(n == 1) return a1; else return S(a1,d,n-1)+a1+(n-1)*d; } int max(int *a,int n){ if(n == 1) return a[0]; else{ int m = max(a,n-1); return m > a[n-1] ? m:a[n-1]; } } void rev(int *a, int n){ if(n > 1){ int t=a[0]; a[0]=a[n-1]; a[n-1]=t; rev(a+1,n-2); } } /* int cj(int *a, int n){ if(n == 1) return a[0]; else{ int c = cj(a,n-1); return c*a[n-1]; } } */ double ave(int *a, int n){ if(n == 1) return a[0]; else{ double k = (n-1)*ave(a,n-1); return (k+a[n-1])/n; } } int gys(int m, int n){ int r; if(m < n){ int t=m; m=n; n=t; } if((r=m%n) == 0) return n; else{ m=n; n=r; gys(m,n); } } void main(){ int a1, d, n1, n,a[50],u,v; cout<<"请输入等差数列首项a1: "; cin>>a1; cout<<"请输入等差数列公差d: "; cin>>d; cout<<"请输入等差数列项数n: "; cin>>n1; cout<<"前"<<n1<<"项和为:"<<S(a1,d,n1); cout<<"/n"<<endl; cout<<"请输入数组的元素个数n:"; cin>>n; cout<<"请输入数组中的元素: "; for(int i=0;i<n;i++){ cin>>a[i]; } cout<<"数组中的最大元素为:"<<max(a,n)<<"/n"<<endl; cout<<"将数组中元素倒序为:"; rev(a, n); for(int j=0;j<n;j++) cout<<a[j]<<" "; cout<<"/n"<<endl; //cout<<"数组中所有元素的乘积为:"<<cj(a,n)<<"/n"<<endl; cout<<"数组中元素的平均值为:"<<ave(a,n)<<"/n"<<endl; cout<<"请输入两个整数:"; cin>>u; cin>>v; cout<<u<<"与"<<v<<"的最大公约数为:"<<gys(u,v)<<endl; } 三、二叉树 #include<stdio.h> #include<stdlib.h> #include<iostream.h> #define MAX_TREE_SIZE 20 #define OVERFLOW -2 #define OK 1 #define ERROR 0 typedef char TElemType; typedef int Status; typedef struct BiTNode {//定义一个二叉树 TElemType data; struct BiTNode *lchild, *rchild; }BiTNode, *BiTree; typedef struct QNode {//定义链队列结点 BiTree data; struct QNode *next; }QNode,*QueuePtr; typedef struct {//定义链队列头尾指针 QueuePtr front; QueuePtr rear; }LinkQueue; Status InitQueue (LinkQueue &Q) { Q.front = Q.rear = (QueuePtr) malloc (sizeof(QNode)); if(!Q.front) exit(OVERFLOW); Q.front -> next = NULL; return OK; } Status EnQueue (LinkQueue &Q,BiTree e) { QueuePtr p; p = (QueuePtr) malloc (sizeof(QNode)); if(!p) exit (OVERFLOW); p -> data = e; p -> next = NULL; Q.rear -> next = p; Q.rear = p; return OK; } Status QueueEmpty (LinkQueue Q) { return (Q.front == Q.rear); } Status DeQueue (LinkQueue &Q, BiTree &e) { QueuePtr p; if(Q.front == Q.rear) return ERROR; p = Q.front -> next; e = p -> data; Q.front -> next = p -> next; if(Q.rear == p) Q.rear = Q.front; free(p); return OK; } Status DestroyQueue(LinkQueue &Q){ while(Q.front){ Q.rear=Q.front->next; free(Q.front); Q.front = Q.rear; } return OK; } Status CreateBiTree(BiTree &T){//建立一个二叉树 char ch; ch = getchar(); if(ch == ' ') T = NULL; else{ if(!(T=(BiTree)malloc(sizeof(BiTNode)))) exit(OVERFLOW); T->data = ch; CreateBiTree(T->lchild); CreateBiTree(T->rchild); } return OK; } void PreOrderTraverse(BiTree T){//先序遍历递归形式 if(T){ cout<<T->data; PreOrderTraverse(T->lchild); PreOrderTraverse(T->rchild); } } void InOrderTraverse(BiTree T){//中序遍历递归形式 if(T){ InOrderTraverse(T->lchild); cout<<T->data; InOrderTraverse(T->rchild); } } void PostOrderTraverse(BiTree T){//后序遍历递归形式 if(T){ PostOrderTraverse(T->lchild); PostOrderTraverse(T->rchild); cout<<T->data; } } Status LevelTraverseBiTree(BiTree T){//层序遍历 LinkQueue Q; InitQueue(Q); EnQueue(Q,T); while(!QueueEmpty(Q)){ DeQueue(Q,T); cout<<T->data; if(T->lchild) EnQueue(Q,T->lchild); if(T->rchild) EnQueue(Q,T->rchild); } DestroyQueue(Q); return OK; } /* void LevelTraverseBiTree(BiTree T){//层序遍历 BiTree Q[MAX_TREE_SIZE]; int front=0,rear=0; BiTree p; if(T){ //根结点入队 Q[rear]=T; rear=(rear+1)%MAX_TREE_SIZE; } while(front!=rear){ p=Q[front]; //队头元素出队 front=(front+1)%MAX_TREE_SIZE; cout<<p->data; if(p->lchild){ //左孩子不为空,入队 Q[rear]=p->lchild; rear=(rear+1)%MAX_TREE_SIZE; } if(p->rchild){ //右孩子不为空,入队 Q[rear]=p->rchild; rear=(rear+1)%MAX_TREE_SIZE; } } } */ Status Nodes(BiTree T){//求所有的结点数 if(!T) return 0; else return 1+Nodes(T->lchild)+Nodes(T->rchild); } Status Ls(BiTree T){//叶子结点的个数 if(!T) return 0; else if(T->lchild==0 && T->rchild==0) return 1; else return Ls(T->lchild)+Ls(T->rchild); } Status TreeDepth(BiTree T){//树的深度 if(!T) return 0; else return (TreeDepth(T->lchild) > TreeDepth(T->rchild) ? TreeDepth(T->lchild) : TreeDepth(T->rchild)) + 1; } Status Brother(BiTree T){//双胞胎数 if(!T) return 0; else{ if(T->lchild && T->rchild) return Brother(T->lchild)+Brother(T->rchild)+1; else return Brother(T->lchild)+Brother(T->rchild); } } Status OnlyChild(BiTree T){//独生子女数 return Nodes(T)-Ls(T)-Brother(T); } void main(){ BiTree T; CreateBiTree(T); cout<<"先序遍历为:"; PreOrderTraverse(T); cout<<endl; cout<<"中序遍历为:"; InOrderTraverse(T); cout<<endl; cout<<"后序遍历为:"; PostOrderTraverse(T); cout<<endl; cout<<"层序遍历为:"; LevelTraverseBiTree(T); cout<<endl; cout<<"结点数为:"<<Nodes(T)<<endl; cout<<"叶子数为:"<<Ls(T)<<endl; cout<<"深度为:"<<TreeDepth(T)<<endl; cout<<"双胞胎数为:"<<Brother(T)<<endl; cout<<"独生子女数为:"<<OnlyChild(T)<<endl; } 四、选择 #include<stdio.h> #include<iostream.h> void Select(int *a,int n,int &s1,int &s2){ s1=0; s2=1; if(a[s1]-a[s2]>0){ int temp; temp=s1; s1=s2; s2=temp; } for(int i=0;i<n;i++){ //遍历数组,寻找最小的两个结点 if(a[i]<a[s1]){ s2=s1; s1=i; } if(a[i]<a[s2] && i!=s1) s2=i; } } void main(){ int a[50]; int n; int s1,s2; cout<<"请输入权值的个数n:"; cin>>n; cout<<"请输入权值:"; for(int i=0;i<n;i++){ cin>>a[i]; } Select(a,n,s1,s2); cout<<"s1="<<s1+1<<", "<<"s2="<<s2+1<<endl; } 五、哈弗曼 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream.h> typedef struct{ unsigned int weight; unsigned int parent,lchild,rchild; }HTNode, *HuffmanTree;//动态分配数组存储哈弗曼树 typedef char * *HuffmanCode;//动态分配数组存储哈弗曼码表 void Select(HuffmanTree HT,int n,int &s1,int &s2){ //从哈夫曼树中选择出最小的两个节点 for(int i=1;i<=n;i++) if(!HT[i].parent){ s1=i; break; } for(i++;i<=n;i++) if(!HT[i].parent){ s2=i; break; } if(HT[s1].weight-HT[s2].weight){ int temp; temp=s1; s1=s2; s2=temp; } for(i=1;i<=n;i++) //对数组进行遍历,寻找最小的两个节点 if(!HT[i].parent){ if(HT[i].weight<HT[s1].weight){ s2=s1; s1=i; } else if(HT[i].weight<HT[s2].weight&&i!=s1) s2=i; } } void HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n){ //w存放n个字符的权值(均>0),构造哈弗曼树HT,并求出n个字符的哈弗曼编码HC HuffmanTree p; if(n<=1) return; int m = 2*n - 1;//总结点数 int s1,s2,i; HT = (HuffmanTree)malloc((m+1) * sizeof(HTNode)); //分配存储空间,0号单元未用 for(p=HT+1,i=1;i<=n;i++,p++){ p->weight=w[i-1]; p->parent=0; p->lchild=0; p->rchild=0; } for(;i<=m;i++,p++){ p->weight=0; p->parent=0; p->lchild=0; p->rchild=0; } for(i=n+1;i<=m;i++){ Select(HT,i-1,s1,s2); HT[s1].parent = i; HT[s2].parent = i; HT[i].lchild = s1; HT[i].rchild = s2; HT[i].weight = HT[s1].weight + HT[s2].weight; } //---从叶子到根逆向求每个字符的哈弗曼编码--- HC = (HuffmanCode)malloc((n+1)*sizeof(char *));//分配n个字符编码的头指针向量 char *cd = (char *)malloc(n*sizeof(char));//分配求编码的工作空间 cd[n-1] = '/0'; for(i=1;i<=n;i++){ int start = n-1; for(int c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent)//从叶子到根逆向求编码 if(HT[f].lchild == c) cd[--start] = '0'; else cd[--start] = '1'; HC[i] = (char *)malloc((n-start) * sizeof(char));//为第i个字符编码分配空间 strcpy(HC[i],&cd[start]);//从cd复制编码(串)到HC } free(cd); } void output (HuffmanTree HT,int n){ //输出哈夫曼树的存储结构 int i=1; cout<<"序号"<<" "<<"双亲"<<" "<<"左孩子"<<" " <<"右孩子"<<" "<<"权值"<<endl; for(;i<=2*n-1;i++){ HT++; cout<<" "<<i<<" "<<HT->parent<<" "<< HT->lchild<<" "<<HT->rchild<<" "<<HT->weight <<endl; } } void main(){ HuffmanTree HT=NULL; HuffmanCode HC=NULL; int n; int w[50]; cout<<"请输入需要编码的字符总数:"; cin>>n; cout<<"请输入需要编码的字符的权值:"; for(int a=0;a<n;a++){ cin>>w[a]; } HuffmanCoding(HT,HC,w,n); cout<<endl; output(HT,n); cout<<endl; } 六、链表 #include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<iostream.h> #define OK 1 #define ERROR 0 #define OVERFLOW 2 typedef int ElemType; typedef int Status; typedef struct LNode{ ElemType data; struct LNode *next; }LNode,*LinkList; Status CreateList_L(LinkList &L,int n){ LNode *p; L=(LinkList)malloc(sizeof(LNode)); L->next=NULL; for(int i=n;i>0;--i){ p=(LinkList)malloc(sizeof(LNode)); cin>>p->data; p->next=L->next; L->next=p; } return OK; } Status GetElem_L(LinkList L,int i,ElemType &e){ LNode *p; p=L->next; int j=1; while(p && j<i){ p=p->next; ++j; } if(!p||j>i) return ERROR; e=p->data; return e; } Status ListInsert_L(LinkList &L,int i,ElemType e){ LNode *p,*s; p=L; int j=0; while(p && j<i-1){ p=p->next; ++j; } if(!p||j>i-1) return ERROR; s=(LinkList)malloc(sizeof(LNode)); s->data=e; s->next=p->next; p->next=s; return OK; } Status LinkListLength_L(LinkList L){ LNode *p; int count=0; p=L->next; while(p){ count++; p=p->next; } return count; } int equal(ElemType e1,ElemType e2){ return (e1==e2); } int LocateElem_L(LinkList &L,ElemType e,Status(*equal)(ElemType,ElemType)){ LNode *p; p=L->next; for(int j=1;j<=LinkListLength_L(L);j++){ if((equal)(p->data,e)) return j; else p=p->next; } return ERROR; } void ListTraverse_L(LinkList L) { LNode *p; p=L->next; for(int i=0;i<LinkListLength_L(L);i++) { cout<<p->data<<" "; p=p->next; } cout<<endl; } void union_L(LinkList &La,LinkList &Lb){ ElemType e; int La_len=LinkListLength_L(La); int Lb_len=LinkListLength_L(Lb); for(int i=1;i<=Lb_len;i++){ GetElem_L(Lb,i,e); if(!LocateElem_L(La,e,equal)) ListInsert_L(La,++La_len,e); } } void MergeList_L(LinkList &La,LinkList &Lb,LinkList &Lc){ LNode *pa,*pb,*pc; pa=La->next; pb=Lb->next; Lc=pc=La; while(pa && pb){ if(pa->data<=pb->data){ pc->next=pa; pc=pa; pa=pa->next; } else{ pc->next=pb; pc=pb; pb=pb->next; } } pc->next=pa?pa:pb; free(Lb); } void main(){ LinkList La,Lb,Lc; ElemType e,b; int i=0,n1=0,n2=0; if(CreateList_L(La,n1)) cout<<"创建成功!"<<endl; else cout<<"创建失败!"<<endl; cout<<"请输入链表La的长度n:"; cin>>n1; cout<<"请输入链表La中的元素:"; CreateList_L(La,n1); cout<<"所创建的链表La为:"<<endl; ListTraverse_L(La); cout<<"/n"; if(CreateList_L(Lb,n2)) cout<<"创建成功!"<<endl; else cout<<"创建失败!"<<endl; cout<<"请输入链表Lb的长度n:"; cin>>n2; cout<<"请输入链表Lb中的元素:"; CreateList_L(Lb,n2); cout<<"所创建的链表Lb为:"<<endl; ListTraverse_L(Lb); cout<<"/n"; GetElem_L(La,i,e); cout<<"输入你要获得的La中元素的位置i:"; cin>>i; cout<<"您获得元素e="<<GetElem_L(La,i,e)<<endl; cout<<"/n"<<endl; cout<<"请输入您所要插入La中的元素的位置i:"; cin>>i; cout<<"请输入您所要插入La中的元素e:"; cin>>e; ListInsert_L(La,i,e); cout<<"现在的链表La为:"<<endl; ListTraverse_L(La); cout<<"/n"<<endl; cout<<"请输入您想要在表La中检查的数字e:"; cin>>b; if(LocateElem_L(La,b,equal)) cout<<"您检查的数字"<<b<<"在表La中的位置为 i="<<LocateElem_L(La,b,equal)<<endl; else cout<<"您检查的数字"<<b<<"在表La中不存在,返回"<<LocateElem_L(La,b,equal)<<endl; cout<<"/n"; cout<<"将表Lb合并到表La中为:"<<endl; union_L(La,Lb); ListTraverse_L(La); cout<<"/n"; cout<<"合并表La与表Lb所得新表Lc为:"<<endl; MergeList_L(La,Lb,Lc); ListTraverse_L(Lc); cout<<endl; } 七、三元组 #include<iostream.h> #include<stdio.h> #include<stdlib.h> #define OK 1 #define OVERFLOW -2 #define ERROR 0 typedef int Status; typedef int ElemType; typedef ElemType *Triplet ; Status InitTriplet ( Triplet &T,ElemType v1,ElemType v2,ElemType v3); Status InitTriplet (Triplet &T,ElemType v1,ElemType v2,ElemType v3){ T=(ElemType*)malloc(3*sizeof(ElemType)); if(!T) exit(OVERFLOW); T[0]=v1; T[1]=v2; T[2]=v3; return OK; } Status Get(Triplet T,ElemType i,ElemType &e){ if(i<1||i>3) return ERROR; e=T[i-1]; return OK; } Status Put(Triplet &T,ElemType i,ElemType e){ if(i<1||i>3) return ERROR; T[i-1]=e; return OK; } Status IsAscending(Triplet T){ return (T[0]<=T[1])&&(T[1]<=T[2]); } Status IsDescending(Triplet T){ return (T[0]>=T[1])&&(T[1]>=T[2]); } Status Max(Triplet T){ return (T[0]>=T[1])?((T[0]>=T[2])?T[0]:T[2]):((T[1]>=T[2])?T[1]:T[2]); } Status Min(Triplet T){ return (T[0]<=T[1])?((T[0]<=T[2])?T[0]:T[2]):((T[1]<=T[2])?T[1]:T[2]); } Status DestroyTriplet(Triplet &T){ free(T); T=NULL; return OK; } void main() { Triplet T1; ElemType j,a,b,c; ElemType i,e; cout<<"/n初始化三元组,请输入三元组的三个元素:"; cin>>a>>b>>c; cout<<"创建成功返回1,失败返回-2 /t结果返回"<<InitTriplet (T1,a,b,c)<<endl; cout<<"创建的三元组为:"<<endl; for(j=0;j <3;j++) cout<<"/t"<<T1[j]<<endl; Get(T1,i,e); cout<<"/n请输入要获得元素的位置i:"; cin>>i; cout<<"i输入合法返回1,不合法返回0 /t结果返回"<<Get(T1,i,e)<<endl; cout<<"获得的第"<<i<<"个元素:e="<<e<<endl; cout<<"/n请输入要替换数据的位置 i:"; cin>>i; cout<<"i输入合法返回1,不合法返回0 /t结果返回"<<Put(T1,i,e)<<endl; cout<<"请输入要替换的数字 e:"; cin>>e; Put(T1,i,e); cout<<"T的第"<<i<<"个元素换为了"<<e<<endl; cout<<"替换后的三元组为:"<<endl; for(j=0;j<3;j++) cout<<"/t"<<T1[j]<<endl; IsAscending(T1); cout<<"/n若3个元素按升序排列,返回1,否则返回0 /t结果返回"<<IsAscending(T1)<<endl; IsDescending(T1); cout<<"/n若3个元素按降序排列,返回1,否则返回0 /t结果返回"<<IsDescending(T1)<<endl; Max(T1); cout<<"/n三个数中最大的数是:"<<Max(T1)<<endl; Min(T1); cout<<"/n三个数中最小的数是:"<<Min(T1)<<endl; DestroyTriplet(T1); cout<<"/n三元组已成功销毁,返回"<<DestroyTriplet(T1)<<endl; } 八、顺序表 #include<iostream.h> #include<stdio.h> #include<stdlib.h> #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 #define OK 1 #define OVERFLOW -2 #define ERROR 0 typedef int Status; typedef int ElemType; typedef struct{ ElemType *elem; int length; int listsize; }SqList; Status InitList_Sq(SqList &L){ L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); L.length=0; L.listsize=LIST_INIT_SIZE; return OK; } int GetElem_Sq(SqList L,int i,ElemType &e){ if(i<1||i>L.length) return ERROR; e=L.elem[i-1]; return e; } int equal(ElemType e1,ElemType e2){ return (e1==e2); } int LocateElem_Sq(SqList L,ElemType e,Status(*compare)(ElemType,ElemType)){ int i=1; while(i<=L.length ) if(compare(L.elem[i-1],e)) return i; else ++i; return 0; } Status ListInsert_Sq(SqList &L,int i,ElemType e){ ElemType *q; ElemType *p; ElemType *newbase; if(i<1||i>L.length+1) return ERROR; if(L.length>=L.listsize){ newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType)); if(!newbase) exit(OVERFLOW); L.elem=newbase; L.listsize += LISTINCREMENT; } q=&(L.elem[i-1]); for(p=&(L.elem[L.length-1]);p>=q;--p) *(p+1)=*p; *q=e; ++L.length; return OK; } int ListLength_Sq(SqList L){ return L.length; } void MergeList_Sq(SqList La, SqList Lb, SqList &Lc){ InitList_Sq(Lc); int i=1,j=1; int k=0; ElemType ai,bj; int La_len,Lb_len; La_len=ListLength_Sq(La); Lb_len=ListLength_Sq(Lb); while((i<=La_len)&&(j<=Lb_len)){ GetElem_Sq(La,i,ai); GetElem_Sq(Lb,j,bj); if(ai<=bj){ ListInsert_Sq(Lc,++k,ai); ++i; } else{ ListInsert_Sq(Lc,++k,bj); ++j; } } while(i<=La_len){ GetElem_Sq(La,i++,ai); ListInsert_Sq(Lc,++k,ai); } while(j<=Lb_len){ GetElem_Sq(Lb,j++,bj); ListInsert_Sq(Lc,++k,bj); } } void union_Sq(SqList &La,SqList &Lb){ int La_len; int Lb_len; ElemType e; La_len=ListLength_Sq(La); Lb_len=ListLength_Sq(Lb); for(int i=1;i<=Lb_len;i++){ GetElem_Sq(Lb,i,e); if(!LocateElem_Sq(La,e,equal)) ListInsert_Sq(La,++La_len,e); } } void main(){ SqList La; SqList Lb; SqList Lc; int a,b,e; InitList_Sq(La); if(!La.elem) cout<<"La初始化失败!"<<endl; else cout<<"返回 "<<OK<<" La初始化成功!"<<endl; cout<<"表La中元素为:"<<endl; ListInsert_Sq(La,1,3); ListInsert_Sq(La,2,5); ListInsert_Sq(La,3,8); ListInsert_Sq(La,4,11); for(int i=0;i<La.length;i++){ cout<<La.elem[i]<<" "; } cout<<"/n"; InitList_Sq(Lb); if(!La.elem) cout<<"/n"<<"Lb初始化失败!"<<endl; else cout<<"/n"<<"返回 "<<OK<<" Lb初始化成功!"<<endl; cout<<"表Lb中元素为:"<<endl; ListInsert_Sq(Lb,1,2); ListInsert_Sq(Lb,2,6); ListInsert_Sq(Lb,3,8); ListInsert_Sq(Lb,4,9); ListInsert_Sq(Lb,5,11); ListInsert_Sq(Lb,6,15); ListInsert_Sq(Lb,7,20); for(int j=0;j<Lb.length;j++){ cout<<Lb.elem[j]<<" "; } cout<<"/n"; cout<<"/n请输入您想要获得表La中元素的位置i:"; cin>>a; cout<<"您所获得的表La中的第"<<a<<"个元素为e="<<GetElem_Sq(La,a,e)<<endl; cout<<"/n"; cout<<"/n请输入您想要获得表Lb中元素的位置i:"; cin>>a; cout<<"您所获得的表Lb中的第"<<a<<"个元素为e="<<GetElem_Sq(Lb,a,e)<<endl; cout<<"/n"; cout<<"请输入您想要在表La中检查的数字e:"; cin>>b; if(LocateElem_Sq(La,b,equal)) cout<<"您检查的数字"<<b<<"在表La中的位置为:"<<LocateElem_Sq(La,b,equal)<<endl; else cout<<"您检查的数字"<<b<<"在表La中不存在,返回"<<LocateElem_Sq(La,b,equal)<<endl; cout<<"/n"; cout<<"请输入您想要在表Lb中检查的数字e:"; cin>>b; if(LocateElem_Sq(Lb,b,equal)) cout<<"您检查的数字"<<b<<"在表Lb中的位置为:"<<LocateElem_Sq(Lb,b,equal)<<endl; else cout<<"您检查的数字"<<b<<"在表Lb中不存在,返回"<<LocateElem_Sq(Lb,b,equal)<<endl; cout<<"/n"; cout<<"请输入您想要在表La中插入的位置i,以及插入的元素e"<<endl; cin>>a; cin>>b; ListInsert_Sq(La,a,b); cout<<"插入元素后的表La为:"; for(int k=0;k<La.length;k++) cout<<" "<<La.elem[k]; cout<<"/n"<<endl; cout<<"请输入您想要在表Lb中插入的位置i,以及插入的元素e"<<endl; cin>>a; cin>>b; ListInsert_Sq(Lb,a,b); cout<<"插入元素后的表Lb为:"; for(int l=0;l<Lb.length;l++) cout<<" "<<Lb.elem[l]; cout<<"/n"<<endl; MergeList_Sq(La,Lb,Lc); cout<<"表La与表Lb合并的表Lc为:"<<endl; for(int n=0;n<Lc.length;n++){ cout<<" "<<Lc.elem[n]; } cout<<"/n"<<endl; cout<<"将表Lb并到表La中为:"<<endl; union_Sq(La,Lb); for(int m=0;m<La.length;m++){ cout<<" "<<La.elem[m]; } cout<<endl; } 九、栈与队列 #include<stdio.h> #include<stdlib.h> #include<iostream.h> #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 #define OVERFLOW -2 #define OK 1 #define ERROR 0 typedef int ElemType; typedef struct{ ElemType *base; ElemType *top; int stacksize; }SqStack; typedef int Status; typedef struct QNode { ElemType data; struct QNode *next; }QNode,*QueuePtr; typedef struct { QueuePtr front; QueuePtr rear; }LinkQueue; Status InitStack(SqStack &S){ S.base=(ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); if(!S.base) exit(OVERFLOW); S.top=S.base; S.stacksize=STACK_INIT_SIZE; return OK; } Status InitQueue (LinkQueue &Q) { Q.front = Q.rear = (QueuePtr) malloc (sizeof(QNode)); if(!Q.front) exit(OVERFLOW); Q.front -> next = NULL; return OK; } Status Push(SqStack &S,ElemType e){ if(S.top-S.base==S.stacksize){ S.base=(ElemType *)realloc(S.base,(S.stacksize + STACKINCREMENT) * sizeof(ElemType)); if(!S.base) exit(OVERFLOW); S.top=S.base + S.stacksize; S.stacksize += STACKINCREMENT; } *S.top++=e; return OK; } Status Pop(SqStack &S, ElemType &e){ if(S.top==S.base) return ERROR; e=* --S.top; return OK; } Status StackEmpty(SqStack S){ return S.base==S.top; } Status EnQueue (LinkQueue &Q,ElemType e) { QueuePtr p; p = (QueuePtr) malloc (sizeof(QNode)); if(!p) exit (OVERFLOW); p -> data = e; p -> next = NULL; Q.rear -> next = p; Q.rear = p; return OK; } Status DeQueue (LinkQueue &Q, ElemType &e) { QueuePtr p; if(Q.front == Q.rear) return ERROR; p = Q.front -> next; e = p -> data; Q.front -> next = p -> next; if(Q.rear == p) Q.rear = Q.front; free(p); return OK; } Status QueueEmpty (LinkQueue Q) { return (Q.front == Q.rear); } Status QueueLength (LinkQueue Q){ QueuePtr p; int count = 0; p = Q.front -> next; while(p){ count ++; p = p -> next; } return count; } Status QueueTraverse (LinkQueue Q) { QueuePtr p; p = Q.front -> next; for(int i=0;i<QueueLength(Q);i++) { cout<<p -> data<<" "; p = p -> next; } return OK; } Status algo1(SqStack &S,int m){ ElemType e; for(int i=0;i<m;i++){ cin>>e; Push(S,e); } cout<<"逆序输出整数是: "; while(! StackEmpty(S)){ Pop(S,e); cout<<e<<" "; }; cout<<endl; return OK; } void conversion(SqStack &S,int n){ InitStack(S); ElemType e; while(n){ Push(S,n % 16); n = n/16; } while(!StackEmpty(S)){ Pop(S,e); switch(e){ case 10: cout<<'A'; break; case 11: cout<<'B'; break; case 12: cout<<'C'; break; case 13: cout<<'D'; break; case 14: cout<<'E'; break; default: cout<<e; break; } } } void algo2 (LinkQueue Q){ SqStack S; int d; InitStack(S); while(!QueueEmpty(Q)){ DeQueue(Q,d); Push(S,d); } cout<<"将队列中的元素逆置为: "; while(!StackEmpty(S)){ Pop(S,d); cout<<d<<" "; } } Status alog3 (SqStack &S, LinkQueue &Q){ InitStack(S); InitQueue(Q); ElemType e,e1,e2; cin>>e; while(e != 0){ Push(S,e); EnQueue(Q,e); cin>>e; } while(!StackEmpty(S) && !QueueEmpty(Q)) { Pop(S,e1); DeQueue(Q,e2); if(e1!=e2) return ERROR; } return OK; } void main(){ SqStack S; LinkQueue Q; InitStack(S); InitQueue(Q); int m=0,n=0,l=0; cout<<"请输入整数长度m: "; cin>>m; cout<<"请输入整数: "; algo1(S,m); cout<<"/n"<<endl; cout<<"请输入要转换为16进制的整数: "; cin>>n; cout<<"转换为16进制数为: "; conversion(S,n); cout<<"/n"<<endl; EnQueue (Q,9); EnQueue (Q,8); EnQueue (Q,7); EnQueue (Q,6); EnQueue (Q,5); EnQueue (Q,4); EnQueue (Q,3); EnQueue (Q,2); EnQueue (Q,1); EnQueue (Q,0); cout<<"队列中的元素为: "; QueueTraverse (Q); cout<<endl; algo2 (Q); cout<<"/n"<<endl; cout<<"请输入整数回文,并以0作为结束符 : "; if(alog3 (S,Q)) cout<<"----该整数序列是回文----"<<endl; else cout<<"----该整数序列不是回文----"<<endl; }