




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1线性表的顺序表示#include"iostream"#include"malloc.h"usingnamespacestd;typedefstruct{telemintlength;intlistsize;SqList;intInit_Sq(SqList&L){L.elem=(int*)malloc(100*sizeof(int));ifLelem)exit(-2);L.length=0;L.listsize=100;return1;}intListInsert(SqList&L,inti,inte){return0;if(L.length>=L.listsize){int*newbase=(int*)realloc(L.elem,(L.listsize+10)*sizeof(int));if(!newbase)exit(-2);L.elem=newbase;L.listsize+=10;}int*q=&(L.elem[i-1]);int*p=&(L.elem[L.length-1]);for(p;p>=q;--p){*(p+1)=*p;}*q=e;++L.length;return1;}intListDelete(SqList&L,inti,int&e){return0;int*p=&(L.elem[i-1]);int*q=L.elem+L.length-1;for(++p;p<=q;++p){*(p-1)=*p;}--L.length;returne;}intmain(){inta[6]={1,2,3,4,5};int*q=&a[1];int*p=&a[4];for(p;p>=q;--p){*(p+1)=*p;}*q=3;for(inti=0;i<6;i++){cout<<a[i]<<"";}cout<<endl;SqListlx;Init_Sq(lx);for(intj=1;j<10;j++){ListInsert(lx,j,j);}ListInsert(lx,3,55);inte_return;ListDelete(lx,4,e_return);for(intm=0;m<10;m++){}cout<<endl;cout<<e_return;system("pause");return0;}132345125545678993请按任意键继续...2线性表的链性表示#include"iostream"#include"malloc.h"usingnamespacestd;typedefstructLNode{intdata;structLNode*next;}LNode,*LinkList;intInitList(LinkList&L){L=(LinkList)malloc(sizeof(LNode));L->next=NULL;return1;}intListInsert(LinkList&L,inti,inte){LinkListp=L;jwhile(p&&j<i-1){p=p->next;j;}return0;LinkLists=(LinkList)malloc(sizeof(LNode));s->data=e;s->next=p->next;p->next=s;return1;}intListDelete(LinkList&L,inti){LinkListp=L;jwhile(p->next&&j<i-1){p=p->next;j;}return0;LinkListq=p->next;p->next=q->next;free(q);return1;}intGetElem(LinkListL,inti){LinkListp=L->next;jwhile(p&&j<i){p=p->next;j;}return0;inte=p->data;returne;}tmain{LinkListlx;InitList(lx);for(inti=1;i<6;i++){ListInsert(lx,i,i);}ListDelete(lx,2);for(intj=1;j<5;j++){}cout<<endl;LinkListlx1,lx2;InitList(lx1);InitList(lx2);for(intm=1;m<6;m++){ListInsert(lx1,m,m);}for(intn=1;n<6;n++){ListInsert(lx2,n,2*n);}for(intj=1;j<6;j++){cout<<GetElem(lx1,j)<<"";}system("pause");return0;}34512345请按任意键继续...3双向链表#include"iostream"#include"malloc.h"usingnamespacestd;typedefstructdlist{intdata;structdlist*prior;structdlist*next;}DList,*DLinkList;voidInitList(DLinkList&L){L=(DLinkList)malloc(sizeof(DList));L->next=L;L->prior=L;}intListInsert(DLinkList&L,inti,inte){DLinkListp,s;p=L->next;jwhile(p&&j<i){p=p->next;j;}return0;s=(DLinkList)malloc(sizeof(DList));s->data=e;s->prior=p->prior;p->prior->next=s;s->next=p;p->prior=s;return1;}intListDelete(DLinkList&L,inti,int&e){DLinkListp;p=L->next;jwhile(p&&j<i){p=p->next;j;}return0;e=p->data;p->prior->next=p->next;p->next->prior=p->prior;free(p);return1;}intGetElem(DLinkList&L,inti){DLinkListp,s;p=L->next;jwhile(p&&j<i){p=p->next;j;}return0;inte=p->data;returne;}tmain{DLinkListlx;InitList(lx);for(inti=1;i<6;i++){ListInsert(lx,i,2*i-1);}for(intj=1;j<6;j++){cout<<GetElem(lx,j)<<"";}cout<<endl;ListDelete(lx,2,e);cout<<e<<endl;for(intj=1;j<5;j++){cout<<GetElem(lx,j)<<"";}system("pause");return0;}357931579请按任意键继续...4顺序栈#include"iostream"#include"malloc.h"usingnamespacestd;#defineSTACK_INIT_SIZE100//存ä?储ä¡é空?间?初?始º?分¤?配?量¢?#defineSTACKINCREMENT10//存ä?储ä¡é空?间?分¤?配?增?量¢?typedefstruct{tbaseint*top;intstacksize;ackintInitStack(Stack&S){S.base=(int*)malloc(STACK_INIT_SIZE*sizeof(int));ifSbaseexit(-2);S.stacksize=STACK_INIT_SIZE;return1;}intGetTop(Stack&S,int&e){if(S.top==S.base)return0;e=*(S.top-1);return1;}intPush(Stack&S,inte){if(S.top-S.base>=S.stacksize){S.base=(int*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int));ifSbaseexit(-2);S.top=S.base+S.stacksize;S.stacksize+=STACKINCREMENT;}*S.top++=e;}intPop(Stack&S,int&e){if(S.top==S.base)return0;e=*--S.top;return1;}tmain{Stacklx;InitStack(lx);for(inti=1;i<6;i++){Push(lx,i);}GetTop(lx,e);cout<<e<<endl;tePop(lx,e1);cout<<e1<<endl;GetTop(lx,e1);cout<<e1<<endl;system("pause");return0;}554请按任意键继续...5队列#include"iostream"#include"malloc.h"usingnamespacestd;typedefstructQNode{intdata;structQNode*next;}QNode,*QueuePtr;typedefstruct{QueuePtrfront;QueuePtrrear;}LinkQueue;intInitQueue(LinkQueue&Q){Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));if(!Q.front)exit(-2);Q.front->next=NULL;return1;}intDestroyQueue(LinkQueue&Q){while(Q.front){Q.rear=Q.front->next;free(Q.front);Q.front=Q.rear;}return1;}intInsert(LinkQueue&Q,inte){QueuePtrp=(QueuePtr)malloc(sizeof(QNode));exit(-2);p->data=e;p->next=NULL;Q.rear->next=p;Q.rear=p;return1;}intGetFront(LinkQueue&Q,int&e){if(Q.front==Q.rear)return0;e=Q.front->next->data;return1;}{}{}{GetRear(LinkQueue&Q,int&e)if(Q.front==Q.rear)return0;e=Q.rear->data;return1;Delete(LinkQueue&Q,int&e)if(Q.front==Q.rear)return0;QueuePtrp;p=Q.front->next;e=p->data;Q.front->next=p->next;if(Q.rear==p)Q.rear=Q.front;free(p);return1;main()LinkQueuelx;InitQueue(lx);for(inti=1;i<6;i++){Insert(lx,i);}frontGetFront(lx,front);cout<<front<<endl;intrear;GetRear(lx,rear);cout<<rear<<endl;Delete(lx,e);cout<<e<<endl;GetFront(lx,front);cout<<front<<endl;system("pause");return0;}1512请按任意键继续...6循环队列#include"iostream"#include"malloc.h"usingnamespacestd;#defineMAXSIZE100typedefstruct{tbasefrontintrear;}SqQueue;intInit(SqQueue&Q){Q.base=(int*)malloc(MAXSIZE*sizeof(int));ifQbaseexit(-2);Q.front=Q.rear=0;return1;}intQueueLength(SqQueue&Q){}{}{}{}{}{return(Q.rear-Q.front+MAXSIZE)%MAXSIZE;EnQueue(SqQueue&Q,inte)if((Q.rear+1)%MAXSIZE==Q.front)return0;Q.base[Q.rear]=e;Q.rear=(Q.rear+1)%MAXSIZE;return1;DeQueue(SqQueue&Q,int&e)if(Q.front==Q.rear)return0;e=Q.base[Q.front];Q.front=(Q.front+1)%MAXSIZE;return1;GetFront(SqQueue&Q)inte=Q.base[Q.front];returne;Getrear(SqQueue&Q)inte=Q.base[Q.rear-1];returne;main()SqQueuelx;Init(lx);for(inti=1;i<6;i++){EnQueue(lx,i);}cout<<QueueLength(lx)<<endl;cout<<GetFront(lx)<<endl;cout<<Getrear(lx)<<endl;DeQueue(lx,m);cout<<m<<endl;cout<<GetFront(lx)<<endl;system("pause");return0;}51512请按任意键继续...7顺序表字符串#include"iostream"#include"malloc.h"usingnamespacestd;#defineOK1#defineERROR0typedefstruct{char*ch;intlength;}HString;intStrAssign(HString&T,char*chars){if(T.ch)free(T.ch);//inti=strlen(chars);ntichar*c;for(c=chars;*c;++i,++c);//判D断?条¬?件t为a*c!='\0'T.length=0;}for(intj=0;j<i;j++){T.ch[j]=chars[j];}T.length=i;returnOK;0}intStrLength(HString&S){returnS.length;}intConcat(HString&T,HString&S1,HString&S2){Tch(char*)malloc((S1.length+S2.length)*sizeof(char));for(inti=0;i<S1.length;i++){T.ch[i]=S1.ch[i];}T.length=S1.length+S2.length;for(intj=0;j<S2.length;j++){T.ch[S1.length+j]=S2.ch[j];}returnOK;}intSubString(HString&Sub,HString&S,intpos,intlen){sreturnERROR;}Sub.length=len;for(inti=0;i<len;i++){Sub.ch[i]=S.ch[pos+i];}returnOK;}intPrint(HString&T){for(inti=0;i<T.length;i++){cout<<T.ch[i]<<"";}cout<<endl;returnOK;}tmain{HStringlx,hhc;StrAssign(lx,"huanhuncao");StrAssign(hhc,"lixing");Print(lx);Print(hhc);cout<<StrLength(lx)<<endl;cout<<StrLength(hhc)<<endl;HStringlx1;lxchcharmalloclxlengthhhclengthsizeofchar);Concat(lx1,lx,hhc);Print(lx1);HStringlx2;SubString(lx2,lx1,2,3);Print(lx2);system("pause");return0;}huanhuncaolixing6huanhuncaolixinganh请按任意键继续...8链式字符串就是线性表的链式表示一样9数组的表示#include"iostream"astart#include"malloc.h"usingnamespacestd;#defineMAX_ARRAY_DIM8#defineOK1#defineERROR0typedefstruct{intbaseint*bounds;ntsotalintInitArray(Array&A,intdim,...){returnERRORA.dim=dim;A.bounds=(int*)malloc(dim*sizeof(int));if(!A.bounds)exit(-2);intelemtotal1;va_listap;va_start(ap,dim);for(inti=0;i<dim;i++){A.bounds[i]=va_arg(ap,int);cout<<A.bounds[i]<<"";if(A.bounds[i]<0){returnelemtotal*=A.bounds[i];}cout<<endl;A.elemtotal=elemtotal;va_end(ap);A.base=(int*)malloc(elemtotal*sizeof(int));*(A.base)=0;if(!A.base)exit(-2);A.constants=(int*)malloc(dim*sizeof(int));ifAconstantsexit(-2);A.constants[dim-1]=1;for(inti=dim-2;i>=0;--i){A.constants[i]=A.bounds[i+1]*A.constants[i+1];}nOK}intDestroyArrayArrayA{if(!A.base)returnERRORfree(A.base);A.base=NULL;if(!A.bounds)returnERRORfree(A.bounds);A.bounds=NULL;ifAconsta
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 四川省绵阳市高中学阶段学校2025届数学八年级第一学期期末经典试题含解析
- 2025届浙江省杭州市西溪中学九上数学期末经典模拟试题含解析
- 江苏省徐州市邳州市2025届物理九年级第一学期期末质量检测试题含解析
- 2025届河南省周口市鹿邑县九上物理期末学业水平测试模拟试题含解析
- 长沙环境保护职业技术学院《艺术基础》2023-2024学年第一学期期末试卷
- 大学生检察院实习报告范文3000字
- 建筑工程进度滞后时应对措施
- 高端艺术品收藏与流通交易合作协议
- 江苏省无锡市名校2025届数学九上期末学业质量监测模拟试题含解析
- 多元文化背景下劳动教育心得体会
- 2024年安徽省濉溪县人民医院公开招聘医务工作人员试题带答案详解
- 2025年浙江省宁海县事业单位公开招聘辅警考试题带答案分析
- 四川省广安市(武胜、岳池、华蓥)2024-2025学年八年级下学期期末考试物理试卷(含答案)
- 脑卒中的饮食护理课件
- 高中化学方程式总结
- 辽宁省医学影像云技术规范
- 申报高级会计师资格评审的公示填写模板
- 作文-曼娜回忆录全文小说
- GB/T 17285-2022电气设备电源特性的标记安全要求
- GB/T 14996-2010高温合金冷轧板
- GB 11550-2009汽车座椅头枕强度要求和试验方法
评论
0/150
提交评论