当前位置:网站首页>顺序表的基本操作
顺序表的基本操作
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;
}三、运行结果

边栏推荐
- [video tutorial] functions that need to be turned off in win10 system. How to turn off the privacy option in win10 computer
- [go Language brossage] go from 0 to Getting started 4: Advanced use of slice, Primary Review and Map Getting started Learning
- Bytebase joins Alibaba cloud polardb open source database community
- The difference between the lazy man mode and the hungry man mode
- Coinbase will launch the first encryption derivative for individual investors
- Audio and video 2020 2021 2022 basic operation and parameter setting graphic tutorial
- Based on STM32F103 0.96 inch OLED LCD driver (IIC communication)
- 网络安全审查办公室对知网启动网络安全审查,称其“掌握大量重要数据及敏感信息”
- Install the custom module into the system and use find in the independent project_ Package found
- Microsoft Office Excel 2013 2016 graphic tutorial on how to enable macro function
猜你喜欢

STM32 uses time delay to realize breathing lamp register version

Write a positive integer to the node and return a floating-point number multiplied by 0.85 when reading the node

Teach you how to view the number of connected people on WiFi in detail how to view the number of connected people on WiFi

Bytebase rejoint la communauté de base de données open source d'alicloud polardb

Download steps of STM32 firmware library

实现基于Socket自定义的redis简单客户端

Five day summary of software testing

Capacitive inching touch switch module control (stm32f103c8t6)
![[video tutorial] functions that need to be turned off in win10 system. How to turn off the privacy option in win10 computer](/img/14/0313857adc178ecee4c866a05e54aa.jpg)
[video tutorial] functions that need to be turned off in win10 system. How to turn off the privacy option in win10 computer

Making startup U disk -- Chinese cabbage U disk startup disk making tool V5.1
随机推荐
CVPR 2022缅怀孙剑!同济、阿里获最佳学生论文奖,何恺明入围
Test drive citus 11.0 beta (official blog)
Apache+PHP+MySQL环境搭建超详细!!!
Methods for comparing float types in the kernel
用自身细胞作为原料,首例3D打印耳朵移植成功!未来可打印更复杂器官
RF_ DC system clock setting gen1/gen2
Stackoverflow 年度报告 2022:开发者最喜爱的数据库是什么?
Win7 10 tips for installing Office2010 five solutions for installing MSXML components
First understand redis' data structure - string
The difference between the lazy man mode and the hungry man mode
字节、腾讯也下场,这门「月赚3000万」的生意有多香?
What about the Golden Angel of thunder one? Golden Angel mission details
Docker installing MySQL
等等党们的胜利!挖矿退潮后,显卡价格全面暴跌
Ribbon源码分析之@LoadBalanced与LoadBalancerClient
物聯網?快來看 Arduino 上雲啦
Install the custom module into the system and use find in the independent project_ Package found
The name of the button in the Siyuan notes toolbar has changed to undefined. Has anyone ever encountered it?
Docker installing Oracle
Zadig + cave Iast: let safety dissolve in continuous delivery