算机实验报告1

算机实验报告1
算机实验报告1

电子信息工程学系实验报告——适用于计算机课程

实验项目名称:线性表的插入和删除实验时间:2012-2-22—

班级:计教101姓名:傅倩学号:010701140

实验目的:

熟悉掌握线性表的基本操作在顺序存储结构和链式存储结构上的实现,并熟悉其各自的优缺点及适用性。

实验环境:

xp,win-tc

实验内容及过程:

题目1:

编写程序实现下列的要求:

(1) 设数据元素为整数,实现这样的线性表的顺序存储表示。

(2) 键盘输入10个数据元素,利用顺序表的基本操作,建立该表。

(3) 利用顺序表的基本操作,找出表中的最大的和最小的数据元素(用于比较的数据元素为整数)。

(4) * 若数据元素为学生成绩(含姓名、成绩等字段),重新编程,实现上面的要求。要求尽可能少地修改前面的程序来得到新程序。(这里用于比较的字段为分数)测试数据:

题目2:

编写程序实现下列的要求:

(1) 设学生成绩表中的数据元素为学生成绩(含姓名、成绩字段),实现这样的线性表的链式存储表示。

(2) 键盘输入若干个数据元素(用特殊数据来标记输入数据的结束),利用链表的基本操作(前插或后插算法),建立学生成绩单链表。

(3) 键盘输入关键字值x,打印出表中所有关键字值<=x的结点数据。(用于比较的关键字字段为分数)。

(4) 输入关键字值x,删除表中所有关键字值<=x的结点。(用于比较的关键字字段为分数)。

测试数据:

实验结果及分析:

(1)运行结果:

该程序只有计算总个数和求最大最小值功能,比较简单,不能有其他更多的操作。

(2) 运行结果:

该程序可进行建立链表,输入输出数值,插入数据,删除数据,但无排序功能是很大的不足。实验心得:

通过该实验程序的编写,使我了解到自己的不足,对于许多程序的功能都不太理解,很多代码都是按老师上课写的来用,但是要用书上的代码确总是出错,可是却不知道怎么改。对于C语言知识确实比较薄弱,以至于学习数据结构觉得很难。这个实验主要是对线性表的插入与删除功能进行编写,这应该只是最基础的,但是我还是不太会,比如把那个输出结果进行排序都没有做出来,今后得更加努力学习了。

附录:

(1)第一题程序:

/* 建立顺序表,并找出最大值和最小值*/

#include "stdio.h"

#include "conio.h"

#define maxnum 100

struct seqlist

{

int A[maxnum];

int n; /*元素个数*/

struct seqlist *creatNULL() /*创建空表*/

{

struct seqlist *L;

L=(struct seqlist *)malloc(sizeof(struct seqlist));

L->n=0;

return L;

}

void creat(struct seqlist *L) /*创建表格*/

{

int i;

printf("input the number of table: ");

for(i=0;i<10;i++)

{scanf("%d",&L->A[i]);L->n++;} printf("the table is: ");

for(i=0;i<10;i++)

printf("%5d",L->A[i]);

printf("\n total is: ");

printf("n=%d ",L->n);

}

int Max(struct seqlist *L) /*查找最大值*/

{ int maxi=0;

int i;

for(i=1;i<=L->n-1;i++)

if(L->A[maxi]A[i])

maxi=i;

return L->A[maxi];

}

int Min(struct seqlist *L) /*查找最小值*/

{ int mini=0;

int i;

for(i=1;i<=L->n-1;i++)

if(L->A[mini]>L->A[i])

mini=i;

return L->A[mini];

}

main()

{ int max,min;

struct seqlist *L;

L=creatNULL();

creat(L);

max=Max(L);

min=Min(L);

printf("\nmax=%d",max);

printf("\nmin=%d",min);

getch();

(2)第二题程序:

#include "stdio.h"

#include "conio.h"

struct student

{

int num;

int score;

struct student *next;

};

#define NULL 0

struct student *creat() /*创建一个链表*/

{

struct student *head,*p,*q;

head=(struct student *)malloc(sizeof(struct student));

head->next=NULL;

p=(struct student *)malloc(sizeof(struct student));

printf("please input data: ");

scanf("%d,%d",&p->num,&p->score);

head=p;

while(p->num!=0)

{

q=p;

p=(struct student *)malloc(sizeof(struct student));

scanf("%d,%d",&p->num,&p->score); p->next=NULL;

q->next=p;

}

return head;

}

void print(struct student *head)

{

struct student *p;

p=head;

printf("output data: ");

while(p!=NULL&&p->num!=0)

{

printf("%d,%d ",p->num,p->score);

p=p->next;

}

}

struct student *insertList(struct student *head,int k,int x,int y) /*链表的插入*/ {

int i;

struct student *p,*s;

p=head->next;

i=1;

while((p!=NULL)&&(i

{

p=p->next;i++;

}

if(p!=NULL)

{

s=(struct student *)malloc(sizeof(struct student));

s->num=x;s->score=y;

s->next=p->next;

p->next=s;

}

return head;

}

struct student *delList(struct student *head,int k)/*链表的删除*/

{ int i;

struct student *p,*s;

p=head;

i=1;

while((p!=NULL)&&(i

{

p=p->next;i++;

}

if(p!=NULL)

{s=p->next;

p->next=p->next->next;

}

free(s);

return head;

}

struct student *delete(struct student *head,int x) /*删除小于等于x的数据*/ {

struct student *p;

p=head;

while(p->next!=NULL)

{

if(p->score>x)

p=p->next;

else

{

p->num=p->next->num;

p->score=p->next->score;

p->next=p->next->next;

}

}

return head;

}

void main()

{

int a,b,c,k,d;

struct student *p1,*p2,*p3,*p4,*head;

p1=creat();

head=p1;

print(p1);

getch();

printf("\n\n\ninsert position: ");

scanf("%d",&a);

printf("num: ");

scanf("%d",&b);

printf("score: ");

scanf("%d",&c);

p2=insertList(head,a,b,c) ;

print(p2);

getch();

printf("\ndelete position : ");

scanf("%d",&k);

p3=delList(head,k);

print(p3);

getch();

printf("\n\ndelete the less than or equal to the score:"); scanf("%d",&d);

p4=delete(head,d);

print(p4);

getch();

}

备注:以上各项空白处若填写不够,可自行扩展

相关主题
相关文档
最新文档