




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、15實(shí)驗(yàn)一 線性表的基本操作一、實(shí)驗(yàn)?zāi)康呐c基本要求1掌握數(shù)據(jù)結(jié)構(gòu)中的一些基本概念。數(shù)據(jù)、數(shù)據(jù)項(xiàng)、數(shù)據(jù)元素、數(shù)據(jù)類型和數(shù) 據(jù)結(jié)構(gòu),以及它們之間的關(guān)系。2了解數(shù)據(jù)的邏輯結(jié)構(gòu)和數(shù)據(jù)的存儲(chǔ)結(jié)構(gòu)之間的區(qū)別與聯(lián)系;數(shù)據(jù)的運(yùn)算與數(shù) 據(jù)的邏輯結(jié)構(gòu)的關(guān)系。3掌握線性表的基本操作:插入、刪除、查找以及線性表的合并等運(yùn)算。 4掌握運(yùn)用 C 語言上機(jī)調(diào)試線性表的基本方法。二、實(shí)驗(yàn)條件1硬件:一臺(tái)微機(jī)2軟件:操作系統(tǒng)和 C 語言系統(tǒng)三、實(shí)驗(yàn)方法確定存儲(chǔ)結(jié)構(gòu)后,上機(jī)調(diào)試實(shí)現(xiàn)線性表的基本運(yùn)算。四、實(shí)驗(yàn)內(nèi)容1試編寫在無頭結(jié)點(diǎn)的單鏈表上實(shí)現(xiàn)線性表基本運(yùn)算 LOCATE( L,X),INSERT (L, X , 1)和 DELE
2、TE ( L , 1)的算法。2 假設(shè)有兩個(gè)按數(shù)據(jù)元素值遞增有序排列的線性表 A和B,均以單鏈表作為存 儲(chǔ)結(jié)構(gòu)。 編寫算法將 A 表和 B 表歸并成一個(gè)按元素值遞減有序 (即非遞增有 序,允許值相同)排列的線性表 C,并要求利用原表(即A表和B表)結(jié)點(diǎn) 空間存放表 C。3將一個(gè)線性表中的值就地逆置。4在線性表的順序存儲(chǔ)結(jié)構(gòu)的第一個(gè)位置上插入一個(gè)元素。(注意區(qū)分鏈表和順序表) 實(shí)驗(yàn)代碼:#include "stdlib.h"/ 定義結(jié)構(gòu)體#include "stdio.h" struct nodeint d;struct node *next;struct
3、 node *head1,*head2,*p,*q;void pre( struct node *head)/ 打印數(shù)據(jù)printf( " 鏈表中的數(shù)據(jù)為: n" ); p=head;while (p!=NULL)printf( "%5d",p->d); q=p;p=p->next;printf( "n" );struct node *creat() / 建立鏈表 struct node *head;int x;printf( " 輸入你要儲(chǔ)存的數(shù)據(jù): n" ); head=NULL;q=NULL; s
4、canf( "%d",&x);while (x>0)p=( struct node *)malloc( sizeof ( struct node); p->d=x;p->next=NULL;if (head=NULL) head=p; else q->next=p;q=p;scanf( "%d",&x); getchar(); pre(head);return (head);void locate( struct node *head, int x) / 查找鏈表中的數(shù)據(jù) int u=1; p=head;while
5、 (p->next!= NULL) if (p->d=x) break ;else p=p->next;u+;if (p->d!= x)printf( " 無此結(jié)點(diǎn) ");printf( "在鏈表中的位置為: ");printf( "%d",u);void insert( struct node *head, int x, int i) / 插入數(shù)據(jù) p = head; int j=1;q=( struct node *)malloc( sizeof ( struct node); q->d=x;if (
6、i=1) q->next=head;head=q;elsewhile (j<i-1)&&(p->next !=NULL)j+;p=p->next; q->next=p->next;p->next=q;void delet( struct node *head, int i) / 刪除數(shù)據(jù) p=head; int j=1;if (i<0) printf(" 無此位置 ");if (i=1)q=head; head=head->next; free(q);elsewhile (j<i-1) &&
7、amp; (p->next != NULL) p=p->next;j+; q=p->next;p->next=q->next; free(q);void hebing( struct node *x, struct node *y) / 合并兩個(gè)鏈表p=x;q=y;while (p->next!=NULL)p=p->next;p->next=q;pre(x);void paixu( struct node *head) / 對(duì)鏈表中的數(shù)據(jù)進(jìn)行排序 int m,n,i=1,t;p=head;while (p->next!=NULL)p=p-&
8、gt;next;i+;p=head;for (n=i;n>1;n-)p=head;for (m=1;m<n;m+)q=p->next;if (p->d<q->d)t=p->d;p->d=q->d;q->d=t;p=p->next;void caozuo( struct node *head) / 操作界面int m,n; char t;printf("輸入你要的操作:,查找 2,插入3,刪除n");scanf( "%c" ,&t);switch (t)case '1'
9、; :printf( "輸入你要查找的元素的值: n" );scanf( "%d",&m); locate(head,m); break ;case '2' :printf( " 輸入你要插入的元素的值和位置: n" );scanf( "%d",&m);scanf( "%d",&n); insert(head,m,n);pre(head); break ;case '3' :printf( " 輸入你要?jiǎng)h除的元素的位置: n&qu
10、ot; );scanf( "%d",&m); delet(head,m);pre(head); break ;default :printf( "errorn" );void main() / 主函數(shù)char frag= 'y' ,n=NULL;printf("輸入你要建立的第A鏈表中的元素:n");head1=creat();printf("輸入你要建立的第B鏈表中的元素:n"); head2=creat();/ 選擇操作doprintf("選擇你要操作的鏈表A/B或者合并排序操
11、作C: n"); scanf( "%c",&n);getchar();switch (n)case 'A' : caozuo(head1); break ;case 'B' : caozuo(head2); break ;case 'C' :hebing(head1,head2);paixu(head1);pre(head1);break ;default :printf( "errorn" );printf( "n 是否繼續(xù) y/n : n" );scanf( &qu
12、ot;%c",&frag);getchar();while (frag= 'y' );實(shí)驗(yàn) 2 棧和隊(duì)列的基本操作一、實(shí)驗(yàn)?zāi)康呐c基本要求1掌握棧和隊(duì)列的順序存儲(chǔ)和鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu) 2掌握棧和隊(duì)列的特點(diǎn)。3掌握棧和隊(duì)列的基本運(yùn)算。二、實(shí)驗(yàn)條件1硬件:一臺(tái)微型計(jì)算機(jī)2軟件:操作系統(tǒng)和 C 語言系統(tǒng)。三、實(shí)驗(yàn)方法確定存儲(chǔ)結(jié)構(gòu)后,上機(jī)調(diào)試實(shí)現(xiàn)棧和隊(duì)列的基本運(yùn)算四、實(shí)驗(yàn)內(nèi)容1 寫出棧的入棧和出棧的算法2 寫出隊(duì)列的入隊(duì)和出隊(duì)算法。 實(shí)驗(yàn)代碼: #include "stdlib.h" #include "stdio.h"struct n
13、odeint d;struct node *next;int num; struct node *head;void pre()struct node *p;printf( " 鏈表中的數(shù)據(jù)為: n" );p=head;while (p!=NULL)printf( "%5d",p->d);p=p->next;printf( "n" );void creat()struct node *p,*q; int x;printf( " 輸入你要儲(chǔ)存的數(shù)據(jù),輸入負(fù)數(shù)作為結(jié)束:n" );head=NULL;q=NU
14、LL;scanf( "%d",&x);while (x>0) num+;p=( struct node *)malloc( sizeof ( struct node); p->d=x;p->next=NULL;if (head=NULL) head=p;else q->next=p;q=p;scanf( "%d",&x);getchar();pre();void insert( int x, int i)struct node *p,*q;p = head; int j=1;q=( struct node *)ma
15、lloc( sizeof ( struct node); q->d=x;if (i=1) q->next=head; head=q;elsewhile (j<i-1)&&(p->next !=NULL) j+;p=p->next; q->next=p->next;p->next=q;void delet( int i)struct node *p,*q;p=head; int j=1;if (i<0) printf(" 無此位置 ");if (i=1)q=head; head=head->next;
16、 free(q); elsewhile (j<i-1) && (p->next != NULL) p=p->next;j+; q=p->next;p->next=q->next;free(q);void push()int n; num+; printf( " 請(qǐng)輸入數(shù)據(jù): "); scanf( "%d",&n); getchar(); insert(n,num); pre();void pop()delet(num);num-;pre();void rudui()int n; num+;pri
17、ntf( " 請(qǐng)輸入數(shù)據(jù): "); scanf( "%d",&n); getchar();insert(n,1); pre();void chudui() delet(num); num-; pre();void main()char frag= 'y' ; int n; creat();don" );printf( " 選擇你要的操作入棧出棧入隊(duì)出隊(duì): scanf( "%d",&n);getchar(); switch (n) case 1:printf( " 入棧操作
18、");push();printf( "n" ); break ; case 2:printf( " 出棧操作 "); pop();printf( "n" ); break ; case 3:printf( " 入隊(duì)操作 "); rudui();printf( "n" ); break ; case 4:printf( " 出隊(duì)操作 "); chudui();printf( "n" ); break ; default :printf( "
19、;errorn" );printf( "n 是否繼續(xù) y/n : n" ); scanf( "%c",&frag);getchar(); while (frag= 'y' );實(shí)驗(yàn) 3 二叉樹的構(gòu)造一、實(shí)驗(yàn)?zāi)康呐c基本要求熟練掌握二叉樹的構(gòu)造方法。二、實(shí)驗(yàn)條件1硬件:微機(jī)2軟件:操作系統(tǒng)和 C 語言系統(tǒng)三、實(shí)驗(yàn)方法確定存儲(chǔ)結(jié)構(gòu)后,上機(jī)調(diào)試二叉樹的構(gòu)造方法。四、實(shí)驗(yàn)內(nèi)容設(shè)計(jì)一個(gè)讀入一串整數(shù)構(gòu)成一棵二叉樹的程序。(深度至少為 2)實(shí)驗(yàn)代碼:#include <stdio.h>#include <stdlib.
20、h>struct btnode / 二叉樹結(jié)構(gòu)體char d;struct btnode *lchild;struct btnode *rchild;struct btnode *creatbt( struct btnode *bt, int k) / 建立二叉樹 struct btnode *p,*t;t=( struct btnode *)malloc( sizeof ( struct btnode);printf( " 輸入元素 ( 輸入時(shí)結(jié)束所在分枝 ):" ); char b;scanf( "%c",&b);getchar();i
21、f (b!= '0' )p=( struct btnode *)malloc( sizeof ( struct btnode); p->d=b;p->lchild=NULL;p->rchild=NULL;if (k=0)t=p;if (k=1)bt->lchild=p;if (k=2)bt->rchild=p;creatbt(p,1);creatbt(p,2);return t;void pretrav( struct btnode *bt)if (bt!=NULL)printf( "%cn" ,bt->d); pretr
22、av(bt->lchild); pretrav(bt->rchild);return ;void intrav( struct btnode *bt)if (bt!=NULL)intrav(bt->lchild);printf( "%cn" ,bt->d); intrav(bt->rchild);return ;void postrav( struct btnode *bt)if (bt!=NULL)postrav(bt->lchild);postrav(bt->rchild);printf( "%cn" ,bt
23、->d);return ;int main()struct btnode *m; char frag= 'y'/ 前序遍歷/ 中序遍歷/ 后序遍歷int s;doprintf("請(qǐng)選擇n1、建立二叉樹n2、前序遍歷n3、中序遍歷n4、后序遍歷n5、退出n"); scanf( "%d",&s);getchar();switch (s)case 1:m=creatbt(0,0); break ;case 2:pretrav(m); break ;case 3:intrav(m); break ;case 4:postrav(m)
24、; break ;default : printf( "是否繼續(xù) n y/n" );scanf( "%c",&frag); getchar(); break ;while (frag= 'y' );實(shí)驗(yàn) 4 排序的基本操作一、實(shí)驗(yàn)?zāi)康呐c基本要求1掌握常用的排序方法,并用高級(jí)語言實(shí)現(xiàn)排序算法。 2理解排序的定義和各種排序的特點(diǎn)。3了解排序過程以及依據(jù)的原則,并了解各種排序方法的時(shí)間復(fù)雜度分析。二、實(shí)驗(yàn)條件1硬件:一臺(tái)微機(jī)2軟件:操作系統(tǒng)和 C 語言系統(tǒng)三、實(shí)驗(yàn)方法確定存儲(chǔ)結(jié)構(gòu)后,上機(jī)調(diào)試不同的排序方法。四、實(shí)驗(yàn)內(nèi)容1設(shè)計(jì)一待排序的線
25、性表以順序存儲(chǔ)結(jié)構(gòu)存儲(chǔ),試寫出冒泡排序算法。2給出 n 個(gè)學(xué)生的考試成績表, 每條信息由姓名與分?jǐn)?shù)組成, 試設(shè)計(jì)一個(gè)算法: (1)按分?jǐn)?shù)高低次序,打印出每個(gè)學(xué)生在考試中獲得的名次,分?jǐn)?shù)相同的為同 一名次。(2)按名次列出每個(gè)學(xué)生的姓名與分?jǐn)?shù)。實(shí)驗(yàn)代碼 :#include "stdlib.h"#include "stdio.h"struct node / 結(jié)構(gòu)體變量int f;int m;char n;struct node *next;void pre( struct node *head) / 打印數(shù)據(jù) struct node *p;printf( &
26、quot; 鏈表中的數(shù)據(jù)為: n" );printf("名次 姓名 分?jǐn)?shù)n");p=head;while (p!=NULL)printf("%5d",p->m);printf("%5c",p->n);printf("%5d",p->f);printf("n" );p=p->next;printf( "n" );int num;struct node *creat()/ 建立數(shù)據(jù)struct node *head,*q,*p;int x,y,j; char z;printf( "輸入錄入學(xué)生的人數(shù): n" );head=NULL;q=NULL;scanf( "%d",&num);getchar();for (j=0;j<
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 停車場(chǎng)車位租用合同
- 供應(yīng)鏈管理下物流模型的評(píng)估流程
- 船舶與海洋工程焊接作業(yè)指導(dǎo)書
- 內(nèi)墻抹灰班組承包協(xié)議
- 項(xiàng)目辦公用品預(yù)算表
- 房地產(chǎn)轉(zhuǎn)讓合同協(xié)議書
- IT系統(tǒng)使用情況表
- 廠房雨季施工方案
- 西歐經(jīng)濟(jì)崛起的探究:高中世界歷史課教案
- 公共安全領(lǐng)域技術(shù)創(chuàng)新合作計(jì)劃書
- 重點(diǎn)關(guān)愛學(xué)生幫扶活動(dòng)記錄表
- 2024年部編版五年級(jí)下冊(cè)語文第一單元綜合檢測(cè)試卷及答案
- 5-6歲幼兒園小學(xué)美術(shù)PPT課件教案教程創(chuàng)意幼教手工《樹懶》
- 牛津譯林英語七年級(jí)上冊(cè)7AUnits1-4單元復(fù)習(xí)課件
- 《義務(wù)教育道德與法治課程標(biāo)準(zhǔn)(2022年版)》
- 2023北京高三一模語文匯編:非連續(xù)性文本閱讀
- 初中物理核心素養(yǎng)培養(yǎng)
- 保安公司招聘筆試題及答案
- 介紹錢三強(qiáng)的
- 農(nóng)業(yè)資源與環(huán)境經(jīng)濟(jì)學(xué)
- JCT2110-2012 室內(nèi)空氣離子濃度測(cè)試方法
評(píng)論
0/150
提交評(píng)論