线性表的顺序存储结构
实验
[实验目的]
掌握线性表的顺序存储方式和几个基本操作。
[实验内容]
(1)线性表的初始化操作
(2)线性表的插入
(3)线性表的删除
(4)输出线性表的内容
(5)求2个线性表共同的元素,并输出。
SeqList.h
//SeqList.h
#define MaxSize 100
typedef int ElemType;
typedef struct
{
ElemType List[MaxSize];
int size;
}SequenceList;
void ListInitialize(SequenceList *L); //初始化操作
int ListLength(SequenceList L); //获得线性表长度
int ListInsert(SequenceList *L,int i,ElemType x); //线性表插入元素
int ListDelete(SequenceList *L,int i,ElemType *x); //线性表删除元素
int ListGet(SequenceList L,int i,ElemType *x); //输出第i个元素
void ListPrint(SequenceList L); //输出整个线性表
void GetCommonNumber(SequenceList L1,SequenceList L2); //输出两个线性表相同的元素
SeqList.cpp
//SeqList.cpp
#include "SeqList.h"
#include "stdio.h"
void ListInitialize(SequenceList *L) //初始化操作
{
L->size = 0;
}
int ListLength(SequenceList L) //获得线性表长度
{
return L.size;
}
int ListInsert(SequenceList *L,int i,ElemType x) //线性表在第i的位置插入元素
{
int j;
if(L->size >= MaxSize)
{
printf("Already full,can't insert! 1\n");
//线性表已满,无法插入
return 0;
}
else if(i<0 || i > L->size)
{
printf("Invalid parameter! 2\n");
//插入位置参数错误
return 0;
}
else
{
for(j=L->size ; j>i ; j--)
L->List[j] = L->List[j-1];
L->List[i] = x;
L->size++;
return 1;
}
}
int ListDelete(SequenceList *L,int i,ElemType *x) //线性表删除第i个元素
{
int j;
if(L->size <= 0)
{
printf("Already empty,can't delete! 3\n");
//线性表已空,无法删除
return 0;
}
else if(i<0 || i > L->size-1)
{
printf("Invalid parameter! 4\n");
//删除位置参数错误
return 0;
}
else
{
*x = L->List[i];
for(j=i+1 ; j<L->size ; j++)
L->List[j-1] = L->List[j];
L->size--;
return 1;
}
}
int ListGet(SequenceList L,int i,ElemType *x) //输出第i个位置的元素
{
if(i<0 || i > L.size-1)
{
printf("Invalid parameter! 5\n");
//位置参数错误
return 0;
}
else
{
*x = L.List[i];
return 1;
}
}
void ListPrint(SequenceList L) //输出整个线性表
{
printf("The list is:\n");
for(int i=0 ; i<L.size ; i++)
{
printf("%d ",L.List[i]);
}
putchar(10);
}
void GetCommonNumber(SequenceList L1,SequenceList L2) //输出两个线性表相同的元素
{
printf("the common number:\n");
for(int i=0 ; i<L1.size ; i++)
for(int j=0 ; j<L2.size ; j++)
if(L1.List[i] == L2.List[j])
printf("%d ",L1.List[i]);
putchar(10);
}
SeqListPrj.cpp
//SeqListPrj.cpp
#include "stdio.h"
#include "SeqList.h"
void main()
{
/*(1)线性表的初始化操作*/
SequenceList List1;
ListInitialize(&List1);
/*(2)线性表的插入*/
ListInsert(&List1,0,3);
ListInsert(&List1,0,1);
ListInsert(&List1,0,7);
ListInsert(&List1,0,10);
/*输出线性表*/
printf("插入后List1的元素有:\n");
ListPrint(List1);
/*(3)线性表的删除*/
int delElement;
int pos =1;
ListDelete(&List1,pos,&delElement);
printf("删除的元素位置是:%d , 值是:%d\n",pos,delElement);
/*(4)输出线性表的内容*/
printf("删除后的线性表:\n");
ListPrint(List1);
/*(5)求2个线性表共同的元素,并输出。*/
/*(1)线性表的初始化操作*/
SequenceList List2;
ListInitialize(&List2);
/*(2)线性表的插入*/
ListInsert(&List2,0,2);
ListInsert(&List2,0,5);
ListInsert(&List2,0,7);
ListInsert(&List2,0,10);
ListInsert(&List2,0,3);
/*(4)输出线性表的内容*/
printf("插入后List2的元素有:\n");
ListPrint(List2);
/*输出共同元素*/
printf("两个线性表的共同元素有:\n");
GetCommonNumber(List1,List2);
}
实验示例
插入后List1的元素有:
The list is:
10 7 1 3
删除的元素位置是:1 , 值是:7
删除后的线性表:
The list is:
10 1 3
插入后List2的元素有:
The list is:
3 10 7 5 2
两个线性表的共同元素有:
the common number:
10 3
Press any key to continue