




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、/*1. 設(shè)計(jì)線性表順序存儲(chǔ)結(jié)構(gòu)的11個(gè)基本操作函數(shù),并編程實(shí)現(xiàn)之 */#include<stdio.h>#include<stdlib.h>#include<string.h>#define LIST_INIT_SIZE 100#define LISTINCREMENT 10typedef char ElemType;typedef struct ElemType *elem;int length;int listsize;SqList;bool compare(ElemType p,ElemType e)if (p=e) return true;else
2、 return false;void InitList(SqList &L)/構(gòu)造一個(gè)空的線性表LL.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType);if(!L.elem)exit(0);L.length=0;L.listsize=LIST_INIT_SIZE; void DestroyList(SqList &L)/銷毀線性表Lfree(L.elem);L.elem=NULL;if(L.elem)exit(0);L.length=0;void ClearList(SqList &L)/將L重置為空表L.el
3、em='0'L.length=0;bool ListEmpty(SqList &L)/若L為空表,返回TRUE,否則FALSEif(L.length=0)return true;else return false;int ListLength(SqList L)/返回L中元素個(gè)數(shù)return L.length;bool GetElem(SqList L,int i,ElemType &e)/用e返回?cái)?shù)據(jù)中第i個(gè)元素的值if(!L.length) return false;elseif(i>L.length|i<1) return false;else
4、e=L.elemi-1;return true;int LocateElem(SqList L,ElemType e)int i=1;bool cmp=0;if(L.length=0) return 0;elsewhile(i<=L.length&&!cmp)cmp=compare(L.elemi-1,e);i+;if(cmp) return i-1;else return 0;int PriorElem(SqList L,ElemType cur_e,ElemType &pre_e)/若cur_e是L的數(shù)據(jù)元素,且不是第一個(gè),用pre_e返回它的前驅(qū)。否則操作失
5、敗int i=LocateElem(L,cur_e);if(i=0) printf("不存在您定位的元素!n");else if(i=1) printf("您指定的是首元素,無(wú)前驅(qū)!n");else pre_e=L.elemi-2;return i;int NextElem(SqList L,ElemType cur_e,ElemType &next_e)int i=LocateElem(L,cur_e);if(i=0) printf("不存在您定位的元素!n");else if(i=L.length) printf(&quo
6、t;您指定的是末元素,無(wú)后繼!n");next_e=L.elemi;return i;bool ListInsert(SqList &L,int i,ElemType e)/在L的第i個(gè)位置之前插入新的數(shù)據(jù)e,L的長(zhǎng)度增加1ElemType *newbase,*p,*q;if(i<1|i>L.length+1)return false;elseif(L.length>=L.listsize)newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType);if(!new
7、base) exit(0);L.elem=newbase;L.listsize+=LISTINCREMENT;q=&(L.elemi-1);for(p=&(L.elemL.length-1);p>=q;-p) *(p+1)=*p;*q=e;L.elemL.length+1='0'+L.length;return true;bool ListDelete(SqList &L,int i,ElemType &e)/刪除第i個(gè)元素,并用e返回,長(zhǎng)度減1ElemType *p,*q;if(i<1|i>L.length)return fa
8、lse;elsep=&(L.elemi-1);e=*p;q=L.elem+L.length-1;for(+p;p<=q;+p) *(p-1)=*p;L.elemL.length-1='0'-L.length;return true;void read_file(SqList &L,ElemType FileName)FILE *fp_in;InitList(L);if(fp_in=fopen(FileName,"r")=NULL)printf("can not open file!n");exit(0);while(
9、!feof(fp_in) fgets(L.elem,LIST_INIT_SIZE,fp_in);L.length=strlen(L.elem);fclose(fp_in);void write_file(SqList L)printf("%sn",L.elem);FILE *fp_out;if(fp_out=fopen("re_data.txt","w")=NULL)printf("can not open file!n");exit(0);fputs(L.elem,fp_out);fclose(fp_out);v
10、oid menu()printf("nn");printf(" 請(qǐng)選擇您需要的操作:nn");printf("*n");printf("| 0.ShowList 顯示L中所有元素 |n");printf("| 1.ClearList 將L重置為空表 |n");printf("| 2.ListEmpty 檢查L(zhǎng)為是否為空表 |n");printf("| 3.ListLength 返回L中元素個(gè)數(shù) |n");printf("| 4.GetElem 用
11、e返回表中第i個(gè)元素的值 |n");printf("| 5.LocateElem 找到元素e在表中的位 |n");printf("| 6.PriorElem 用pre_e返回它的前驅(qū) |n");printf("| 7.NextElem 用next_e返回它的后繼 |n");printf("| 8.ListInsert 在L第i個(gè)位置前插入數(shù)據(jù)e |n");printf("| 9.ListDelete 刪除第i個(gè)元素,并用e返回 |n");printf("| 10.Destro
12、yList 銷毀線性表L |n");printf("| 11.exit 返回 |n"); printf("*n");printf("n");int main()int choice;int select;int locate;ElemType e,pre_e,next_e;ElemType FileName12;SqList list;doprintf("nn1.讀取默認(rèn)文件(data.txt)n2.讀取您指定文件n3.退出nnn");printf("請(qǐng)輸入您的選擇:");scanf(
13、"%d",&select);if(select=1) strcpy(FileName,"data.txt");else if(select=2)printf("請(qǐng)輸入文件名:n");getchar();gets(FileName);else if(select!=3) printf("請(qǐng)輸入正確選項(xiàng)!n");continue;read_file(list,FileName);printf("已讀取您指定文件的順序表L!n");system("cls");/domenu
14、();scanf("%d",&choice);switch(choice)case 0:system("cls");printf("以下是順序表的所有元素:n%sn",list.elem);break;case 1:system("cls");ClearList(list);printf("順序表已清空!n");break;case 2:system("cls");if(ListEmpty(list) printf("L為空表!n");else p
15、rintf("L非空!n");break;case 3:system("cls");printf("L的長(zhǎng)度為:%dn",ListLength(list);break;case 4:system("cls");printf("選擇您要讀的元素位置:n");scanf("%d",&locate);if(GetElem(list,locate,e) printf("第%d個(gè)元素是:%cn",locate,e);else printf("順序表
16、長(zhǎng)度為%d,輸入位置不正確!",ListLength(list);break;case 5:system("cls");printf("請(qǐng)輸入您要定位的元素:n");getchar();e=getchar();locate=LocateElem(list,e);if(locate) printf("元素%c在L的第%d個(gè)位置上!n",e,locate);else printf("定位有誤!n");break;case 6:system("cls");printf("請(qǐng)輸入您要
17、定位的元素:n");getchar();e=getchar();locate=PriorElem(list,e,pre_e);if(locate>1)printf("您定位的元素%c的前驅(qū)元素為%cn",e,pre_e);else printf("定位有誤!n");break;case 7:system("cls");printf("請(qǐng)輸入您要定位的元素:n");getchar();e=getchar();locate=NextElem(list,e,next_e);if(locate<lis
18、t.length&&locate)printf("您定位的元素%c的后繼元素為%cn",e,next_e);else printf("定位有誤!n");break;case 8:system("cls");printf("請(qǐng)輸入您指定的位置:n");scanf("%d",&locate);printf("請(qǐng)輸入您要插入的元素:n");getchar();e=getchar();if(ListInsert(list,locate,e) printf("修改成功!n");else printf("您指定有誤!n");break;case 9:system("cls");printf("請(qǐng)輸入您指定的位置:n");scanf("%d",&locate);if(ListDelete(list,locate,e) printf("您刪除的元素是%c:n",e);else printf("您指定有誤!n"
溫馨提示
- 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 高墩施工防墜器速差技術(shù)專題
- 生態(tài)混凝土橋坡綠化工藝
- 2024年“巴渝工匠”杯競(jìng)賽負(fù)荷控制理論考試題庫(kù)大全-上(單選題)
- 高三年級(jí)下冊(cè)二??荚囌Z(yǔ)文試題(含答案)
- 防汛安全培訓(xùn)
- 中班走廊與樓梯健康安全
- 學(xué)校中層領(lǐng)導(dǎo)工作總結(jié)
- 實(shí)驗(yàn)小學(xué)教學(xué)常規(guī)培訓(xùn)
- 招聘面試培訓(xùn)
- 正畸口腔潰瘍護(hù)理常規(guī)
- JT-T-1302.1-2019機(jī)動(dòng)車駕駛員計(jì)時(shí)培訓(xùn)系統(tǒng)第1部分:計(jì)時(shí)終端技術(shù)要求
- 江蘇省泰州市興化市2023-2024學(xué)年七年級(jí)下學(xué)期期末數(shù)學(xué)試題
- 報(bào)關(guān)部報(bào)關(guān)員崗位月度KPI績(jī)效考核表
- 初中英語(yǔ)大綱詞匯表
- 山東省濟(jì)南市槐蔭區(qū)2023-2024學(xué)年小學(xué)六年級(jí)語(yǔ)文畢業(yè)檢測(cè)指導(dǎo)卷含答案
- 網(wǎng)上競(jìng)價(jià)響應(yīng)文件【模板】
- MOOC 化工熱力學(xué)-鹽城師范學(xué)院 中國(guó)大學(xué)慕課答案
- 創(chuàng)維電視電子說明書
- 統(tǒng)編小學(xué)道德與法治四年級(jí)教材分析-解讀
- (2024年)污水處理設(shè)備培訓(xùn)方案
- 2024年湖南開放大學(xué)《商務(wù)談判實(shí)務(wù)》課程參考試題庫(kù)(含答案)
評(píng)論
0/150
提交評(píng)論