当前位置:网站首页>顺序表课设
顺序表课设
2022-06-23 07:30:00 【石先森很疯狂】
实验目的:
掌握线性表的基本操作:插入、删除、查找在顺序存储结构上的实现。
实验条件:
VC++6.0,计算机一台
内容:
建立一有序的顺序表,并实现下列操作:
- 1.把元素x插入表中并保持有序;
- 2.查找值为x的元素,若找到将其删除;
- 3.输出表中各元素的值。
算法思想:
/*建立一有序的顺序表,并实现下列操作: C语言描述 1.把元素x插入表中并保持有序; 2.查找值为x的元素,若找到将其删除; 3.输出表中各元素的值。*/
#include <stdio.h>
#define MAXSIZE 20
//建立顺序表结构体
typedef struct seqlist
{
int elem[MAXSIZE];
int length;
}SeqList;
//初始化顺序表
void IniList(SeqList *l)
{
l->length = 0;
}
//将元素插入表中并保持有序
void InsList(SeqList *l, int n)
{
int i = 0, j;
while (n > l->elem[i] && i < l->length)
i++;
for (j = l->length - 1; j >= i; j--)
l->elem[j+1] = l->elem[j];
l->elem[i] = n;
(l->length)++;
}
//查找某元素是否在表中
int CheckList (SeqList l, int n)
{
int i = 0;
while (i < l.length && n != l.elem[i])
i++;
if (i < l.length)
return i;
return -1;
}
//查找并删除某个元素
void DelList (SeqList *l, int n)
{
int j;
if (n < 0 || n > l->length - 1)
{
printf ("空!请检查该值是否在顺序表内!\n");
return ;
}
for (j = n + 1; j < l->length; j++)
l->elem[j-1] = l->elem[j];
(l->length)--;
}
//输出表中各元素
void PrintList(SeqList l)
{
int i;
for (i = 0; i < l.length; i++)
{
printf ("%d ", l.elem[i]);
}
printf ("\n");
}
int main ()
{
SeqList list;
int index, element;
IniList(&list); //初始化
printf ("请输入第一个元素: "); //输入元素
scanf ("%d", &element);
printf ("请输入其他元素(以0结尾): ");
while (0 != element)
{
InsList(&list, element); //插入元素
scanf ("%d", &element);
}
PrintList(list); //列表输出元素
printf ("请输入要删除的元素: ");
scanf ("%d", &element);
index = CheckList(list, element); //查找元素
if (-1 == index)
{
printf ("%d 不在该顺序表中 \n", element);
}
else
DelList(&list, index); //若找到,则删除元素
PrintList(list); //再次列表输出各个元素
return 0;
}
边栏推荐
猜你喜欢

YGG Spain subdao Ola GG officially established

Qt 使用QDomDocument读取xml文件

Socket socket programming

Talk about routing design in service governance

忽略超长参数违规

openvino系列 19. OpenVINO 与 PaddleOCR 实现视频实时OCR处理

To conquer salt fields and vegetable fields with AI, scientific and technological innovation should also step on the "field"

socket编程(多进程)
![[veusz] import 2D data in CSV](/img/22/467139f5a83ce9e88a57ced732d4d6.png)
[veusz] import 2D data in CSV

【Veusz】导入CSV中的二维数据
随机推荐
@Controller和@RestController的区别?
Using the for loop to output an alphabetic triangle
论文伪代码规范,伪代码在线编辑器,
Take you to tiktok. That's it
INT 104_LEC 06
C#打印缩放
How bootstrap clears floating styles
How do I install MySQL on my computer?
聊聊服务治理中的路由设计
Left multiply right multiply matrix problem
【Veusz】导入CSV中的二维数据
【Kubernetes】Kubernetes各大版本的最新版本下载地址
MySQL gets the system time period
Make a record of glib2.14 upgrading glib2.18 and the principle of the steps
Unity picture loading and saving
ArcMap批量删除距离较近的点
Acwing game 56 [End]
MFC Radio Button分组
Matlab random volatility SV, GARCH using MCMC Markov chain Monte Carlo method to analyze exchange rate time series
What is edge cloud?