验证性实验一 线性表的顺序存储实验
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
验证性实验一线性表的顺序存储实验
实验课程名:数据结构
专业班级:学号:姓名:
实验时间:实验地点:指导教师:
一、实验目的和要求
1、掌握用Visual C++6.0上机调试顺序表的基本方法
2、掌握顺序表的基本操作,插入、删除、查找、以及有序顺序表的合并等算法的实现
二、实验内容
1、顺序表基本操作的实现。
要求生成顺序表时,可以键盘上读取元素,用顺序存储结构实现存储。
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<iostream.h>
#include<malloc.h>
#define LIST_INIT_SIZE 10
#define OK 1
#define ERROR 0
#define LISTINCREMENT 10
typedef int ElemType;
typedef struct
{ int *elem;
int length;
int listsize;
}SqList;
int InitList_Sq(SqList &L) //InitList_Sq() function
{ //Inititial a Sq_List
L.elem=(ElemType *)malloc(LIST_INIT_SIZE *sizeof(ElemType));
if (!L.elem) return(0);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return(1);
}//end of InitList_Sq() function
int ListInsert_sq(SqList &L,int i,int e)//ListInsert_sq()
{ if(i<1||i>L.length+1) //i (location) is illegal
{ cout <<"Initial failure!"<<endl;
getch();
return (ERROR);
}
if(L.length>=L.listsize) //insert into the end of the Sqlist
{ int *Newbase;
Newbase=(int *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(int));
if(!Newbase)
{ cout<<"Overflow!"<<endl;
getch();
return (ERROR);
}
L.elem=Newbase;
L.listsize+=LISTINCREMENT;
}
int *p,*q;
q=&(L.elem[i-1]); //q point at the element before the inserted one
for(p=&(L.elem[L.length-1]);p>=q;--p) //move the element
*(p+1)=*p;
*q=e;
++L.length;
return (OK);
} //ListInser_sq() end
void main()
{ int m,i,x;
SqList A;
InitList_Sq(A);
cout<<"Input the size of A:\n"<<endl;
cin>>m;
cout<<"Input the element of A:\n";
for(i=1;i<=m;i++)
{cin>>x;
ListInsert_sq(A,i,x);
}
cout<<"The element of A:"<<endl;
for(i=0;i<A.length;i++)
cout<<A.elem[i]<<'\t';
cout<<endl;
}
实验结果:
(本人运行程序得到实验截图)
实验结果分析:
本程序中首先定义了一个链表的结构体,然后定义了一个线性表,在插入函数中先申请了一个与头文件中定义的线性表大小相同的内存空间,然后用if语句判断这个线性表是否存在,若不存在则返回0,若存在则将线性表的长度设置为与申请的空间大小相同。
然后就定义了输入和输出函数。
在主函数中首先定义了两个变量,接着定义了一个线性表,然后调用见线性表的函数、输入函数和输出函数。
最后运行程序就得到了如上图的结果。
(二)已知顺序表la和lb中的数据元素按非递减有序排列,将la和lb 表中的数据元素,合并成为一个新的顺序表lc,lc中的数据元素仍按非递减有序排列,并且不破坏la和lb表。
#define OK 1
#define ERROR 0
#define MAXSIZE 20
#define LIST_INIT_SIZE 20
#define LISTINCREMENT 20
#define TRUE 1
#define FALSE 0
#include<iostream.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>
typedef int ElemType;
typedef struct
{ int *elem;
int length;
int listsize;
}SqList;
int InitList_Sq(SqList &L) //InitList_Sq() function
{ //Inititial a Sq_List
L.elem=(int *)malloc(LIST_INIT_SIZE *sizeof(int));
if (!L.elem) return(0);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return(1);
}//end of InitList_Sq() function
int ListInsert_sq(SqList &L,int i,ElemType e)//ListInsert_sq()
{ if(i<1||i>L.length+1) //i (location) is illegal
{ cout <<"Initial failure!"<<endl;
getch();
return (ERROR);
}
if(L.length>=L.listsize) //insert into the end of the Sqlist
{ int *Newbase;
Newbase=(ElemType
*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!Newbase)
{ cout<<"Overflow!"<<endl;
getch();
return (ERROR);
}
L.elem=Newbase;
L.listsize+=LISTINCREMENT;
}
int *p,*q;
q=&(L.elem[i-1]); //q point at the element before the inserted one
for(p=&(L.elem[L.length-1]);p>=q;--p) //move the element
*(p+1)=*p;
*q=e;
++L.length;
return (OK);
} //ListInser_sq() end
int Merge_Sq(SqList &A,SqList &B,SqList &C) //Merge_Sq() function
{ //Merge the SqList A and B to C
C.listsize=C.length=A.length+B.length;
C.elem=(ElemType *)malloc(C.listsize*sizeof(ElemType));
int i=0,j=0; //i and j is the Subscript of A.elem[] and B.elem [] int k=0; //k is the Subscript of C.elem[] while(i<=A.length ) //insert the rest of SqList A { C.elem [k]=A.elem [i];
i++;k++;
}//end of while
while (j<=B.length) //insert the rest of SqList B { C.elem [k-1]=B.elem [j];
j++;k++;
}
cout<<"Success to Merge A and B !"<<endl;
return(1);
}//end of Merge_Sq() function
void main()
{ SqList A,B,C;
int m,n,i,j,x;
InitList_Sq(A);
cout<<"Input the size of A:\n"<<endl;
cin>>m;
cout<<"Input the element of A:\n";
for(i=1;i<=m;i++)
{cin>>x;
ListInsert_sq(A,i,x);
}
InitList_Sq(B);
cout<<"Input the size of B:\n"<<endl;
cin>>n;
cout<<"Input the element of B:\n";
for(j=1;j<=n;j++)
{cin>>x;
ListInsert_sq(B,j,x);
}
InitList_Sq(C);
Merge_Sq(A,B,C);
cout<<"After merge,the element of C:\n"<<endl;
for(i=0;i<m+n;i++)
cout<<C.elem[i]<<'\t';
}
实验结果:
(本人运行程序得到实验截图)
结果分析:
创建了一个SqList的结构体函数和创建线性表的函数,然后是输入函数,使用for 循环来将数字逐个输入,显示的很清楚用户输入的是第几个数字。
用Merge_Sq函数来实现两表的合并,用cout输出。
(三).从有序顺序表A中删除那些在顺序表B和顺序表C中都出现的元素
# define OK 1
# define ERROR 0
# define MAXSIZE 10
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10
#define TRUE 1
#define FALSE 0
#include<malloc.h>
#include<iostream.h>
#include<conio.h>
#include<string.h>
typedef int ElemType;
/* 顺序存储类型*/
typedef struct
{ int *elem;
int length;
int listsize;
}SqList;
/*构造一个空线性表算法*/
int InitList_Sq(SqList &L,int n)
{ //Inititial a Sq_List
L.elem=(int *)malloc(n *sizeof(int));
if (!L.elem) return(0);
L.length=0;
L.listsize=n;
return(1);
}
/* 向顺序表中插入元素*/
int ListInsert_sq(SqList &L,int i,int e)
{
int *p,*q;
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
return (OK);
} //ListInser_sq() end
int rank(SqList &L) /*线性表的排序*/
{
int i,j,k;
for(i=0;i<L.length-1;i++)
for(j=0;j<L.length-1-i;j++)
if(L.elem[j]>L.elem[j+1])
{
k=L.elem[j];
L.elem[j]=L.elem[j+1];
L.elem[j+1]=k;
};
return ERROR;
return OK;
}
int difference_sqlist( SqList& a, SqList b, SqList c ) {
if(a.length*b.length*c.length!=0)
{
int i,j,k,x;
bool m,n;
for(i=0;i<a.length;i++)
{
for(j=0;j<b.length;j++)
if(a.elem[i]==b.elem[j])
{m=true;break;}
for(k=0;k<c.length;k++)
if(a.elem[i]==c.elem[k])
{n=true;break;}
if(m&n)
{
if(i==(a.length-1))
{a.length--;break;}
else
{ for(x=i;x<(a.length-1);x++)
a.elem[x]=a.elem[x+1];
--a.length;
}
}
m=false;
n=false;
}
}
return (a.length);
}
void main()
{
SqList A,B,C;
int i,x,n,m,k,y,z;
cout<<"Input the size of A:"<<endl;
cin>>m;
InitList_Sq(A,m);
cout<<"Input the element of A:"<<endl;
for(i=1;i<=m;i++)
{
cin>>x;
ListInsert_sq(A,i,x);
}
cout<<"Input the size of B:"<<endl;
cin>>n;
InitList_Sq(B,n);
cout<<"Input the element of B:"<<endl; for(i=1;i<=n;i++)
{
cin>>y;
ListInsert_sq(B,i,y);
}
cout<<"Input the size of C:"<<endl;
cin>>k;
InitList_Sq(C,k);
cout<<"Input the element of C:"<<endl; for(i=1;i<=k;i++)
{
cin>>z;
ListInsert_sq(C,i,z);
}
rank(A);
rank(B);
rank(C);
cout<<"Output the original element of A:"<<endl;
for(i=0;i<m;i++)
{
cout<<A.elem[i]<<"\t";
}
cout<<endl;
difference_sqlist(A,B,C );
cout<<"Output the new element of A:"<<endl;
for(i=0;i<A.length;i++)
{
cout<<A.elem[i]<<"\t";
}
}
实验结果:
(本人运行程序得到实验截图)
结果分析:
此程序首先任然是先定义了结构体和见线性表的函数,不过在整个函数中多了线性表的排序函数,在主函数中多了一个两个for循环来比较线性表元素来实现若A中有与B和C中原数相同的则删除A中此元素的功能。
(四)将一顺序存储的线性表(设元素均为整型量)中所有零元素向表尾集中,其他元
素则顺序向表头方向集中。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10
# define OK 1
# define ERROR 0
typedef int ElemType;
typedef struct /* 顺序存储类型*/
{
int *elem;
int length;
int listsize;
}SqList;
int InitList(SqList &L) /*构造一个空线性表算法*/ {
L.elem=(int *)malloc(LIST_INIT_SIZE *sizeof(int));
if (!L.elem) return(0);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
int input(SqList &L) /*输入*/
{
int i;
cout<<"Input the size of the List:";
cin>>L.length;
for(i=0;i<L.length;i++)
{
cout<<"Input the element. NO."<<i+1<<": ";
cin>>L.elem[i];
}
return OK;
}
int output(SqList L) /*输出*/
{
int i;
for(i=0;i<L.length;i++)
cout<<L.elem[i]<<" ";
return ERROR;
}
void main()
{
SqList A;
InitList(A);
input(A);
cout<<"Output the list:";
output(A);
cout<<endl;
int i,j,k;
for(i=0;i<A.length-1;i++)
for(j=0;j<A.length-1-i;j++)
if(A.elem[j]==0)
{
k=A.elem[j];
A.elem[j]=A.elem[j+1];
A.elem[j+1]=k;
}
cout<<"Output the new list:";
output(A);
cout<<endl;
}
实验结果:
(本人运行程序得到实验截图)
结果分析:
此程序在主函数中利用for循环来实现La重元素与0进行比较若为0则插入到线性表尾的功能。
三、结论(写本次实验的收获)
通过本次试验我掌握了线性表的创建、插入、排序以及两个线性表的合并,掌握用Visual C++6.0上机调试顺序表的基本方法。
知道了线性表的顺序存储方式。
了解了线性表中个原书的比较方法。
四、教师批阅。