




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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//存??儲??é空?間?初?始o?分¤?配?量¢?#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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- T/CMES 37005-2023滑道運營管理規范
- T/CIS 47001-2018中國儀器儀表學會標準
- T/CHTS 20019-2022公路橋梁跨縫模數式伸縮裝置
- T/CHTS 10120-2023雄安新區高摻量膠粉改性瀝青路面施工技術指南
- T/CHTS 10047-2021公路養護信息數據元與代碼
- T/CGCC 60-2021鹵蔬菜制品
- T/CEMIA 026-2021濕電子化學品技術成熟度等級劃分及定義
- T/CECS 10271-2023不銹鋼分水器
- T/CECS 10061-2019綠色建材評價新風凈化系統
- T/CCOA 74-2023花生油感官評價
- 小學英語寫作教學的思考與實踐 桂婷婷
- 患者發生過敏性休克應急預案演練腳本模板
- 南京醫科大學招聘考試《綜合能力測試》真題及答案
- 封閉冷通道施工方案
- 2021年新高考全國1卷(含答案解析)
- 《觸不可及》影視鑒賞課件
- 認知知覺障礙的作業治療概述(作業治療技術課件)
- 畢業論文與畢業設計指導課件
- 采購合同一般采購合同
- 形象管理(南開大學)【超星爾雅學習通】章節答案
- 《鮮衣怒馬少年時 唐宋詩詞篇 全集 》讀書筆記PPT模板思維導圖下載
評論
0/150
提交評論