



版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
c語言數據結構相關知識(Clanguagedatastructureknowledge)1includesstdlib.h><includesstdio.h><includesmalloc.h><structlinerlist/Z定義線性表結構I/Z強化理解這里?list號的含義,保證1->list口不報錯:表達式必須包含指向對象的指針類型//以及(1->list=(int*)malloc(ms*sizeof(int)))==nul1)正常分配int*list;//存線性表元素intsize;//存線性表長度intmaxsize;/Z存list數組元素個數);typedefstructlinerlistlist;voidinitlist(list*1,intm){/Z初始化線性表if((1->list=(int*)malloc(ms*sizeof(int)))==null)printf("內存申請失敗!ヽn");exit(1);)1->size=0;1->maxsize=ms.)insertlist(listint*1,intitem,intrc){//將item插入線性表1的rc位置//item為記錄值,rc為插入位置inti;if(1->size==1->maxsize)/Z線性表已滿return-1;if(rc)<0)//插入位置為〇到1ー>sizerc=0;if(cr>1>size)TOC\o"1-5"\h\zfor (i=1 ー〉size-1; i > = rc;-)// 1 [i+ 1]= 1 [i].1- > list [i+ 1] =1- > list [];/Z線性表元素后移1- > list [rc] = item;1- > size ++;return0;)voidoutputlist(list*1){/Z輸出線性表元素inti;for(i=0;i<1>size;i++)printf("%d”,1->list[]);printf("ヽn");findlist(listint*1,intitem){/Z尋找線性表元素,返回20為元素位置,1為沒找到inti;for(i=0;i<1>size;i++)[if(item==1ー>list□)/Z找到相同元素,返回位置returni;)return-1;)deletelistl(listint*1,intitem)/Z找到相同元素進行刪除{//刪除指定元素值的線性表記錄,返回ユ0為刪除成功inti,n;for(i=0;i<1>size;i++)if(item==1->list[])break;if(i<1>size){for(n=i;n<1>size-1;n++)1->list[n]=1ー)!ist[n+1].1->size ;returni;)return-1;)deletelist2(listint*1,intrc)Deletethespecified{//recordRCpositionelementsInti;If(rc<0||rc>=L->size)Return-1;For(i=rc;i<L->size-l;i++)L->list[i]=L->list[i+1];L->size一一;Return0;)Void(main)(LISTLL;Inti,r;//printf("LISTaddr=%p\tsize=%d\tMaxSize=%d\n”,LL.list,LLsize,LL.MAXSIZE);InitLIST(&LL,10);Printf("LISTaddr=%p\tsize=%d\tMaxSize=%d\n",LL.list,LL.size,LL.MAXSIZE);OutputLIST(&LL);While(1)Printf("pleaseinputelementvalues,0inputendinsert:");Fflush(stdin);Scanf(%d,&i);If(i==0)break;Printf("pleaseentertheinsertionposition:");Scanf(%d,&r);InsertLIST(&LL,I,R-l);Printf("lineartable:");OutputLIST(&LL);}While(1)(Printf("pleaseenterthesearchelementvalue,enterthe0endoftheinsertoperation:");Fflush(stdin);Scanf(%d,&i);If(i==0)break;R-FindLIST(&LL,I);If(r<0)Printf("notfound!");ElsePrintf("thereareelementsthatmeettheconditionsforlocation:%d\n,r+1);)While(1)IPrintf("pleaseenterthedeletedelementvalues,0inputendsearchoperations:v);Fflush(stdin);Scanf(%d,&i);If(i==0)break;R-DeleteLISTl(&LL,I);If(r<0)Printf("notfound!");Else(Printf("thereareelementsthatmeettheconditionsforlocation:%d\n,r+1);OutputLIST(&LL);})While(1)[Printf("pleaseenterthedeletedelementposition,0inputenddelete:");Fflush(stdin);Scanf(%d,&r);If(r==0)break;I=DeleteLlST2(&LL,R-l);If(i<0)Printf("\nline");ElseIPrintf("lineartable:");OutputLIST(&LL);Twottinclude<stdio.h>ttinclude"stdlib.h"#include<malloc.h>ttdefineLIST_INIT_SIZE100ttdefineOK1#defineERROR0ttdefineOVERFLOW-1#defineNum3TypedefintDataType;TypedefintStatus;TypedefstructDataType*elem;IntLength;intlistsize;seqlist);seqlist1;statusinitseqlist(seqlist*1)1->elem=(datatype*)malloc(list_init_size*sizeof(datatype));if(!1->elem)exit(overflow);1->length=0;1->listsize=list_init_size;returnok;)statusinsertseqlist(seqlist*1,inti,datatype(e)(datatype*q*p;if(i<1IIi>1>length+1)returnerror;elseif(1-1->>>>=lengthlistsize)!printf("theseqlistisfull!");returnerror;elseq=1->elem+(i-1);for(p=1+1->->elemlength-1;p>=(q,p)(p+1)=*p;q=(e);+1->length;returnok;))intdeleteseqlist(seqlist*1,inti)intj;if(i<1IIi>1>length)printf("theinumberisnotexist!")returnerror;)else!for(j=i-1;(j)<1>length;j++)*(1->elem+j)=*(1->elem+j+1);1->length ;returnok;})//voidsearchseqlist(seqlist*1,datatypex)voidsearchseqlist(seqlist*1,intx)intj=0;>elem+j).>elem+j).=(x))J++;if(j)>1>length)printf("error");elseprintf("運行結果");printf("%d",(j+1);)voidprintfseqlist(seqlist*1)[printf("運行結果:");inti;for(i=0;i<1>length;i++)printf("%d"*(1->elem+(i));voidlenseqlist(seqlist*1)inti=0,*j;for(j=lー>iく1>elem;length;j++)i++;printf("an元素個數為:%d",(i);)voidpriorseqlement(seqlist*1,inti)(int*j;if(i<1IIi>1>length)printf("輸入有誤");elseif(i==1)printf("無前驅");else(j=1->elem+(i-2);printf("運行結果:");printf("%d"*(j);statusnextseqelement(seqlist*1,inti)!int*j;if(i<1II1->length)printf("輸入有誤");elseif(i==1)printf("無后繼”);else(j=1->elem+i;printf("運行結果:“);printf("%d"*(j);returnok;無效listseqempty(詳細說明:*L)如果(L->長度==0)printf("書序表為空”);elseprintf(“順序表非空”);)狀態(tài)changseqlist(詳細說明:*L)inti,j,q;為(i=0,J=L>length-1!我く1>長2;i++,J)(Q=X(L—>元素+我);*(L—>元素+我)=X(L->元素+J);*(L—>元素+J)+Q;)還可以;狀態(tài)clearseqlist(詳細說明:*L)1ー〉長度=0;還可以;)無效destoryseqlist(詳細說明:*L)!免費(D;1ー〉長度=0;L—>listsize=0;}無效mainO!詳細說明:?拉=L;intI,E,J,X,K,M,P,Q=0;initseqlist(L);printf("輸入元素:");為(P=La->兀素;QV民;Q++++)scanf(“%d”,P);洛杉磯ー》長度++;)printfseqlist(L);printf("\n輸入要插入元素的位置和元素:“);scanf(“%d,%d”,&我&E);insertseqlist(LA,I,E);printfseqlist(L);printf("\n輸入要刪除元素的位置:");scanf("%d",與J);deleteseqlist(L,J);printfseqlist(L);printf("\n輸入要查找的值:”);scanf(“%d”,和X);searchseqlist(L,X);printfseqlist(L);printf("\n輸入元素求其前驅:“);scanf(“刎”,和K);priorseqlist(L,K);printfseqlist(L);printf("\n輸入元素求其后繼:“);scanf("%d”,&M);nextseqlist(L,M);printf("\n求表長:”);lenseqlist(L);//判斷表是否為空printf("\n判斷表是否為空:\n");listseqempty(L);printf("\n");printf("逆轉:\n");changseqlist(L);printfseqlist(L);printf("\n");printf(“置空表:\n");clearseqlist(L);printf(“\n”);printf("銷毀:”);destoryseqlist(L);)單鏈表代碼:H/列表。#包括く文件使用命名空間;typedefint域;typedefstruct的學生輸出N;下個學生;}生,*slistlink;班段{公共:slist();//建立一1個空鏈表**************createslist()/建立鏈表?*************無效;clearslist()/將鏈表重置為空鏈表**************無效;s1istlength()/返回鏈表中的長度**************輸出;布爾slistempty();//judgewhetherthelistisempty**************listElemType(GetElem);//a**************returnsalistoftheelementsVoid(SortElem);//thesortlistVoid(SListlnsert);//insertanelement**************Void(SListDelete);//deleteanelement**************Bool(SIistDeleteSame);//deletethesameelementsVoid(ShowSList);//displayalistof**************Void(FileSave);//writefilesVoid(FileRead);//openfromfileVoid(FileDelete);//deletefromthefileVoid(FileCheck);//checkthecontentsofthefile(?SList);//destructionlistPrivate:SListLinkhead;SListLinkp;SListLinkq;SListLinktemp;Intslistlength;);SList::(SList)//createanemptylistHead=p=q=temp=NULL;Slistlength=O;VoidSList:(CreateSList)//theestablishmentofthelist:[If(head==NULL)(ElemTypem;Cout<<"pleaseenteranumber:(0,negativenumberindicatestheendofinput)"<<endl;Cin>>m;If(m>0)IHead=q=p=temp=newStudent;Q->n=m;++slistlength;While(m=0&&m>-0!)Cout<<"pleaseenteranumber:?endl;Cin>>m;If(m=0&&m>=0!)!P=newStudent;Pー〉n=m;++slistlength;Qー〉next=p;Q=P;)}Pー〉next=NULL;Coutくく?endlcreatesuccess";//nooutput?ElseHead=p=q=NULL;ElseCout<<"youhavealreadycreatedanewlistof"<<endl;IntSList:(SListLength)://returntolistlengthCoutくく/thenumberofelementsinthelistis:?slistlength<<endl;Returnslistlength;ElemTypeSList:(GetElem)://returnsanelementlistcorrespondingserialnumberIf(head=NULL!)[ElemTypem;“Pleaseenterthecout?toreturnthenumberinthelistintheserialnumber"?endl;Cin>>m;If(m>=0)[If(slistlength>=m)IIntindx=l;P=head;If(m==indx)“Returntoyoursearchlistcout?number:<<head->n<<endl;Returnhead->n;)p=pー〉next;++indx;if(m==indx)icout<く”鏈表返回你查找的數:"<<p>n<<final;returnp->n;))cout<く"序號超出鏈表范圍〃くくfinal;return1;))else(cout<く"不允許非法輸入"くくfinal;return1;))else(cout<く"此鏈表是空鏈表"くくfinal;allslist::slistempty()//判斷鏈表是否為空鏈表if(head!==null)!cout<く〃此鏈表不是空鏈表〃くくfinal;returnfalse;)else(cout<く〃此鏈表是空鏈表〃くくfinal;returntrue;allslist::slistdeletesame()/Z刪除鏈表中相同元素elemtypem;cout<く〃刪除鏈表中相同元素的節(jié)點,請輸入一個要刪除的數:“く<final;cin>>m;if(m>=0){if(head!==null)returntrue;)else{cout<く”這是ー個空鏈表”くくfinal;returnfalse;elsecout<く”不允許非法輸入"くくfinal;returnfalse;)}voidslist::sortelem()/Z對鏈表排序iif(head!==null)!for(p=head;p!=null;p=p->next)(temp=p;for(qp->next;q!=null;q=qー〉next)
for(qif(q>n>=temp->n)temp=q;if(temp.=p)elemtypet=pn;p->n=temp->n;temp->n=t;cout<く"排序成功”く<final;cout<く”此鏈表是空鏈表,不能進行此操作〃くくfinal;voidslist::clearslist()/Z將鏈表重置為空表if(head!==null)if(head一>===null)deletehead;head=null;elsep=head->next;while(q!=null&p!==null)q=p->next;head一>==q;deletep;P=q;q=p->next;)deletehead;head=null;slistlength=0;}<final;cout<<final;cout<く〃此鏈表是空鏈表"<<final;voidslist::showslist()/Z顯示一個鏈表Iif(head!==null)(p=head;cout<く"顯示鏈表所有元素;“く<final;die{cout<<p>n<<final;p=p->next;}而(P!=null);cout?“此鏈表共有元素:“くくslistlengthくく“個”くくendl;coutくく“此鏈表是空鏈表"くくendl;))無效:slistinsert()/頭插法插入一個元素段:{ElemTypem;coutくく“請輸入要插入的數字:"くくendl;CIN>>M;如果(m>0)如果(頭!=null)p=新學生;P>N=M;++slistlength;P>下=頭;頭=P;)其他的I頭=新學生;頭ー〉n=m;++slistlength;頭ー>下=null;)coutくく“插入數據成功“くくendl;返回;無效:slistdeleteO/刪除ー個元素段:ElemTypem;布爾fliG=真;coutくく“請輸入要刪除的數字:“くくendl;CIN>>M;如果(m>0)I如果(頭!=null){如果(頭->n==m)/頭結點指向的值符合刪除要求如果(頭->下==null)/刪除頭結點,且頭結點沒有后繼元素時刪除的頭;頭=null; slistlength;coutくく“刪除數據成功”くくendl;返回;)其他//刪除頭結點,且頭結點有后繼元素時IP=頭ー》下;頭ー>下=null;刪除的頭;頭=null; slistlength;頭=P;coutくく“刪除數據成功”くくendl;返回;p=頭;做{如果(P>N==m)I如果(P>下==null)/要刪除的是鏈表的最后ー個元素IQ二頭;而(Q-〉下ー>下!=null)/尋找出倒數第二個節(jié)點{q=q>下;刪除P;P=null; slistlength;coutくく“刪除數據成功”くくendl;FliG=false;打破;)其他//要刪除的是鏈表的中間ー個元素(Q=頭;而(Q-)下ー>n!=m)(q=q〉下;}Q〉下二P〉下;刪除P;P=null; slistlength;coutくく“刪除數據成功”くくendl;FliG=false;打破;))P=Pー)下;}而(P!=null);如果(飛行)(Coutくくdoesnotexist"yourinputdatalistin<<endl;Return;ElseCout<<"thislistisemptylist,can,tdothis"<<endl;Return;Void(SList::FileSave)[OFSTREAMout;Out.open("one-waylinkedlist.Txtpractice");If(out.isopen)(!){"Erroropeningfile"?endlcerr?;Return;ElseIf(head==NULL)!Thecerr?listisemptyandnodatacanbesaved”くくendl;)ElseIP=head;Do(Out?p->n?,\n);P=p->next;}while(P!=NULL);Cout?"file"?endl;(out.close);Void(SList::FileRead)Ifstreamin;In.open(,zone-waylinkedlist.Txtpractice");If(in.isopen)(!)"Erroropeningfile"?endlcerr?;Return;ElseIf(head=NULL!)Thecout<<listisnotempty,notforthisoperation”くくendl;Return;ElseHead=p=q=temp=newStudent;ElemTypenl;While(true)If((in?nl))(.Eof)Deletep;Tempー〉next=NULL;Break;Q->n=nl;++slistlength;P=newStudent;Qー〉next=p;Temp=q;Q=P;))Cout<<"fileread"?endl;)(in.close);Void(SList::FileDelete)Ifstreamin;In.open("one-waylinkedlist.Txtpractice");If(in.isopen)(!)[Thecerr?filedoesnotexist?endl;Return;)(in.close);Chara[]="delsinglelistexercise.Txt";ElemTypechoice;Do(Cout?"sureyouwanttodeletethefile?1==0==isnot?endlrCin>>choice;System("CLS");}while(choice!=l&&choice!=0);Switch(choice)Case1:System(a);Cout<<"successfullydeletedfile"<<endl;Break;Case0:Cout<<giveup"deletethefile"?endl;Break;Void(SList::FileCheck)Ifstreamin;In.open("one-waylinkedlist.Txtpractice");If(in.isopen)(!){Cerr<<"filecheckerror,please
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國古式桌子數據監(jiān)測研究報告
- 黑龍江省哈爾濱工大附中2024-2025學年九年級下學期化學寒假調研測試題(含答案)
- 2025年軍隊文職人員招聘之軍隊文職政治學練習題(一)及答案
- 2019-2025年消防設施操作員之消防設備中級技能通關提分題庫及完整答案
- 產品采購協議細節(jié)
- 房地產公司涉及的設計方面協議年
- 促銷活動效果分析統計表
- 慢病相關知識培訓課件
- 人力資源招聘與員工離職統計表
- 河南省駐馬店上蔡縣2024-2025學年七年級上學期期末生物學試題(含答案)
- 人際關系與溝通技巧-職場中的上行溝通-下屬與上司溝通
- 超聲引導下椎管內麻醉
- 幼兒園科學課件:《大肚子媽媽》
- (完整版)200210號文-工程勘察設計收費標準(2002年修訂本)本月修正2023簡版
- 基于核心素養(yǎng)下小學英語單元整體作業(yè)設計實踐研究 論文
- XX學校初高貫通銜接培養(yǎng)實施方案
- 2022版《義務教育科學課程標準》試題及答案
- 組織效能概述和提高組織效能的方法
- 私募股權投資業(yè)務激勵管理辦法
- 廣東省深圳市南山區(qū)2022-2023學年下學期學科素養(yǎng)期末學業(yè)評價三年級科學試卷(掃描版無答案)
- 高考日語基礎歸納總結與練習(一輪復習)
評論
0/150
提交評論