数据结构实验(二) 线性表

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

南通大学
数据结构
实验(二)线性表
姓名:金凯
班级:软件工程121
学号: 1102052019
时间:2013年10月12日
1.试验目的
1. 掌握线性表的逻辑特性。

2. 分析问题的特性,选择合适的存储结构。

3. 根据存储结构的特性,设计算法。

4. 认真学习《数据结构实践教程》P403 附录 A“实验报告示例”,掌握该课程实验报告要求,写出合格的报告。

2、实验任务
1.实验内容选项
(1)实验书P339 2.4 一元多项式求导问题
(2)用线性表实现一个通讯录的基本操作:创建、插入记录、删除记录、添加记录、查找记录、浏览记录。

2.按要求形成实验报告(电子文档)。

3.将实验报告(电子文档)、源程序与运行截图打包形成文件:
实验二 - 软件工程1102052019-金凯.zip
3、问题描述
序号求解问题
1.
求一元多项式P n(x)=P0+P1x+ P2x2+```+ P n x n的一阶导数。

2.
用线性表实现一个通讯录的基本操作:创建、插入记录、删
除记录、添加记录、查找记录、浏览记录。

4、数据结构设计
①问题一求一元多项式P n(x)=P0+P1x+ P2x2+```+ P n x n的一阶导数。

一元多项式是个和式,其中每一项由系数和指数幂确定。

因此,
可用下列线性表来表示:((p1,e1),(p2,e2),……(p n,e n))。

线性表的每个元素含有两个数据项:一个数据p,另一个指数幂e。

存储结构链式,顺序式均可。

若为链式存储,结点结构如下:
coef exp next
其中:coef为系数域,存放非零项的系数;
Exp为指数域,存放非零项的指数;
next为指针域,存放指向下一结点的指针。

②问题二用线性表实现一个通讯录的基本操作:创建、插入记录、删除记录、添加记录、查找记录、浏览记录。

通讯录可以看成一个单向链表,其中每一个结点就是一个人名和电话数据。

这里采用链式存储,结点结构如下:
name[10] number next
其中:name为联系人的姓名,存放的是字符串;
Number为联系人的电话号码,存放的是double型的数据。

