当前位置:网站首页>顺序表的基本操作
顺序表的基本操作
2022-06-24 19:03:00 【Learning The Monk】
目录
一、实验要求
1、验证性实验:实现顺序表的基本操作
实验内容:编写一个程序sqlist.cpp (或.c),
实现顺序表的各种基本运算和整体建表算法(假设顺序表的内容类型ElemType为char),
并在此基础上设计一个程序exp1.cpp (或.c)完成一下功能。
(1) 初始化顺序表L。
(2) 将元素a、b、c、d、e依次插入顺序表L中。
(3) 输出顺序表L。
(4) 输出顺序表L的长度。
(5) 判断顺序表L是否为空。
(6) 输出顺序表L的第3个元素。
(7) 输出元素a的位置。
(8) 在第4个元素位置上插入元素f。
(9) 输出顺序表L。
(10) 删除顺序表L的第3个元素。
(11) 输出顺序表L。
(12) 销毁顺序表L。
二、代码实现
/*
1、验证性实验:实现顺序表的基本操作
实验内容:编写一个程序sqlist.cpp (或.c),
实现顺序表的各种基本运算和整体建表算法(假设顺序表的内容类型ElemType为char),
并在此基础上设计一个程序exp1.cpp (或.c)完成一下功能。
(1) 初始化顺序表L。
(2) 将元素a、b、c、d、e依次插入顺序表L中。
(3) 输出顺序表L。
(4) 输出顺序表L的长度。
(5) 判断顺序表L是否为空。
(6) 输出顺序表L的第3个元素。
(7) 输出元素a的位置。
(8) 在第4个元素位置上插入元素f。
(9) 输出顺序表L。
(10) 删除顺序表L的第3个元素。
(11) 输出顺序表L。
(12) 销毁顺序表L。
*/
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100 //线性表最大分配容量
#define LISTINCREMENT 10 //线性表扩展容量
typedef char ElemType; //数据元素为字符型
typedef struct
{
/* data */
ElemType *data;
int length;
int listsize;
}sqlist;
//初始化顺序表
int InitList(sqlist *L)
{
//动态分配:malloc函数申请一片连续的存储空间
L->data=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->data)
{
return ERROR;
}
L->length=0; //初始表长为0
L->listsize=LIST_INIT_SIZE;
return OK;
}
//将元素e插入到顺序表L的第k个位置。
int List_Insert(sqlist *L,int k,ElemType e)
{
int i=0;
if(!L->data)
return ERROR;
if (k>L->length+1|| k<1)//判断插入位置的合法性
{
return ERROR;
}
if (L->length >= L->listsize)
{
char *new_base; //空间不足,重新分配
new_base = (char*)realloc(L->data, sizeof(char)*(L->listsize + LISTINCREMENT));
L->data = new_base;
if (!L->data)
{
return ERROR;
}
L->data = new_base;
L->listsize += LISTINCREMENT;
}
//插入元素e
if(k<L->length)
{
for (i = L->length - 1 ; i>= k - 1; i--)
L->data[i + 1] = L->data[i];
}
L->data[k - 1] = e;
L->length++; //多一个元素长度加1
return OK;
}
//输出顺序表L。
int print_List_data(sqlist *L)
{
if(!L->data)
return ERROR;
for (int i = 0; i < L->length; i++)
{
/* code */
printf("%c ",L->data[i]);
}
printf("\n");
return OK;
}
//输出顺序表L的长度。
int print_List_Length(sqlist *L)
{
if(!L->data)
return ERROR;
printf("%d\n",L->length);
return OK;
}
//判断顺序表L是否为空。
int ListEmpty(sqlist *L)
{
if(!L->data||L->length!=0)
return ERROR;
else
return OK;
}
//输出顺序表L的第i个元素。(该元素e是需要返回给用户的)
int GetElem(sqlist *L, int i, ElemType *e)
{
if (!L->data)
{
return ERROR;
}
if (i < 1 || i >= L->length-1)
{
return ERROR; //i值不合法
}
*e =L->data[i-1];
return OK;
}
//输出元素a的位置。(该元素e是不需要返回给用户的)
int LocateElem(sqlist *L, char e)
{
if (!L->data)
{
return ERROR;
}
for (int i = 0; i < L->length; i++)
{
if (L->data[i]==e)
{
return i+1; //返回元素所在位置
}
}
return OK;
}
// 删除顺序表L的第i个元素。(该元素e是不需要返回给用户的)
int ListDelete(sqlist *L, int i, char e)
{
int k;
if (!L->data)
{
return ERROR;
}
if (i<1 || i>L->length)
{
return ERROR; //i值不合法
}
e = L->data[i - 1];
for (k = i; k <= L->length ;k++)
L->data[k - 1] = L->data[k];
L->length--;
return OK;
}
// 销毁顺序表L。
int DestroyList(sqlist *L)
{
if (!L->data)
{
return ERROR; //表不存在
}
else
free(L->data); //释放内存 销毁线性表
printf(" 线性表销毁成功!");
return 0;
}
int main()
{
sqlist L;
ElemType e;
printf("1:初始化顺序表L\n");
InitList(&L);
printf("2:依次采用尾插法插入a , b , c , d , e元素\n");
List_Insert(&L, 1, 'a');
List_Insert(&L, 2, 'b');
List_Insert(&L, 3, 'c');
List_Insert(&L, 4, 'd');
List_Insert(&L, 5, 'e');
printf("3:顺序表L:");
print_List_data(&L);
printf("4:线性表L表长为:");
print_List_Length(&L);
printf("5:顺序表L为:%s\n", (ListEmpty(&L) ? "空" : "非空"));
GetElem(&L, 3, &e);
printf("6:顺序表的第3个元素是:%c\n", e);
printf("7:线性表L中元素a的位置为: %d\n", LocateElem(&L, 'a'));
printf("8:在第4个元素位置上插入f元素\n");
List_Insert(&L, 4, 'f');
printf(" 顺序表L为:");
print_List_data(&L);
printf("9:删除L的第3个元素\n");
ListDelete(&L, 3, e);
printf(" 顺序表L为:");
print_List_data(&L);
printf("10:销毁顺序表L\n");
DestroyList(&L);
return 0;
}
三、运行结果
边栏推荐
猜你喜欢
Stackoverflow annual report 2022: what are developers' favorite databases?
Where are Xiaomi mobile phone's favorite SMS and how to delete them
Making startup U disk -- Chinese cabbage U disk startup disk making tool V5.1
The name of the button in the Siyuan notes toolbar has changed to undefined. Has anyone ever encountered it?
年轻人捧红的做饭生意经:博主忙卖课带货,机构月入百万
Information theory of popular science Shannon
2022年最新四川建筑八大员(电气施工员)模拟题库及答案
宅男救不了元宇宙
[go Language brossage] go from 0 to Getting started 4: Advanced use of slice, Primary Review and Map Getting started Learning
Byte and Tencent have also come to an end. How fragrant is this business of "making 30million yuan a month"?
随机推荐
【CANN文档速递05期】一文让您了解什么是算子
基于SSM的物料管理系统(源码+文档+数据库)
What are the functions of IBPs open source form designer?
Vs2017 add header file path method
Behind Tiantian Jianbao storm: tens of millions in arrears, APP shutdown, and the founder's premeditated plan to run away?
Wait for the victory of the party! After mining ebb tide, graphics card prices plummeted across the board
Test drive citus 11.0 beta (official blog)
Cooking business experience of young people: bloggers are busy selling classes and bringing goods, and the organization earns millions a month
Kubernetes cluster deployment
Using dynamic time warping (DTW) to solve the similarity measurement of time series and the similarity identification analysis of pollution concentration in upstream and downstream rivers
Clustered index (clustered index), nonclustered index (nonclustered index)
Ribbon source code analysis @loadbalanced and loadbalancerclient
思源笔记工具栏中的按钮名称变成了 undefined,有人遇到过吗?
Anti epidemic through science and technology: white paper on network insight and practice of operators | cloud sharing library No.20 recommendation
gateway
Teach you how to cancel computer hibernation
[cann document express issue 06] first knowledge of tbe DSL operator development
Making startup U disk -- Chinese cabbage U disk startup disk making tool V5.1
【CANN文档速递04期】揭秘昇腾CANN算子开发
How does the video platform import the old database into the new database?