




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、/*1. 設計線性表順序存儲結構的11個基本操作函數(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)/構造一個空的線性表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中元素個數(shù)return L.length;bool GetElem(SqList L,int i,ElemType &e)/用e返回數(shù)據(jù)中第i個元素的值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ù)元素,且不是第一個,用pre_e返回它的前驅。否則操作失
5、敗int i=LocateElem(L,cur_e);if(i=0) printf("不存在您定位的元素!n");else if(i=1) printf("您指定的是首元素,無前驅!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;您指定的是末元素,無后繼!n");next_e=L.elemi;return i;bool ListInsert(SqList &L,int i,ElemType e)/在L的第i個位置之前插入新的數(shù)據(jù)e,L的長度增加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個元素,并用e返回,長度減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(" 請選擇您需要的操作:nn");printf("*n");printf("| 0.ShowList 顯示L中所有元素 |n");printf("| 1.ClearList 將L重置為空表 |n");printf("| 2.ListEmpty 檢查L為是否為空表 |n");printf("| 3.ListLength 返回L中元素個數(shù) |n");printf("| 4.GetElem 用
11、e返回表中第i個元素的值 |n");printf("| 5.LocateElem 找到元素e在表中的位 |n");printf("| 6.PriorElem 用pre_e返回它的前驅 |n");printf("| 7.NextElem 用next_e返回它的后繼 |n");printf("| 8.ListInsert 在L第i個位置前插入數(shù)據(jù)e |n");printf("| 9.ListDelete 刪除第i個元素,并用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.讀取默認文件(data.txt)n2.讀取您指定文件n3.退出nnn");printf("請輸入您的選擇:");scanf(
13、"%d",&select);if(select=1) strcpy(FileName,"data.txt");else if(select=2)printf("請輸入文件名:n");getchar();gets(FileName);else if(select!=3) printf("請輸入正確選項!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的長度為:%dn",ListLength(list);break;case 4:system("cls");printf("選擇您要讀的元素位置:n");scanf("%d",&locate);if(GetElem(list,locate,e) printf("第%d個元素是:%cn",locate,e);else printf("順序表
16、長度為%d,輸入位置不正確!",ListLength(list);break;case 5:system("cls");printf("請輸入您要定位的元素:n");getchar();e=getchar();locate=LocateElem(list,e);if(locate) printf("元素%c在L的第%d個位置上!n",e,locate);else printf("定位有誤!n");break;case 6:system("cls");printf("請輸入您要
17、定位的元素:n");getchar();e=getchar();locate=PriorElem(list,e,pre_e);if(locate>1)printf("您定位的元素%c的前驅元素為%cn",e,pre_e);else printf("定位有誤!n");break;case 7:system("cls");printf("請輸入您要定位的元素: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("請輸入您指定的位置:n");scanf("%d",&locate);printf("請輸入您要插入的元素:n");getchar();e=getchar();if(ListInsert(list,locate,e) printf("修改成功!n");else printf("您指定有誤!n");break;case 9:system("cls");printf("請輸入您指定的位置:n");scanf("%d",&locate);if(ListDelete(list,locate,e) printf("您刪除的元素是%c:n",e);else printf("您指定有誤!n"
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 車輛股權轉讓與改裝升級服務合同
- 出租車司機聘用及服務質量保障合同
- 項目中止后采購合同數(shù)量變更及取消協(xié)議
- 老妖精咨詢工程師課件
- 美術紅色文化課件設計
- 安全生產方針五要素
- 物業(yè)消防工作計劃
- 安全生產管理制度及處罰條例
- 水電安全操作規(guī)程
- 安全生產提升活動總結
- 山東交通學院成人教育工程制圖-復習卷期末考試復習題及參考答案
- 小學科學學科建設方案
- 四川省達州市2024年數(shù)學八年級下冊期末聯(lián)考試題含解析
- 創(chuàng)維電視電子說明書
- (2024年)污水處理設備培訓方案
- 外貿專業(yè)知識培訓
- 環(huán)保法規(guī)與政策培訓
- 2024年湖南開放大學《商務談判實務》課程參考試題庫(含答案)
- 快速康復外科理念ERAS與圍手術期護理
- 教師招聘公共基礎知識(教育學、心理學-教育法規(guī))試題
- 藥店風險管理和安全防控
評論
0/150
提交評論