您的位置:首页 > 理论基础 > 数据结构算法

数据结构实验报告---链表创建输出,学生信息系统

2019-01-29 19:42 579 查看

上机实验习题
上机实验要求及规范
C程序设计具有比较强的理论性,同时也具有较强的可应用性和实践性。在上机实验是一个重要的教学环节。具体实习步骤如下:
1.问题分析与系统结构设计
充分地分析和理解问题本身,弄清要求做什么(而不是怎么做),限制条件是什么。按照以数据结构为中心的原则划分模块,搞清数据的逻辑结构(是线性表还是树、图?),确定数据的存储结构(是顺序结构还是链表结构?)。然后设计有关操作的函数。在每个函数模块中,要综合考虑系统功能,使系统结构清晰、合理、简单和易于调试。最后写出每个模块的算法头和规格说明,列出模块之间的调用关系(可以用图表示),便完成了系统结构设计。
3.上机准备
熟悉高级语言用法,如C语言。熟悉机器(即操作系统),基本的常用命令。静态检查主要有两条路径,一是用一组测试数据手工执行程序(或分模块进行);二是通过阅读或给别人讲解自己的程序而深入全面地理解程序逻辑,在这个过程中再加入一些注释和断言。如果程序中逻辑概念清楚,后者将比前者有效。
4.上机调试程序
调试最好分块进行,自底向上,即先调试底层函数,必要时可以另写一个调用驱动程序,表面上的麻烦工作可以大大降低调试时所面临的复杂性,提高工作效率。
5.整理实习报告
在上机实开始之前要充分准备实验数据,在上机实践过程中要及时记录实验数据,在上机实践完成之后必须及时总结分析。写出实验报告。
一、实验报告的基本要求:
一般性、较小规模的上机实验题,必须遵循下列要求。养成良好的习惯。
(1)姓名 班级 学号 日期
(2)题目:内容叙述
(3)程序清单(带有必要的注释)
(4)调试报告:
实验者必须重视这一环节,否则等同于没有完成实验任务。这里可以体现个人特色、或创造性思维。具体内容包括:测试数据与运行记录;调试中遇到的主要问题,自己是如何解决的;经验和体会等。
二、实验习报告的提高要求:
阶段性、较大规模的上机实验题,应该遵循下列要求。养成科学的习惯。
(1)需求和规格说明
描述问题,简述题目要解决的问题是什么。规定软件做什么。原题条件不足时补全。
(2)设计
a.设计思想:存储结构(题目中限定的要描述);主要算法基本思想。
b.设计表示:每个函数的头和规格说明;列出每个函数所调用和被调用的函数,也可以通过调用关系图表达。
c.实现注释:各项功能的实现程度、在完成基本要求的基础上还有什么功能。
(3)用户手册:即使用说明。
(4)调试报告:调试过程中遇到的主要问题是如何解决的;设计的回顾、讨论和分析;时间复杂度、空间复杂度分析;改进设想;经验和体会等。

编制部门:国土资源学院 编制人:李谦 审核人: 编制日期:2017年3月23日
项目编号 No. C001
项目名称 数据结构
训练对象 GIS 2015级7、8
学时 4
课程名称 数据结构实验
教 材 数据结构(C语言版)

目的
Objective 1.学会用算法来进行编程。
第一题
建立一个学生信息录入系统,要求:录入学生的姓名,学号,性别,年龄,成绩,地址
并输出。
第二题
创建两个链表,给每个链表赋值,并合并输出
第三题
为学生信息创建链表,并输出
第一题:

程序代码如下:
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#define LEN sizeof (struct stu)