5、算法设计
①问题一求一元多项式P n(x)=P0+P1x+ P2x2+```+ P n x n的一阶导数。

Step 1:初始化
1:结构体定义:
struct node
{
float coef; //定义系数
int exp; //定义指数
node *next; //指针域
};
2:定义空链表:
node * Head_Create() //定义首结点{
node *Head;
Head=new node;
Head->next=NULL;
return Head;
}
3:删除链表:
void deletechain(node *h) //释放内存{
node *p;
while(h)
{
p=h;
h=h->next;
delete p;
}
h=NULL;
}
4:多项式显示:
node * show (node*Head)
{
node *p;
p=Head->next;
while(p!=NULL)
{
cout<<p->coef<<"x^"<<p->exp<<'\t';
p=p->next;
}
return Head;
}
5:多项式建立:
node *create (node *Head)
{
Node *p;
int a;
float b;
Head=NULL;
while(a)
{
p=new node;
cout<<"输入系数和指数:"<<endl;
cin>>a>>b;
p->coef=a;
p->exp=b;
p->next=Head;
Head=p;
}
return Head;
}
6:函数求导表示:
Node * root (node*Head)
{
node *p;
p=Head->next ;
while(p!=NULL)
{
if(p->exp!=0) //系数不为零时进行求导
{
p->coef=p->coef*p->exp;
p->exp=p->exp-1; //求导后系数变为原来的系数乘以指数,
指数变为原来的指数减一
}
else p->coef=0;
p=p->next;
}
return Head;
}
②问题二用线性表实现一个通讯录的基本操作:创建、插入记录、删除记录、添加记录、查找记录、浏览记录。

1.建立一个链表来存放数据:
struct node { //创建链表
char name[10];
double number;
node *next;
};
2.插入记录的函数,用来初始化链表:
node *new_chain(){ //插入记录
node *h =NULL,*p,*tail;
cout<<"初始化通讯录时插入的记录是:"<<endl;
cout<<"姓名"<<'\t'<<"电话"<<endl;
p=new node;
cout<<strcpy(p->name,"Tom")<<'\t';
p->number=123;
cout<<p->number<<endl;
tail = h =p;
p=new node;
cout<<strcpy(p->name,"Jerry")<<'\t';
p->number=456;
cout<<p->number<<endl;
tail->next = p,tail = p;
tail->next = NULL;
return h;
}
3.添加记录,可以添加任意多个数据:
node *input(node *h){ //添加记录
node *p,*tail;
int i,n;
cout<<"输入想添加的联系人个数:";
cin>>n;
cout<<"姓名"<<'\t'<<"电话"<<endl;
for(i=0;i<n;i++)
{
p=new node;
cin>>p->name>>p->number;
if(!h)tail = h = p;
else tail->next = p,tail = p;
}
if(h)tail->next = NULL;
cout<<endl;
return h;
}
4.查找某个联系人的函数,这里附加了一个判断联系人总数的函数:
int chain_length(const node *head){
int num=1;
while(head)
{
head=head->next;
num++;
}
return num;
}
void search_people(const node *head){ //查找第i个记录int num=1,i;
cout<<"您想查询第几个数据?i=";
cin>>i;
if(i>0&&i<chain_length(head)){
while(head)
{
if(num == i){
cout<<"第"<<i<<"个数据是:";
cout<<head->name<<'\t'<<head->number<<endl;
}
head=head->next;
num++;
}
cout<<endl;
}
else{
cout<<endl<<"超出范围,请重新输入!"<<endl<<endl;
search_people(head);
}
}
5.删除某一个特定的联系人,根据输入的名字来判别:
node *delete_one_node(node *head){ //删除某一个特定数据
char people[10];
cout<<"请输入你要删除的人名:";
cin>>people;
node *p1,*p2;
if(head == NULL){
cout<<"链表为空,无法删除!";
return NULL;
}
if(!strcmp(head->name, people)){
cout<<"已删除了"<<head->name<<'\t'<<head->number<<"的数据";
p1 = head;
head = head->next;
delete p1;
}
else{
p1 = head;p2 = p1->next;
while(strcmp(p2->name, people) && p2->next != NULL){
p1=p2;p2 = p2->next;
}
if(!strcmp(p2->name, people)){
p1->next = p2->next;
cout<<"已删除了"<<p2->name<<'\t'<<p2->number<<"的数据";
delete p2;
}
else cout<<"没有找到要删除的数据!"<<endl;
}
cout<<endl;
}
6.输出通讯录中的所有记录,即:遍历链表:
void output(const node *head){ //输出通讯录中的记录
cout<<"通讯录里的数据是:"<<endl;
while(head)
{
cout<<head->name<<'\t'<<head->number<<endl;
head=head->next;
}
cout<<endl;
}
7.释放链表空间:
void deletechain(node *h){ //释放结点空间
node *p;
while(h){
p=h;
h=h->next;
delete p;
}
}
6.运行与测试
------------------------------①问题一程序代码:---------------------------
#include <iostream>
using namespace std;
struct node
{
float coef; //定义系数
int exp; //定义指数
node *next; //指针域
};
node * Head_Create() //定义首结点
{
node *Head;
Head=new node;
Head->next=NULL;
return Head;
}
void deletechain(node *h) //释放内存
{
node *p;
while(h)
{
p=h;
h=h->next;
delete p;
}
h=NULL;
}
node *create (node *Head) //多项式建立
{
node *p;
float a;
int b;
Head=NULL;
while(a)
{
p=new node;
cout<<"输入系数和指数:"<<endl;
cin>>a>>b;
p->coef=a;
p->exp=b;
p->next=Head;
Head=p;
}
return Head;
}
node * root (node*Head) //求导后的结果
{
node *p;
p=Head->next ;
while(p)
{
if(p->exp!=0) //系数不为零时进行求导
{
p->coef=(p->coef)*(p->exp);
p->exp=p->exp-1;
}
else p->coef=0;
p=p->next;
}
return Head;
}
node * show (node *Head) //多项式的输出
{
int i=0;
node *p;
p=Head->next;
while(p!=NULL)
{
if(i!=0){
cout<<"+";
}
if(p->coef<0){
cout<<'('<<p->coef<<')'<<"x^"<<p->exp;
}
else if(p->coef>0){
cout<<p->coef<<"x^"<<p->exp;
}
else{
cout<<"0";
}
p=p->next;
i=1;
}
return Head;
}
int main(void)
{
node *Head;
Head= Head_Create();
Head=create(Head);
cout<<"原函数输出为:"<<"f(x)=";
show(Head);
Head=root(Head);
cout<<endl<<endl<<"求导后函数输出:"<<"f(x)=";
show(Head);
cout<<endl<<endl;
deletechain(Head);
cout<<"-----------程序测试BY:金凯----------";
cout<<endl<<endl;
return 0;
}
------------------------------②问题二程序代码:--------------------------- #include<iostream>
using namespace std;
struct node { //创建链表
char name[10];
double number;
node *next;
};
node *new_chain(){ //插入记录
node *h =NULL,*p,*tail;
cout<<"初始化通讯录时插入的记录是:"<<endl;
cout<<"姓名"<<'\t'<<"电话"<<endl;
p=new node;
cout<<strcpy(p->name,"Tom")<<'\t';
p->number=123;
cout<<p->number<<endl;
h = tail = p;
p=new node;
cout<<strcpy(p->name,"Jerry")<<'\t';
p->number=456;
cout<<p->number<<endl;
tail->next = p,tail = p;
tail->next = NULL;
return h;
}
node *input(node *h){ //添加记录
node *p,*tail;
int i,n;
cout<<"输入想添加的联系人个数:";
cin>>n;
cout<<"姓名"<<'\t'<<"电话"<<endl;
for(i=0;i<n;i++)
{
p=new node;
cin>>p->name>>p->number;
if(!h)tail = h = p;
else tail->next = p,tail = p;
}
if(h)tail->next = NULL;
cout<<endl;
return h;
}
int chain_length(const node *head){
int num=1;
while(head)
{
head=head->next;
num++;
}
return num;
}
void search_people(const node *head){ //查找第i个记录int num=1,i;
cout<<"您想查询第几个数据?i=";
cin>>i;
if(i>0&&i<chain_length(head)){
while(head)
{
if(num == i){
cout<<"第"<<i<<"个数据是:";
cout<<head->name<<'\t'<<head->number<<endl;
}
head=head->next;
num++;
}
cout<<endl;
}
else{
cout<<endl<<"超出范围,请重新输入!"<<endl<<endl;
search_people(head);
}
}
node *delete_one_node(node *head){ //删除某一个特定数据char people[10];
cout<<"请输入你要删除的人名:";
cin>>people;
node *p1,*p2;
if(head == NULL){
cout<<"链表为空,无法删除!";
return NULL;
}
if(strcmp(head->name, people)==0){
cout<<"已删除了"<<head->name<<'\t'<<head->number<<"的数据";
p1 = head;
head = head->next;
delete p1;
}
else{
p1 = head;p2 = p1->next;
while(strcmp(p2->name, people) && p2->next != NULL){
p1=p2;p2 = p2->next;
}
if(!strcmp(p2->name, people)){
p1->next = p2->next;
cout<<"已删除了"<<p2->name<<'\t'<<p2->number<<"的数据";
delete p2;
}
else cout<<"没有找到要删除的数据!"<<endl;
}
cout<<endl<<endl;
return head;
}
void output(const node *head){ //输出通讯录中的记录
cout<<"通讯录里的数据是:"<<endl;
while(head)
{
cout<<head->name<<'\t'<<head->number<<endl;
head=head->next;
}
cout<<endl;
}
void deletechain(node *h){ //释放结点空间
node *p;
while(h){
p=h;
h=h->next;
delete p;
}
}
int main()
{
node *head =NULL;
head = new_chain();
head = input(head);
output(head);
search_people(head);
head = delete_one_node(head);
output(head);
deletechain(head);
return 0;
}
7.试验总结及收获
通过本次试验,我学会正确使用单链表,懂得在程序中多项式求导的表示方法。

同时,我也明白了多项式的建立及其显示。

在调试阶段,我当建立链表后,应在使用结束时删除链表释放内存。

软件工程121 金凯
2013年10月12日9:25 PM (完)。

相关文档
最新文档