仅供参考,记得看看自己班的课代表有没有发模板哦2333
实 验 报 告
课 程: 数据结构
班 级:
实验序号: 1
姓 名:
学 号:
实验日期:
题 目: 顺序表的建立和运算
一、 实验目的和要求
①熟悉C语言的上机环境,进一步掌握C语言的结构特点。
②熟练掌握线性表的结构特点和基本操作。
③学会使用顺序表解决实际问题。
二、实验环境
Windows2000 ,VB
三、实验内容及实施
(1)实验步骤
①建立一个顺序表,输入n个元素并输出, 定义函数名为CreateList(SqList &L, int n);
②查找线性表中的最大元素并输出,定义函数名为(SqList &L, ElemType &maxelem);
③在线性表的第i个元素前插入一个正整数x,定义函数名为ListInsert(SqList &L, int i, ElemType x);
④删除线性表中的第j个元素,定义函数名为ListDelete(SqList &L, int j);
⑤将线性表中的元素按升序排列,定义函数名为SortList(SqList &L);
⑥将线性表中的元素就地逆序(只允许用一个暂存单元),定义函数名为ReverseList(SqList &L);
(2)程序流程图
(3)源程序
#include
#include
#include
#include
#define MAXSIZE 100
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define LIST_INIT_SIZE 5
#define LISTINCREMENT 1
using namespace std;
typedef int Status;
typedef int ElemType; //表内元素类型为 int, 可更改
typedef struct
{
ElemType *elem; //注意和课本上一样,这里是指针
int length;
}SqList;
int n, i, j; //全局变量
ElemType maxelem, x;
Status InitList(SqList &L) //建立
{
L.elem = new ElemType[MAXSIZE]; // new 是申请空间,详见博客 https://blog.csdn.net/leigelaile1/article/details/81835592
if(!L.elem) 根据上面博客的说法,这句话毫无用处, 不成功会自动退出,但是未遇见过不成功的情况, 所以先照着书上来了2333
exit(OVERFLOW);
L.length = 0;
return OK;
}
Status CreateList(SqList &L, int n) //建立一个顺序表,输入n个元素并输出
{
printf("Please enter the number of elements:\n");
scanf("%d", &n);
if(n <= 0 || n > MAXSIZE)
{
printf("Transboundary\n");
return ERROR;
}
printf("Please enter the elements:\n");
for(int i = 0; i < n; ++i)
{
cin >> L.elem[i]; //也可以通过插入操作建立
++L.length;
}
for(int i = 0; i < L.length; ++i)
cout << L.elem[i] << ' ';
cout << endl;
return OK;
}
Status Find_MaxElem(SqList &L, ElemType &maxelem) //查找线性表中的最大元素并输出
{
if(!L.length)
{
printf("Non-existent\n");
return ERROR;
}
maxelem = L.elem[0];
for(int i = 0; i < L.length; ++i)
if(L.elem[i] > maxelem)
maxelem = L.elem[i];
printf("The largest element in the linear table is:\n");
cout << maxelem << endl;
return OK;
}
Status ListInsert(SqList &L, int i, ElemType x) //在线性表的第i个元素前插入一个正整数x
{
printf("Please enter the insertion position and value:\n");
cin >> i >> x;
if((i < 1) || (i > L.length+1))
{
printf("Transboundary\n");
return ERROR;
}
if(L.length == MAXSIZE)
return ERROR;
for(int j = L.length-1; j >= i-1; --j)
L.elem[j+1] = L.elem[j];
L.elem[i-1] = x;
++L.length;
printf("The linear table after inserting is as follow:\n");
for(int i = 0; i < L.length; ++i)
cout << L.elem[i] << ' ';
cout << endl;
return OK;
}
Status ListDelete(SqList &L, int j) //删除线性表中的第j个元素
{
printf("Enter the position of the element to delete:\n");
scanf("%d", &j);
if((j < 1) || (j > L.length))
{
printf("Transboundary\n");
return ERROR;
}
for(int i = j; i <= L.length; ++i)
L.elem[i-1] = L.elem[i];
--L.length;
printf("The linear table after deleting is as follow:\n");
for(int i = 0; i < L.length; ++i)
cout << L.elem[i] << ' ';
cout << endl;
return OK;
}
Status SortList(SqList &L) //将线性表中的元素按升序排列
{
if(!L.length)
{
printf("Transboundary\n");
return ERROR;
}
int i, j, k;
ElemType t;
for(i = 0; i < L.length-1; ++i)
{
k = i;
for(j = i+1; j < L.length; ++j)
if(L.elem[j] < L.elem[k])
k = j;
if(k != i)
{
t = L.elem[i];
L.elem[i] = L.elem[k];
L.elem[k] = t;
}
}
printf("The linear table after ascending is as follow:\n");
for(i = 0; i < L.length; ++i)
cout << L.elem[i] << ' ';
cout << endl;
return OK;
}
Status ReverseList(SqList &L) //将线性表中的元素就地逆序(只允许用一个暂存单元)
{
if(!L.length)
{
printf("Transboundary\n");
return ERROR;
}
int l, r;
ElemType t;
l = 0;
r = L.length-1;
while(l < r)
{
t = L.elem[l];
L.elem[l] = L.elem[r];
L.elem[r] = t;
++l;
--r;
}
printf("The linear table after local reverse order is as follow:\n");
for(int i = 0; i < L.length; ++i)
cout << L.elem[i] << ' ';
cout << endl;
return OK;
}
int main()
{
SqList L;
InitList(L);
CreateList(L, n);
Find_MaxElem(L, maxelem);
ListInsert(L, i, x);
ListDelete(L, j);
SortList(L);
ReverseList(L);
return 0;
}
(4)实验结果
四、实验感想
通过这次实验,我巩固了线性表的结构特点和基本操作以及C语言相关的程序设计方法与技术,同时意识到,熟练掌握课本知识是实验的基础,不把课本上的相关知识学深学透, 实验就会显得很难, 且会浪费很多时间。这次实验重要的不仅仅是实现一个顺序表的各种操作, 更重要的是通过做实验的过程学到思考问题并动手解决的方法。
PS:如果想让这些操作间互不影响,去掉参数前的 & 即可, 如 SortList(SqList L)
如果想结果好看点, 多加一个换行符hhh
例如把上面⑤ ⑥操作去掉 & ,运行结果是
一周后
老师讲了这个实验,意思是从用户角度出发,设计要人性化,让用户能自行选择进行什么样的操作,(貌似只有少数人的实验符合老师的这个想法,所以一定要跟老师及时沟通啊)
修改如下(只改了主函数)
(PS: 1 只能输入一次 ,因为只定义了一个 SqList L; 如果多次输入 1 ,建立多个顺序表的话, 估计的 SqList 一个数组才行2333)
int main()
{
int num;
SqList L;
printf("What do you want to do?\n");
printf("0.退出程序\n");
printf("1.建立一个顺序表,输入n个元素并输出\n");
printf("2.查找线性表中的最大元素并输出\n");
printf("3.在线性表的第i个元素前插入一个正整数x\n");
printf("4.删除线性表中的第j个元素\n");
printf("5.将线性表中的元素按升序排列\n");
printf("6.将线性表中的元素就地逆序\n");
printf("Please enter the serial number of the operation you want to perform:\n");
while(cin >> num) //循环输入, 可执行多个操作
{
switch(num)
{
case 0: exit(0);
case 1: InitList(L);
CreateList(L, n); break;
case 2: Find_MaxElem(L, maxelem); break;
case 3: ListInsert(L, i, x); break;
case 4: ListDelete(L, j); break;
case 5: SortList(L); break;
case 6: ReverseList(L); break;
default: printf("The serial number is illegal.Please enter again:\n");
}
printf("Please enter the serial number of the operation you want to perform:\n");
}
return 0;
}
运行结果