




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上 數(shù)據(jù)結(jié)構(gòu)第一次作業(yè)1.源程序#include<stdio.h>#include<malloc.h>#include<string.h>#include <stdlib.h>#include <iostream>typedef structint no;char name100;char Snumber20;char Pnumber20;char QQ20;char sex50; student; /學(xué)生信息的類型 typedef student elemptype;typedef struct nodeel
2、emptype data;struct node *next;linklist; /定義單鏈表 linklist *head; /定義單鏈表頭 int start() /控制linklist*creatlist()的結(jié)束 char n10="end"char str110; printf("結(jié)束輸入end,否則請(qǐng)按回車!nn"); printf("請(qǐng)輸入:");gets(str1);printf("nn"); if(strcmp(n,str1)=0)getchar();return(0); else getchar
3、();return(1);linklist*creatlist() int a; char ch10; elemptype x; linklist *head,*r,*p; p=(linklist*)malloc(sizeof(linklist); head=p; p->next=NULL; r=p; while(1) printf("n請(qǐng)輸入學(xué)號(hào):n"); scanf("%d",&x.no); printf("n請(qǐng)輸入姓名:n"); scanf("%s",&); getchar(
4、); printf("n請(qǐng)輸入專業(yè)n"); scanf("%s",&x.Snumber); getchar(); printf("n請(qǐng)輸入電話號(hào)碼n"); scanf("%s",&x.Pnumber); getchar(); printf("n請(qǐng)輸入QQn"); scanf("%s",&x.QQ); getchar(); printf("n請(qǐng)輸入性別n"); scanf("%s",&x.sex); get
5、char(); p=(linklist*)malloc(sizeof(linklist); p->data=x; p->next=NULL; r->next=p; r=r->next; a=start(); if(a=0)break; system("pause");system("cls"); return(head); void insert(linklist*head,int x,elemptype y)linklist *q,*p,*r;r=(linklist*)malloc(sizeof(linklist); r->
6、;data=y; if(head->next=NULL) head->next=r; r->next=NULL; else q=head;p=head->next; while(p!=NULL)&&(p->data.no!=x) q=p;p=p->next; if(p!=NULL) q->next=r; r->next=p; else q->next=r; r->next=NULL; linklist *del(linklist*head,int i)int j=0;linklist *p,*s,*q;p=head;j
7、=0;while(p->next!=NULL)&&(j<i-1)p=p->next; j+;if(p->next!=NULL)q=p->next;p->next=p->next->next;free(q);else return NULL; s=head; return s; linklist *locate(linklist *head,int k) linklist *s;s=head->next;while(s!=NULL)if(s->data.no!=k) s=s->next; else break; r
8、eturn s;linklist *get(linklist *head,int i)int j;linklist *p;p=head;j=0;while(p->next!=NULL)&&(j<i)p=p->next;j+;if(i=j)&&(i!=0) return p;else return NULL;void outlin(linklist*h) linklist*p; p=h->next; while(p!=NULL) printf("%d,%s,%s,%s,%s,%sn",p->data.no,p-&g
9、t;,p->data.Snumber,p->data.Pnumber,p->data.QQ,p->data.sex); p=p->next; printf("n 輸出結(jié)束 n"); void Menu_one() /主菜單 printf("nnnnnnn");printf(" *n");printf(" 存儲(chǔ)學(xué)生信息的線性表nn");printf(" 1、用單鏈表來(lái)創(chuàng)建n");printf(" 2、用順序表來(lái)創(chuàng)建n");prin
10、tf(" 3、返回n");printf(" *n");printf("nnntt");void Menu_two_2() /次菜單 printf("nnnnnnn");printf(" *n");printf(" 用單鏈表存儲(chǔ)學(xué)生信息nn");printf(" 1、錄入學(xué)生信息(只能錄入一次?。﹏");printf(" 2、往線性表插入一個(gè)信息n");printf(" 3、在線性表中刪除一個(gè)信息n");printf(
11、" 4、按值檢索n");printf(" 5、按序號(hào)檢索n");printf(" 6、遍歷線性表n");printf(" 7、退出n");printf(" *n");printf("nnntt");void main_switch_2(char j)int i;linklist *k; elemptype m,n; int a,b; switch(j) case '1': printf("請(qǐng)?zhí)砑訉W(xué)生信息n"); head=creatlist
12、(); outlin(head); system("pause");system("cls"); break; case '2': printf("n在第幾個(gè)信息之前插入?(輸入學(xué)號(hào))");printf("n請(qǐng)輸入:");scanf("%d",&m.no);printf("nnn準(zhǔn)備插入的新信息nnn");printf("n請(qǐng)輸入學(xué)號(hào):n"); scanf("%d",&n.no); printf(&quo
13、t;n請(qǐng)輸入姓名:n"); scanf("%s",&); printf("n請(qǐng)輸入專業(yè)n"); scanf("%s",&n.Snumber); printf("n請(qǐng)輸入電話號(hào)碼n"); scanf("%s",&n.Pnumber); printf("n請(qǐng)輸入QQn"); scanf("%s",&n.QQ); printf("n請(qǐng)輸入性別n"); scanf("%s"
14、;,&n.sex);insert(head,m.no,n) ;printf("nn新的學(xué)生信息表n"); outlin(head); system("pause");system("cls"); break; case '3': outlin(head);printf("n請(qǐng)輸入要?jiǎng)h除第幾個(gè)信息:n");scanf("%d",&a);printf("n新的信息表n");del(head,a);outlin(head);system("
15、pause");system("cls"); break; case '4':printf("n請(qǐng)輸入你要查找的學(xué)號(hào):n");scanf("%d",&a);k=locate(head,a);printf("%d,%s,%s,%s,%s,%sn",k->data.no,k->,k->data.Snumber,k->data.Pnumber,k->data.QQ,k->data.sex); system("pause&qu
16、ot;);system("cls");break; case '5':printf("n請(qǐng)輸入你要查找的序號(hào):n");scanf("%d",&a);k=get(head,a);printf("%d,%s,%s,%s,%s,%sn",k->data.no,k->,k->data.Snumber,k->data.Pnumber,k->data.QQ,k->data.sex);system("pause");system(&
17、quot;cls"); break; case '6': printf("n學(xué)生信息表nn"); outlin(head); system("pause");system("cls"); break; case '7': exit(0); int main1()char a100; head=(linklist*)malloc(sizeof(linklist); head->next=NULL; /生成頭結(jié)點(diǎn) while(1) Menu_two_2();printf("nt請(qǐng)輸
18、入功能編號(hào):");gets(a);system("pause");system("cls"); if(a1!='0')printf("n輸入錯(cuò)誤n");system("pause");system("cls");continue; elseif(a0='0') break;main_switch_2(a0);return 0; /-以下為順序表的函數(shù)-using namespace std;struct student1 int id; / 學(xué)號(hào) cha
19、r name30; / 姓名 char sex2; / 性別 char Pnumber20; char QQ20; float gread; / 成績(jī);typedef struct student1 DataType; / 指定struct student為DataTypestruct SeqList int MAX; / 順序表中最大元素的個(gè)數(shù) int count; / 存放線性表中元素的個(gè)數(shù)count <= MAXLENGTH DataType *element; / element0, element1, ., elementn - 1存放線性表中的元素 DataType *m;t
20、ypedef struct SeqList *MySeqList; / 初始化并創(chuàng)建空順序表/ 功能:在順序表中求某元素的下標(biāo),沒(méi)有查找到,則返回-1int locateSeqList(MySeqList mySeqList, int id) for (int i = 0; i < mySeqList->count; +i) if (mySeqList->elementi.id = id) / 傳入一個(gè)元素x,查找到后返回下標(biāo)i return (i); return (-1);int updateSeqList(MySeqList mySeqList, int id) /學(xué)生
21、信息修改 int iRc = locateSeqList(mySeqList, id); if (iRc = -1) printf("不存在指定下標(biāo)!n"); return (0); cout<<"姓名:" cin>>mySeqList->elementiR; cout<<"學(xué)號(hào): " cin>>mySeqList->elementiRc.id; cout<<"性別: " cin>>mySeqList->eleme
22、ntiRc.sex; cout<<"電話: " cin>>mySeqList->elementiRc.Pnumber; cout<<"QQ: " cin>>mySeqList->elementiRc.QQ; cout<<"成績(jī): " cin>>mySeqList->elementiRc.gread; return 1; / 功能: 創(chuàng)建空順序表MySeqList initSeqList(int m) MySeqList mySeqList = (
23、MySeqList)malloc(sizeof(struct SeqList); / 分配內(nèi)存空間 if (mySeqList != NULL) mySeqList->element = (DataType*)malloc(sizeof(DataType) * m); / 為里面的元素分配m個(gè)DataType大小的內(nèi)存空間,相當(dāng)于初始化了一個(gè)長(zhǎng)度為m的數(shù)組 if (mySeqList->element) mySeqList->MAX = m; / 如果創(chuàng)建了元素,MAXLENGTH為最大元素的個(gè)數(shù) mySeqList->count = 0; / 空表長(zhǎng)度為0 retur
24、n (mySeqList); else free(mySeqList); / 記得要手動(dòng)釋放空間,否則很容易產(chǎn)生內(nèi)存泄漏 printf("內(nèi)存空間不足,請(qǐng)關(guān)閉一些程序,然后再試!n"); / 存儲(chǔ)分配失敗,提示空間不足 return NULL;/ 功能: 判斷線性表是否為空int isEmptySeqList(MySeqList mySeqList) return (mySeqList->count =0);/ 功能:順序表的pos下標(biāo)后面插入,插入成功返回1,失敗返回0int insertNextSeqList(MySeqList mySeqList, int po
25、s, DataType x) if (pos < 0 | pos >= mySeqList->count) printf("不存在指定下標(biāo)!n"); return (0); +mySeqList->count; if (mySeqList->count >= mySeqList->MAX) -mySeqList->count; printf("表產(chǎn)生了溢出!n"); return (0); for (int i = mySeqList->count - 1; i != pos + 1; -i) myS
26、eqList->elementi = mySeqList->elementi - 1; / 同樣地,把pos+1插入位置及之后的元素均后移一個(gè)位置 mySeqList->elementi = x; / 插入元素x return (1); / 功能:順序表的刪除(根據(jù)下標(biāo)刪除)int deleteSeqList(MySeqList mySeqList, int pos) if (pos < 0 | pos >= mySeqList->count) / 不存在下標(biāo)為pos的元素,注意下標(biāo)范圍是從0到count-1 printf("不存在指定下標(biāo)!n&q
27、uot;); return (0); for (int i = pos; i < mySeqList->count - 1; +i) mySeqList->elementi = mySeqList->elementi + 1; / 被刪除元素之后的元素均前移一個(gè)位置 -mySeqList->count; / 元素個(gè)數(shù)減1 return (1);/ 功能:根據(jù)元素值刪除,實(shí)現(xiàn)順序表的刪除int deleteSeqListByValue(MySeqList mySeqList, int id) int pos = locateSeqList(mySeqList, id
28、); if (pos = -1) printf("不存在指定下標(biāo)!n"); return (0); deleteSeqList(mySeqList, pos); return (1); / 輸出線性表的元素值void printSeqList(MySeqList &mySeqList) for (int i = 0; i < mySeqList->count; +i) / 輸出線性表的元素值 cout<< "學(xué)號(hào):" << mySeqList->elementi.id << ",姓名
29、:" << mySeqList-> << ",性別:" << mySeqList->elementi.sex ; cout<< ",電話:" << mySeqList->elementi.Pnumber<< ",QQ:" << mySeqList->elementi.QQ<<",成績(jī):" << mySeqList->elementi.gread
30、; cout<<endl; cout << endl;/ 根據(jù)學(xué)生id,輸出線性表的元素值void printSeqListById(MySeqList &mySeqList,int id) for (int i = 0; i < mySeqList->count; +i) / 輸出線性表的元素值 if (id = mySeqList->elementi.id) cout<< "學(xué)號(hào):" << mySeqList->elementi.id << ",姓名:" &l
31、t;< mySeqList-> << ",性別:" << mySeqList->elementi.sex ; cout<< ",電話:" << mySeqList->elementi.Pnumber<< ",QQ:" << mySeqList->elementi.QQ<<",語(yǔ)文:" << mySeqList->elementi.gread; cout<
32、<endl; break; int main2()MySeqList mySeqList = initSeqList(20); / 初始化一個(gè)長(zhǎng)20的表L: system("cls"); printf("nnnnnnn");printf(" *n");printf(" 用順序表存儲(chǔ)學(xué)生信息nn");printf(" 1、錄入學(xué)生信息n");printf(" 2、查找學(xué)生信息n");printf(" 3、刪除學(xué)生信息n");printf("
33、4、修改學(xué)生信息n");printf(" 5、遍歷學(xué)生信息n");printf(" 6、退出學(xué)生系統(tǒng)n");printf(" *n");printf("nnntt"); int i; cout<<"請(qǐng)選擇一個(gè)操作(1-5):" cin>>i; system("pause"); system("cls"); if (i = 1) mySeqList->count = 1; int iRc = 0; while(true
34、&&mySeqList->count<20) cout<<endl<<"請(qǐng)?zhí)砑訉W(xué)生信息(輸入*退出添加):"<<endl; cout<<"姓名:" cin>>mySeqList->elementiR; if (strcmp(mySeqList->elementiR,"*") = 0) mySeqList->count-; goto L; cout<<"學(xué)號(hào): " cin>
35、;>mySeqList->elementiRc.id; cout<<"性別: " cin>>mySeqList->elementiRc.sex; cout<<"電話: " cin>>mySeqList->elementiRc.Pnumber; cout<<"QQ: " cin>>mySeqList->elementiRc.QQ; cout<<"成績(jī): " cin>>mySeqList->
36、;elementiRc.gread; cout << "成功添加學(xué)生成績(jī)信息成績(jī)。"<<endl; printSeqList(mySeqList); system("pause"); system("cls"); mySeqList->count+; iRc+; else if (i = 2) L4: cout<<"請(qǐng)輸入要查找的學(xué)生學(xué)號(hào):"<<endl; int sid; cin>>sid; if (locateSeqList(mySeqList,
37、sid) != -1) cout<<"成功查詢學(xué)號(hào)為"<<sid<<"的學(xué)生成績(jī)。"<<endl; printSeqListById(mySeqList,sid); else cout<<"查詢學(xué)生成績(jī)錯(cuò)誤,可能不存在學(xué)號(hào)為"<<sid<<"的學(xué)生."<<endl; int iopselect; cout<<endl<<"還要繼續(xù)查詢嗎?(按0返回主菜單,否則繼續(xù)此操作。)"&
38、lt;<endl; cin>>iopselect; if (iopselect = 0) goto L ; else goto L4; else if (i = 3) L1: cout<<"請(qǐng)輸入要?jiǎng)h除的學(xué)生學(xué)號(hào):"<<endl; int stu_id; cin>>stu_id; if (deleteSeqListByValue(mySeqList,stu_id) = 1) cout<<"成功刪除學(xué)生成績(jī)。"<<endl; else cout<<"刪除學(xué)生
39、成績(jī)出錯(cuò)。"<<endl; printSeqList(mySeqList); int iop; cout<<endl<<"還要繼續(xù)刪除嗎?(按0返回主菜單,否則繼續(xù)此操作。)"<<endl; cin>>iop; if (iop = 0) goto L ; else goto L1; else if (i = 4) L3: cout<<"請(qǐng)輸入要修改的學(xué)生學(xué)號(hào):"<<endl; int id; cin>>id; if(updateSeqList(myS
40、eqList,id) =1) cout << "成功修改學(xué)生成績(jī)信息成績(jī)。"<<endl; else cout << "修改學(xué)生成績(jī)信息成績(jī)出錯(cuò)。"<<endl; printSeqList(mySeqList); int iselect; cout<<endl<<"還要繼續(xù)修改嗎?(按0返回主菜單,否則繼續(xù)此操作。)"<<endl; cin>>iselect; if (iselect = 0) goto L ; else goto L3;
41、else if (i = 5) system("cls"); printSeqList(mySeqList); system("pause"); system("cls"); goto L ; else if (i = 6) system("cls"); cout<<"您已經(jīng)出本系統(tǒng),歡迎下次再使用."<<endl; return 0;int main() int q;Menu_one();printf("請(qǐng)輸入序號(hào):"); scanf("%d",&q);getchar();system("cls");if(q=1)main1();else if(q=2)main2(); elseexit(0); 2. 結(jié)果截圖單鏈表:1錄入信息2 插入信息3 刪除信息4 按值檢索6 按序號(hào)檢索順序表:1 錄入信息2 查找信息3 刪除信息4 修改信息小組成員任務(wù)分配單鏈表: 湛嘉琪,王小雨順序
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 小型煙草公司策劃方案
- 寢室管理月活動(dòng)方案
- 寵物用品公司展會(huì)策劃方案
- 小學(xué)武術(shù)興趣活動(dòng)方案
- 家具店慶促銷活動(dòng)方案
- 客艙特色活動(dòng)方案
- 小學(xué)合唱比賽活動(dòng)方案
- 寵物踏青活動(dòng)方案
- 小學(xué)冬季活動(dòng)創(chuàng)意活動(dòng)方案
- 小學(xué)世界脊柱日活動(dòng)方案
- 二年級(jí)下冊(cè)期末無(wú)紙筆測(cè)評(píng)方案
- CJJ89-2012 城市道路照明工程施工及驗(yàn)收規(guī)程
- 娛樂(lè)場(chǎng)所突發(fā)事件應(yīng)急處理
- 2024年信息科技中考考試題庫(kù)及答案(模擬)
- 2023年新疆維吾爾自治區(qū)烏魯木齊市天山區(qū)小升初數(shù)學(xué)試卷(內(nèi)含答案解析)
- 20G520-1-2鋼吊車梁(6m-9m)2020年合訂本
- 2023年陜西初中地理生物會(huì)考卷子
- 電梯維護(hù)保養(yǎng)規(guī)則(TSG T5002-2017)
- 初中物理-摩擦力課件-市公開(kāi)課一等獎(jiǎng)省賽課獲獎(jiǎng)?wù)n件
- (高清版)TDT 1057-2020 國(guó)土調(diào)查數(shù)據(jù)庫(kù)標(biāo)準(zhǔn)
- 上半年學(xué)校安全穩(wěn)定工作總結(jié)
評(píng)論
0/150
提交評(píng)論