struct stu
{
char num[100];
char name[50];
char sex[3];
char age[100];
char score[100];
char adre[50];
struct stu *next;
};  /*定义学生信息链表的结点结构*/
struct stu *creat(void)
{
struct stu *p1,*p2,*head;
head=NULL;
int i=0;
p1=(struct stu*)malloc(LEN);
while(1)
{
i++;
scanf("%s",p1->num);
if(strcmp(p1->num,"end")==0)
break;
else
{
scanf("%s",p1->name);
scanf("%s",p1->sex);
scanf("%s",p1->age);
scanf("%s",p1->score);
scanf("%s",p1->adre);
p1->next=NULL;
if(head==NULL)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct stu*)malloc(LEN);
}
}
return head;
}
struct stu *swap(struct stu *head)
{
struct stu *p,*q,*t;
p=head;
q=NULL;
while(p!=NULL)
{
t=p->next;
p->next=q;
q=p;
p=t;
}
return q;
}//单项链表逆转函数
void print(struct stu *p)
{
struct stu *p1;
p1=p;
while(p1!=NULL)
{
printf("%s %s %s %s %s %s\n",p1->num,p1->name,p1->sex,p1->age,p1->score,p1->adre);
p1=p1->next;
}
}
int main()
{
struct stu *p,*head;
printf("****************************************************************\n");
printf("*      Welcome to use the system of the students' informations *\n");
printf("****************************************************************\n");
printf("*      Please input the informations of students:              *\n");
printf("*                   1:Student's number                         *\n");
printf("*                   2:Student's name                           *\n");
printf("*                   3:Student's sex                            *\n");
printf("*                   4:Student's age                            *\n");
printf("*                   5:Student's score                          *\n");
printf("*                   6:Student's address                        *\n");
printf("*                   7:quit the system(input 'end')             *\n");
printf("****************************************************************\n");
printf("*    number\tname\tsex\tage\tscore\taddress                    *\n");/*添加一个学生信息*/
head=creat();
p=swap(head);
printf("*           output the informations of students                *\n");
printf("*    number\tname\tsex\tage\tscore\taddress                    *\n");
print(p);
return 0;
}

第二题

#include <stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

void print(struct node *h)
{
struct node *p;
p=h->next;
while(p!=NULL)
{
printf("->%d",p->data);
p=p->next;
}
printf("\n\n");
}

struct node* order(struct node *head)
{
struct node *h,*p,*q;
int i,n,chg;
printf("输入当前排序链表长度:");
scanf("%d",&n);
h=head;
for(i=0;i<n-1;i++)
for(q=h->next,p=q->next;p!=NULL;q=p,p=p->next)
if(q->data>p->data)
{
chg=p->data;
p->data=q->data;
q->data=chg;
}
printf("排序后的链表为:");
print(h);
return h;
}

struct node* creat()
{
int i,n;
struct node *p,*q,*h;

printf("输入要创建的链表长度:");
scanf("%d",&n);
h=(struct node*)malloc(sizeof(struct node));
h->data=n;
h->next=NULL;

q=h;
for(i=0;i<n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
printf("input num[%d]:",i);
scanf("%d",&p->data);
p->next=NULL;
q->next=p;
q=p;
}
printf("创建的链表为:");
print(h);
return h;
}

struct node* join(struct node *head1,struct node *head2)
{
struct node *h,*p,*p1,*p2;
h=(struct node *)malloc(sizeof(struct node));
h->data=0;
h->next=NULL;
p=h;
p1=head1->next;
p2=head2->next;
while(p1&&p2)
{
if(p1->data<p2->data)
{
p->next=p1;
p=p1;
p1=p1->next;
}
else
{
p->next=p2;
p=p2;
p2=p2->next;
}
}
p->next=p1?p1:p2;
printf("合并后的链表为:");
print(h);
return h;
}
void main()
{
struct node *head,*head1,*head2;
head1=creat();
hea
20000
d1=order(head1);
head2=creat();
head2=order(head2);
head=join(head1,head2);
}

第三题

#include <stdio.h>
#include <stdlib.h>
#define NULL 0
typedef struct
{
int num;
int grade;
}stu;

typedef struct node
{
stu data;
struct node *next;
}node;
node *p,*head,*q;

void main()
{
int n;
int i;
printf("please enter the number of student:\n");
scanf("%d",&n);
head=(node*)malloc(sizeof(node));
head->next=NULL;
q=head;
for(i=0;i<n;i++)
{
printf("请输入第%d个学生的信息:\n",i+1);
p=(node*)malloc(sizeof(node));
scanf("%d %d",&p->data.num,&p->data.grade);
q->next=p;
q=q->next;
}
p->next=NULL;
printf("学生信息链``表为:\n");
q=head;
q=q->next;
while(q)
{
printf("%d  %d\n",q->data.num,q->data.grade);
q=q->next;
}
}

心得体会:结构体和链表的创建之间的联系,链表的存储和输出的格式是什么样子的也比在之前更加的清楚了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: