數(shù)據(jù)結(jié)構(gòu)順序表及鏈表實(shí)驗(yàn)
來(lái)源:程序員人生 發(fā)布時(shí)間:2016-09-29 08:18:25 閱讀次數(shù):2993次
實(shí)驗(yàn)?zāi)康?/span> :
1
、掌握線性表的定義;
2
、掌握線性表的基本操作,如建立、查找、插入和刪除等。
實(shí)驗(yàn)內(nèi)容:
定義1個(gè)包括學(xué)生信息(學(xué)號(hào),姓名,成績(jī))的的
順序表和鏈表,使其具有以下功能:
(1) 根據(jù)指定學(xué)生個(gè)數(shù),逐一輸入學(xué)生信息;
(2) 逐一顯示學(xué)生表中所有學(xué)生的相干信息;
(3) 根據(jù)姓名進(jìn)行查找,返回此學(xué)生的學(xué)號(hào)和成績(jī);
(4) 根據(jù)指定的位置可返回相應(yīng)的學(xué)生信息(學(xué)號(hào),姓名,成績(jī));
(5) 給定1個(gè)學(xué)生信息,插入到表中指定的位置;
(6) 刪除指定位置的學(xué)生記錄;
(7) 統(tǒng)計(jì)表中學(xué)生個(gè)數(shù)。
參考信息 :
Definition of structure student
:
typedef struct {
char no[8]; //8
位學(xué)號(hào)
char name[20]; //
姓名
int price; //
成績(jī)
}Student;
Definition of sequential list:
typedef struct {
Student *elem; //
指向數(shù)據(jù)元素的基地址
int length; //
線性表確當(dāng)前長(zhǎng)度
}SqList
;
Definition of linked list
:
typedef struct LNode{
Student data; //
數(shù)據(jù)域
struct LNode *next; //
指針域
}LNode,*LinkList;
實(shí)驗(yàn)要求 :
(1) 程序要添加適當(dāng)?shù)淖⑨專绦虻臅鴮懸扇?/span>
縮進(jìn)格式
。
(2) 程序要具在1定的
硬朗性,即當(dāng)輸入數(shù)據(jù)非法時(shí), 程序也能適當(dāng)?shù)刈龀龇磻?yīng),如
插入刪除時(shí)指定的位置不對(duì)
等等。
(3) 程序要做到
界面友好,在程序運(yùn)行時(shí)用戶可以根據(jù)相應(yīng)的提示信息進(jìn)行操作。
(4) 根據(jù)實(shí)驗(yàn)報(bào)告模板詳細(xì)書寫實(shí)驗(yàn)報(bào)告,在實(shí)驗(yàn)報(bào)告中給出鏈表
根據(jù)姓名進(jìn)行查找的算法和插入算法的流程圖
。
(5) 上傳源程序到Saike網(wǎng)絡(luò)教學(xué)平臺(tái)。順序表的源程序保存為
SqList.c
,鏈表的源程序保存為 LinkList.c
。
鏈表實(shí)現(xiàn):
//鏈表實(shí)現(xiàn)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
typedef struct {
char name[20]; //姓名
char nu[8]; //8位學(xué)號(hào)
int price; //成績(jī)
} Student;
typedef struct LNode {
Student data; //數(shù)據(jù)域
struct LNode *next; //指針域
} LNode,*LinkList;
LNode L;
LinkList q;
void Input() {
printf(" 請(qǐng)輸入要錄入的學(xué)生信息(包括姓名(cstring),學(xué)號(hào)(cstring),成績(jī)(int))個(gè)數(shù):");
int num,k=0;
LinkList p;
scanf("%d",&num);
while(num--) {
p=(LNode *)malloc(sizeof(LNode));
printf("請(qǐng)輸入第%d個(gè)學(xué)生的信息:",++k);
scanf("%s%s%d",p->data.name,p->data.nu,&p->data.price);
p->next=NULL;
q=&L;
while(q->next!=NULL) {
q=q->next;
// printf("---\n");
}
q->next=p;
}
printf("錄入終了\n");
}
void Output() {
q=L.next;
printf("姓名 學(xué)號(hào) 成績(jī)\n");
while(q->next!=NULL) {
printf("%s %s %d\n",q->data.name,q->data.nu,q->data.price);
q=q->next;
}
printf("%s %s %d\n",q->data.name,q->data.nu,q->data.price);
}
void search() {
printf("請(qǐng)輸入學(xué)生姓名(輸入⑴結(jié)束查詢):");
char c[20];
int flag=0;
q=L.next;
while(scanf("%s",c)&&c[0]!='-') {
flag=0;
while(q->next!=NULL) {
if(strcmp(c,q->data.name)==0) {
printf("姓名:%s 學(xué)號(hào):%s 成績(jī):%d\n",q->data.name,q->data.nu,q->data.price);
flag=1;
break;
}
q=q->next;
}
if(q->next==NULL&&strcmp(c,q->data.name)==0) {
printf("姓名:%s 學(xué)號(hào):%s 成績(jī):%d\n",q->data.name,q->data.nu,q->data.price);
flag=1;
}
if(!flag)
printf("不存在該學(xué)生的信息\n");
printf("請(qǐng)輸入學(xué)生姓名(輸入⑴結(jié)束查詢):");
}
printf("查詢結(jié)束\n");
}
void find() {
printf("輸入要查找第幾個(gè)學(xué)生的信息:");
int k,step=0,cnt=0;
scanf("%d",&k);
q=L.next;
while(q->next!=NULL) {
step++;
if(step==k) {
printf("姓名:%s 學(xué)號(hào):%s 成績(jī):%d\n",q->data.name,q->data.nu,q->data.price);
cnt=1;
break;
}
q=q->next;
}
if(step==k⑴) {
printf("姓名:%s 學(xué)號(hào):%s 成績(jī):%d\n",q->data.name,q->data.nu,q->data.price);
cnt=1;
}
if(!cnt)
printf("不存在此學(xué)生信息\n");
}
void insert() {
LinkList p;
p=(LNode *)malloc(sizeof(LNode));
printf("請(qǐng)輸入要插入的學(xué)生信息:");
scanf("%s%s%d",p->data.name,p->data.nu,&p->data.price);
int k,step=0,cnt=0;
printf("請(qǐng)輸入要插入的第幾個(gè)位置:");
scanf("%d",&k);
q=L.next;
while(q->next!=NULL) {
step++;
if(step==k⑴) {
cnt=1;
break;
}
q=q->next;
}
if(step==k⑴) {
p->next=q->next;
q->next=p;
}
if(step==k⑵) {
p->next=NULL;
q->next=p;
cnt=1;
}
if(!cnt) {
printf("位置輸入過(guò)大\n");
}
printf("插入終了\n");
}
void cutout() {
int k,step=0,cnt=0;
LinkList p;
printf("請(qǐng)輸入要?jiǎng)h除第幾個(gè)學(xué)生信息:");
scanf("%d",&k);
q=L.next;
while(q->next!=NULL) {
step++;
if(step==k⑴) {
cnt=1;
break;
}
q=q->next;
}
if(step==k⑴) {
p=q->next;
q->next=p->next;
free(p);
}
if(!cnt)
printf("位置輸入過(guò)大\n");
printf("刪除終了\n");
}
void statistics() {
int step=0;
q=L.next;
while(q->next!=NULL) {
step++;
q=q->next;
}
printf("統(tǒng)計(jì)表中的學(xué)生個(gè)數(shù)是%d\n",step+1);
}
void began() {
printf("學(xué)生成績(jī)管理系統(tǒng):\n");
printf("********************************************************************************\n");
printf(" 1.輸入學(xué)生信息\n");
printf(" 2.顯示學(xué)生表中的學(xué)生相干信息\n");
printf(" 3.根據(jù)姓名查找學(xué)生學(xué)號(hào)和成績(jī)\n");
printf(" 4.得到指定位置相應(yīng)的學(xué)生信息\n");
printf(" 5.插入學(xué)生信息\n");
printf(" 6.刪除指定位置的學(xué)生記錄\n");
printf(" 7.統(tǒng)計(jì)表中的學(xué)生個(gè)數(shù)\n");
printf(" 8.安全退出\n\n");
printf("********************************************************************************\n");
printf("請(qǐng)輸入要實(shí)現(xiàn)的功能編號(hào)^.^:");
}
int main() {
began();
int x;
while(scanf("%d",&x)&&x!=8) {
switch(x) {
case 1: {
Input();
break;
}
case 2: {
Output();
break;
}
case 3: {
search();
break;
}
case 4: {
find();
break;
}
case 5: {
insert();
break;
}
case 6: {
cutout();
break;
}
case 7: {
statistics();
break;
}
}
system("pause");
system("cls");
began();
}
printf("安全退出\n");
return 0;
}
順序表實(shí)現(xiàn):
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<malloc.h>
#include<algorithm>
using namespace std;
const int N = 105;
typedef struct {
char name[20]; //姓名
char nu[8]; //8位學(xué)號(hào)
int price; //成績(jī)
} Student;
typedef struct {
Student *elem;
int length;
} SqList;
SqList L;
void build() {
L.elem=(Student *)(malloc(sizeof(Student)*N)); //建立線性表
L.length=0;
if(!L.elem)
printf("建立順序表失敗\n");
}
void Input() { //輸入
int y,k,p=0;
printf("請(qǐng)輸入要讀入的學(xué)生信息(包括姓名(cstring),學(xué)號(hào)(cstring),成績(jī)(int)) 數(shù)目:");
scanf("%d",&y);
for(int i=0; i<y; i++) {
if(i>=100) {
printf("內(nèi)存超限\n");
return ;
}
printf("請(qǐng)輸入第%d個(gè)學(xué)生信息:",++p);
scanf("%s%s%d",L.elem[i].name,L.elem[i].nu,&L.elem[i].price);
L.length++;
}
printf("建立學(xué)生成績(jī)表完成\n");
}
void Output() { //輸出
printf("姓名 學(xué)號(hào) 成績(jī)\n");
for(int i=0; i<L.length; i++) {
printf("%s %s %d\n",L.elem[i].name,L.elem[i].nu,L.elem[i].price);
}
//printf("按回車鍵繼續(xù)\n");
}
void search() { //按姓名查找
printf("輸入要查找的學(xué)生姓名:");
char tem[20];
scanf("%s",tem);
for(int i=0; i<L.length; i++) {
if(!strcmp(L.elem[i].name,tem)) {
printf("姓名:%s 學(xué)號(hào):%s 成績(jī):%d\n",L.elem[i].name,L.elem[i].nu,L.elem[i].price);
return ;
}
}
printf("沒(méi)有此學(xué)生信息\n");
}
void find() { //按位置查找
printf("輸入要查找第幾個(gè)學(xué)生的信息:");
int temp;
scanf("%d",&temp);
printf("姓名:%s 學(xué)號(hào):%s 成績(jī):%d\n",L.elem[temp⑴].name,L.elem[temp⑴].nu,L.elem[temp⑴].price);
}
void insert() { //插入
int num,p=0;
Student e;
printf("輸入你想插入的學(xué)生數(shù)量:");
scanf("%d",&num);
if(L.length+num>=100) {
printf("存儲(chǔ)不夠,插入失敗\n");
return ;
}
while(num--) {
int s;
printf("輸入要插入的第%d個(gè)學(xué)生的姓名、學(xué)號(hào)、成績(jī):",++p);
scanf("%s%s%d",e.name,e.nu,&e.price);
printf("輸入要插入第幾個(gè)位置:");
scanf("%d",&s);
for(int i=L.length; i>s⑴; i--) {
L.elem[i]=L.elem[i⑴];
}
L.elem[s⑴]=e;
L.length++;
}
printf("插入成功\n");
}
void cutout() { //刪除
int num;
printf("輸入共刪除幾個(gè)學(xué)生信息:");
scanf("%d",&num);
while(num--) {
int s;
printf("輸入刪除第幾個(gè)學(xué)生信息:");
scanf("%d",&s);
if(s>=L.length) {
printf("沒(méi)有此學(xué)生的信息\n");
continue;
}
for(int i=s⑴; i<L.length⑴; i++) {
L.elem[i]=L.elem[i+1];
}
L.length--;
}
printf("刪除終了\n");
}
void statistics() { //表中的學(xué)生信息個(gè)數(shù)
printf("表中的學(xué)生信息個(gè)數(shù)是:%d\n",L.length);
}
void began() {
printf("學(xué)生成績(jī)管理系統(tǒng):\n");
printf("********************************************************************************\n");
printf(" 1.輸入學(xué)生信息\n");
printf(" 2.顯示學(xué)生表中的學(xué)生相干信息\n");
printf(" 3.根據(jù)姓名查找學(xué)生學(xué)號(hào)和成績(jī)\n");
printf(" 4.得到指定位置相應(yīng)的學(xué)生信息\n");
printf(" 5.插入學(xué)生信息\n");
printf(" 6.刪除指定位置的學(xué)生記錄\n");
printf(" 7.統(tǒng)計(jì)表中的學(xué)生個(gè)數(shù)\n");
printf(" 8.安全退出\n\n");
printf("********************************************************************************\n");
printf("請(qǐng)輸入要實(shí)現(xiàn)的功能編號(hào)^.^:");
}
int main() {
began();
build();
int x;
while(scanf("%d",&x)&&x!=8) {
switch(x) {
case 1: {
Input();
break;
}
case 2: {
Output();
break;
}
case 3: {
search();
break;
}
case 4: {
find();
break;
}
case 5: {
insert();
break;
}
case 6: {
cutout();
break;
}
case 7: {
statistics();
break;
}
}
system("pause");
system("cls");
began();
}
printf("退出成功\n");
return 0;
}
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)