数据结构 实验1 线性表(顺序表)

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
(2)下面是一个不太完整的的源程序,目的为学生提供一个示范,供学生参考。
线性表的顺序存储结构,顺序表类。本程序的特点是,数据元素的类型不再是简单类型(int,char,float),而是更加接近使实用的比较复杂的结构体类型。在数据元素输入输出时,情况比较复杂一些。
#include <stdio.h>
break;
case 'S':
case 's':
printf("please input the number wanted\n");
scanf("%d",&temp);
printf("%d\n",Search_sq(&L1,temp));
printf("X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARCH,D=DELETE\n");
实验
一、实验目的
1. 了解线性表的逻辑结构特性,以及这种特性在计算机内的顺序存储结构。通常称为顺序表。
2.重点是线性表的基本操作在顺序存储结构上的实现;其中以插入和删除的操作为侧重点;并进一步学习结构化的程序设计方法。
3. 掌握使用 C语言中顺序表的程序设计方法,掌握阅读与补充源程序的方法。
二、实例
}
void creat_sq(Sqlist *L)
{
int tablen,i=0;
ElemType temp;
printf("please input the table length\n");
scanf("%d",&tablen);
printf("please input a serial number\n");
do
{
scanf("%d",&temp);
insert_sq(L,i,temp);
i++;
}while((i<tablen)&&(i<L->listsize ));
}
void display_sq(Sqlist *L)
{
int i;
i=0;
do
{
}
while(++i<L->length);
printf("\n**********Sqlist diplay**********\n");
break;
case 'D':
case 'd':
printf("please input thedeletedpos \n");
scanf("%d",&i);
printf("%d\n",delete_sq(&L1,i));
printf("X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARCH,D=DELETE\n");
break;
case 'V':
case 'v':
printf("please input the pos wanted\n");
scanf("%d",&i);
printf("%d\n",Search_sq_byV(&L1,i));
printf("X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARCH,D=DELETE\n");
default:break;
}
}
return 1;
}
实验内容:
1阅读代码。
2补充程序中没有实现的部分代码。
3完整显示出插入和删除元素时顺序表中元素的移动现象。
4使用随机函数方法产生1万个元素,同样随机生成插入(删除)元素位置循环100次,统计出平均需要多少时间。
break;
case 'I':
case 'i':
printf("please input the number to be inserted,D=DELETE\n");
scanf("%d",&temp);
printf("please input the position to be inserted(1~100)\n");
scanf("%d",&i);
insert_sq(&L1,i-1,temp);
printf("**********Sqlist inserted**********\n");
printf("X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARCH,D=DELETE\n");
{
int i;
i=0;
do
{
}
while(++i<L->length);
printf("\n**********%d deleted**********\n",pos);
}
int main(void)
{
Sqlist L1;
ElemType temp;
char cmd;
int i=0;
initlist_sq(&L1);
1. 线性表的顺序存储表示(结构)及实现。
(1)关于线性表的顺序存储结构的本质是:在逻辑上相邻的两个数据元素ai-1, ai,在存储地址中也是相邻的,既地址连续。顺序存储结构也称“向量(vector)”。在下列类设计中,采用静态一维数组elem[]表示向量,同时用length表示线性表长度。
ElemType elem[MAXSIZE];
int length;
有时可以采用动态一维数组来表示向量。
ElemType*elem;
int length;
intMAXSIZE
这要求在类的构造函数中,为其elem动态分配存储空间,而在析构函数中释放内存空间。
在上机实验时,需要将数据结构的类定义(包括成员函数的定义)的程序代码,写入源程序。同时用户必须自己编写一段主函数main(),在主函数中创建声明类的具体对象,通过这些对象调用类的公有函数。以便将一个典型数据结构类运用到实际问题中去。
break;
case 'P':
case 'p':
display_sq(&L1);
printf("X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARCH,D=DELETE\n");
break;
case 'X':
case 'x':
free(L1.elem);
return 1;
#include <stdlib.h>
#define MAXSIZE 10000
typedef int ElemType;
typedef struct list
{
ElemType *elem;
int listsize;
int length;
}Sqlist;
void initlist_sq(Sqlist *L)
}
int Search_sq(Sqlist *L,ElemType x)
{
int i;
i=0;
do
{
}
while(++i<L->length);
return 0;
}
ElemType Search_sq_byV(Sqlist *L,int n)
{
//ElemType temp;
return 0 ;
}
void delete_sq(Sqlist *L,int pos)
{
L->elem=(ElemType *)malloc(MAXSIZE*sizeof(ElemType));
if(!L->elem) exit(0);
L->length=0;
L->listsize=MAXSIZE;
}
void insert_sq(Sqlist *L,int n,ElemType x)
printf("X=EXIT,C=CREATE,P=DISPLAY,I=INSERT,S=SEARCH,D=DELETE\n");
while(1)
{
cmd=getchar();
switch(cmd)
{
case 'C':
case 'c':
creat_sq(&L1);
printf("X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARCH,D=DELETE\n");
{
int i;
if(n<0||n>L->length+1)
{
printf("n is error\n");
return ;
}Βιβλιοθήκη Baidu
for(i=L->length;i>=n;i--)
{
L->elem[i+1]=L->elem[i];
}
L->elem[i-1]=x;
L->length++;
//printf("**********Sqlist inserted**********\n");
相关文档
最新文档