《C++数据结构实例代码(15页).doc》由会员分享,可在线阅读,更多相关《C++数据结构实例代码(15页).doc(15页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-C+数据结构实例代码-第 15 页1 线性表的顺序表示#include iostream#include using namespace std;typedef structint *elem;int length;int listsize;SqList;int Init_Sq(SqList &L)=(int *)malloc(100*sizeof(int);if(!)exit(-2);=0;=100;return 1;int ListInsert(SqList &L,int i,int e)if(iL.length+1)return 0;if(=)int *newbase=(int *)r
2、ealloc(,(L.listsize+10)*sizeof(int);if(!newbase)exit(-2);=newbase;+=10;int *q=&(i-1);int *p=&(L.length-1);for(p;p=q;-p)*(p+1)=*p;*q=e;return 1;int ListDelete(SqList &L,int i,int &e)if(i)return 0;int *p=&(i-1);e=*p;int *q=L.elem+L.length-1;for(+p;p=q;-p)*(p+1)=*p;*q=3;for(int i=0;i6;i+)coutai ;couten
3、dl;SqList lx;Init_Sq(lx);for(int j=1;j10;j+)ListInsert(lx,j,j);ListInsert(lx,3,55);int e_return;ListDelete(lx,4,e_return);for(int m=0;m10;m+)cout*(lx.elem+m) ;coutendl;coutnext=NULL;return 1;int ListInsert(LinkList &L,int i,int e)LinkList p=L;int j=0;while(p&jnext;+j;if(!p|ji-1)return 0;LinkList s=(
4、LinkList)malloc(sizeof(LNode);s-data=e;s-next=p-next;p-next=s;return 1;int ListDelete(LinkList &L,int i)LinkList p=L;int j=0;while(p-next&jnext;+j;if(!(p-next)|ji-1)return 0;LinkList q=p-next;p-next=q-next;free(q);return 1;int GetElem(LinkList L,int i)LinkList p=L-next;int j=1;while(p&jnext;+j;if(!p
5、|jdata;return e;int main()LinkList lx;InitList(lx);for(int i=1;i6;i+)ListInsert(lx,i,i);ListDelete(lx,2);for(int j=1;j5;j+)coutGetElem(lx,j) ;coutendl;LinkList lx1,lx2;InitList(lx1);InitList(lx2);for(int m=1;m6;m+)ListInsert(lx1,m,m);for(int n=1;n6;n+)ListInsert(lx2,n,2*n);for(int j=1;j6;j+)coutGetE
6、lem(lx1,j)next=L;L-prior=L;int ListInsert(DLinkList &L,int i,int e)DLinkList p,s;p=L-next;int j=1;while(p&jnext;+j;if(!p|ji)return 0;s=(DLinkList)malloc(sizeof(DList);s-data=e;s-prior=p-prior;p-prior-next=s;s-next=p;p-prior=s;return 1;int ListDelete(DLinkList &L,int i,int &e)DLinkList p;p=L-next;int
7、 j=1;while(p&jnext;+j;if(!p|ji)return 0;e=p-data;p-prior-next=p-next;p-next-prior=p-prior;free(p);return 1;int GetElem(DLinkList &L,int i)DLinkList p,s;p=L-next;int j=1;while(p&jnext;+j;if(!p|ji)return 0;int e=p-data;return e;int main()DLinkList lx;InitList(lx);for(int i=1;i6;i+)ListInsert(lx,i,2*i-
8、1);for(int j=1;j6;j+)coutGetElem(lx,j) ;coutendl;int e;ListDelete(lx,2,e);couteendl;for(int j=1;j5;j+)coutGetElem(lx,j)=)=(int *)realloc(,(S.stacksize+STACKINCREMENT)*sizeof(int);if(!)exit(-2);+=STACKINCREMENT;*+=e;int Pop(Stack &S,int &e)if(=)return 0;e=*-;return 1;int main()Stack lx;InitStack(lx);
9、for(int i=1;i6;i+)Push(lx,i);int e;GetTop(lx,e);couteendl;int e1;Pop(lx,e1);coute1endl;GetTop(lx,e1);coute1next=NULL;return 1;int DestroyQueue(LinkQueue &Q)while()=-next;free();return 1;int Insert(LinkQueue &Q,int e)QueuePtr p=(QueuePtr)malloc(sizeof(QNode);if(!p)exit(-2);p-data=e;p-next=NULL;-next=
10、p;=p;return 1;int GetFront(LinkQueue &Q,int &e)if(=)return 0;e=-next-data;return 1;int GetRear(LinkQueue &Q,int &e)if(=)return 0;e=-data;return 1;int Delete(LinkQueue &Q,int &e)if(=)return 0;QueuePtr p;p=-next; e=p-data;-next=p-next;if(=p)free(p);return 1;int main()LinkQueue lx;InitQueue(lx);for(int
11、 i=1;i6;i+)Insert(lx,i);int front;GetFront(lx,front);coutfrontendl;int rear;GetRear(lx,rear);coutrearendl;int e;Delete(lx,e);couteendl;GetFront(lx,front);coutfrontendl;system(pause);return 0;1512请按任意键继续. . .6 循环队列 #include iostream#include using namespace std;#define MAXSIZE 100typedef structint *ba
12、se;int front;int rear;SqQueue;int Init(SqQueue &Q)=(int *)malloc(MAXSIZE*sizeof(int);if(!)exit(-2);=0;return 1;int QueueLength(SqQueue &Q)return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;int EnQueue(SqQueue &Q,int e)if(Q.rear+1)%MAXSIZE=)return 0;=e;=(Q.rear+1)%MAXSIZE;return 1;int DeQueue(SqQueue &Q,int &e)
13、if(=)return 0;e=;=(Q.front+1)%MAXSIZE;return 1;int GetFront(SqQueue &Q)int e=;return e;int Getrear(SqQueue &Q)int e=Q.rear-1;return e;int main()SqQueue lx;Init(lx);for(int i=1;i6;i+)EnQueue(lx,i);coutQueueLength(lx)endl;coutGetFront(lx)endl;coutGetrear(lx)endl;int m;DeQueue(lx,m);coutmendl;coutGetFr
14、ont(lx)endl;system(pause);return 0;51512请按任意键继续. . .7 顺序表字符串#include iostream#include using namespace std;#define OK 1#define ERROR 0typedef structchar *ch;int length;HString;int StrAssign(HString &T,char *chars)=(char *)malloc(sizeof(char);if(T.ch)free(T.ch);/int i=strlen(chars);int i=0;char *c;for
15、(c=chars;*c;+i,+c);/判D断?条?件t为a*c!=0if(!i)T.ch=NULL;=0;=(char*)malloc(i*sizeof(char);for(int j=0;ji;j+)T.chj=charsj;=i;return OK;0int StrLength(HString &S)return ;int Concat(HString &T,HString &S1,HString &S2)=(char *)malloc(S1.length+S2.length)*sizeof(char);for(int i=0;iS1.length;i+)T.chi=S1.chi;=S1
16、.length+S2.length;for(int j=0;jS2.length;j+)T.chS1.length+j=S2.chj;return OK;int SubString(HString &Sub,HString &S,int pos,int len)if(pos|lenS.length-pos+1)return ERROR;=len;=(char *)malloc(len*sizeof(char);for(int i=0;ilen;i+)Sub.chi=S.chpos+i;return OK;int Print(HString &T)for(int i=0;iT.length;i+
17、)coutT.chi ;coutendl;return OK;int main()HString lx,hhc;StrAssign(lx,huanhuncao);StrAssign(hhc,lixing);Print(lx);Print(hhc);coutStrLength(lx)endl;coutStrLength(hhc)endl;HString lx1;=(char *)malloc()*sizeof(char);Concat(lx1,lx,hhc);Print(lx1);HString lx2;SubString(lx2,lx1,2,3);Print(lx2);system(pause
18、);return 0;h u a n h u n c a ol i x i n g106h u a n h u n c a o l i x i n ga n h请按任意键继续. . .8 链式字符串就是线性表的链式表示一样9 数组的表示 #include iostream#include /提供宏va_start#include using namespace std;#define MAX_ARRAY_DIM 8#define OK 1#define ERROR 0typedef structint *base;int dim;int *bounds;int *constants;int e
19、lemtotal;Array;int InitArray(Array &A,int dim,.)if(dimMAX_ARRAY_DIM)return ERROR;=dim;cout数组维数是:endl;=(int *)malloc(dim*sizeof(int);if(!)exit(-2);int elemtotal=1;va_list ap;va_start(ap,dim);cout数组每维元素数目分别是: ;for(int i=0;idim;i+)i=va_arg(ap,int);couti ;if(i0)return -2;elemtotal *=i;coutendl;cout数组元素数
20、目是:elemtotal=0;-i)i=i+1*i+1;return OK;int DestroyArray(Array &A)if(!)return ERROR;free();=NULL;if(!)return ERROR;free();=NULL;if(!)return ERROR;=NULL;return OK;int Locate(Array &A,va_list ap,int &off)off=0;int ind=0;for(int i=0;iA.dim;i+)ind=va_arg(ap,int);if(ind=i)return -1;off+=i*ind;return OK;int
21、 returnLocate(Array &A,int any,.)int off=0;va_list ap;va_start(ap,any);int ind=0;for(int i=0;iA.dim;i+)ind=va_arg(ap,int);if(ind=i)return -1;off+=i*ind;va_end(ap);return off;int Value(Array &A,int any,.)int e;int off=0;va_list ap;va_start(ap,any);int ind=0;for(int i=0;iA.dim;i+)ind=va_arg(ap,int);if
22、(ind=i)return -1;off+=i*ind;va_end(ap);e=*(A.base+off);return e;int Assign(Array &A,int e,.)int off=0;va_list ap;va_start(ap,e);int ind=0;for(int i=0;iA.dim;i+)ind=va_arg(ap,int);if(ind=i)return -1;off+=i*ind;va_end(ap);*(A.base+off)=e;return OK;int main()Array lx;InitArray(lx,2,3,4);/构造一个二维数组,前面2是维
23、数,后面两个数字是每维的长度/上图是3行4列的二维数组,行的下标是0,1,2,列的下标是0,1,2,3/InitArray(lx1,3,2,3,4);/三维数组的写法coutlx23的相对地址为:returnLocate(lx,-1,2,3)endl;/这里的2和3都是下标,相当于是a23,所以地址为11,也就是最后一个元素的地址,地址是从0开始的coutlx00的相对地址为:returnLocate(lx,-1,0,0)endl;Assign(lx,108,0,0);coutlx00的值为:Value(lx,-1,0,0)endl;Assign(lx,257,1,1);coutlx11的值为:Value(lx,-1,1,1)endl;system(pause);return 0;数组维数是:2数组每维元素数目分别是: 3 4数组元素数目是:12lx23的相对地址为:11lx00的相对地址为:0lx00的值为:108lx11的值为:257请按任意键继续. . .