本文实例为大家分享了C++实现基于静态数组的顺序表,供大家参考,具体内容如下
实现的基本操作有:
1. 初始化
2. 尾插
3. 尾删
4. 头插
5. 头删
6.查找任意元素
7. 读任意位置元素
8. 修改任意位置元素
9. 查找指定元素值的下标
10. 在任意位置插入元素
11.删除指定位置的元素
12.删除任意元素
13.打印数据
头文件seqlist.h:
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define maxsize 1000//链表最多元素个数
typedef int seqType;
typedef struct seqlist
{
seqType arr[maxsize];//数据域,存放数据
size_t size;//size_t是无符号长整型,是一种类型,size在数组中表示元素个数
}seqlist ;
void PrintSeqList(seqlist *seq); //打印数据
void print_seqlist(char *s); //打印标题
void seqlistInit(seqlist *seq); //链表初始化
void seqlistPushBack(seqlist *seq, seqType value); //尾插元素,value是插入元素的值
void seqlistPopBack(seqlist *seq); //尾删元素,value是插入元素的值
void seqlistPushHead(seqlist*seq, seqType value); //头插元素
void seqlistPopHead(seqlist*seq); //头删元素
seqType seqlistFind(seqlist *seq, seqType data); //查找任意元素
seqType seqlistRead_pos(seqlist*seq, size_t pos); //读取任意位置的元素
size_t seqlistFind_pos(seqlist*seq, seqType value); //查找指定元素的下标
seqType seqlistModify(seqlist*seq, size_t pos, seqType data); //修改任意位置的元素
void seqlistInsert_pos(seqlist *seq, size_t pos, seqType data); //在任意位置插入元素
void seqlistErase_pos(seqlist *seq, size_t pos); //删除任意位置的元素
void seqlistRemove(seqlist *seq, seqType data); //删除元素
实现文件seqlist.c
#include"seqlist.h"
void PrintSeqList(seqlist *seq)//打印数据
{
size_t i = 0;
if (seq->size == 0)
{
printf("线性表为空,打印结束\n");
return;
}
for (i = 0; i < seq->size; i++)
{
printf("下标为%d的元素是:%d\n",i, seq->arr[i]);
}
printf("\n");
}
void print_seqlist(char *s)//打印标题
{
int i = 0;
printf("%s\n",s);
printf("\n");
}
void seqlistInit(seqlist *seq)//链表初始化
{
assert(seq);
seq->size = 0;//有效元素赋值为0;
}
void seqlistPushBack(seqlist *seq, seqType value)//尾插元素,value是插入元素的值
{
assert(seq);//判断指针是否为空
if (seq->size == maxsize)
{
printf("元素已满,无法插入\n");
return;
}
else
seq->arr[seq->size++] = value;
}
void seqlistPopBack(seqlist *seq)//尾删元素,value是插入元素的值
{
assert(seq);//判断指针是否为空
if (seq->size ==0)
{
printf("内容已为空,无法删除\n");
return;
}
else
seq->size--;
}
void seqlistPushHead(seqlist *seq, seqType value)//头插元素
{
assert(seq);//判断指针是否为空
if (seq->size == maxsize)
{
printf("元素已满,无法插入\n");
return;
}
else
{
int i = seq->size - 1;
for (; i >= 0; i--)
{
seq->arr[i + 1] = seq->arr[i];
}
seq->arr[0]=value;
seq->size++;
}
}
void seqlistPopHead(seqlist *seq)//头删元素
{
assert(seq);//判断指针是否为空
if (seq->size == 0)
{
printf("内容已为空,无法删除\n");
return;
}
else
{
size_t i =1;
for (; i < seq->size; i++)
{
seq->arr[i - 1] = seq->arr[i];
}
seq->size--;
}
}
seqType seqlistFind(seqlist *seq, seqType data)//查找任意元素
{
size_t i = 0;
assert(seq);//判断指针是否为空
if (seq->size == 0)
{
printf("线性表为空\n");
return -1;
}
for (i = 0; i < seq->size - 1; i++)
{
if (seq->arr[i] == data)
{
return seq->arr[i];
}
}
return -1;
}
seqType seqlistRead_pos(seqlist *seq, size_t pos)//读取任意位置的元素
{
assert(seq);//判断指针是否为空
if (seq->size == 0)
{
printf("内容为空,无法读取内容\n");
return -1;
}
else if (pos> seq->size)
{
printf("读取位置错误\n");
}
else
return seq->arr[pos];
}
size_t seqlistFind_pos(seqlist *seq, seqType value)//查找指定元素的下标
{
assert(seq);//判断指针是否为空
size_t i= 0;
for (; i < seq->size; i++)
{
if (seq->arr[i] == value)
return i;
}
return -1;
}
seqType seqlistModify(seqlist *seq, size_t pos, seqType data)//修改任意位置元素
{
assert(seq);//判断指针是否为空
if (seq->size == 0)
{
printf("内容为空,无法修改内容\n");
return -1;
}
for (; pos < seq->size; pos++)
{
seq->arr[pos] = data;
return seq->arr[pos];
}
}
void seqlistInsert_pos(seqlist *seq, size_t pos, seqType data)//在任意位置插入元素
{
assert(seq);
if (seq->size == maxsize)
{
printf("内容已满,无法继续插入内容\n");
return;
}
else if (pos>seq->size)
{
printf("非法位置,不允许插入\n");
return;
}
else
{
size_t m= seq->size ;
for (m = seq->size; m > pos; m--)
{
seq->arr[m] = seq->arr[m-1];
}
seq->arr[pos] = data;
seq->size++;
}
}
void seqlistErase_pos(seqlist *seq, size_t pos)//删除任意位置的元素
{
assert(seq);
if (seq == NULL)
{
printf("内容已为空!\n");
return;
}
else if (pos > seq->size)
{
printf("该位置无法删除!\n");
return;
}
else
{
size_t i ;
for (i = pos; i < seq->size-1; i++)
{
seq->arr[i] = seq->arr[i+1];
}seq->size--;
}
}
void seqlistRemove(seqlist *seq, seqType data)//删除元素
{
assert(seq);//判断指针是否为空
size_t i = 0;
i = seqlistFind_pos(seq,data);
if (i >= 0)
{
while (i <seq->size)
{
seq->arr[i] = seq->arr[i + 1];
i++;
}
seq->size--;
return;
}
else
{
printf("没有找到该元素");
return;
}
}
在进行插入、删除等操作中,若对元素的位置移动不清楚的借助画图能更好地理解。
测试函数test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"seqlist.h"
void Test_PushBack()//测试顺序表尾插元素
{
print_seqlist("*****尾插3个元素至顺序表*****");
seqlist seq;
seqlistInit(&seq);
seqlistPushBack(&seq, 2);
seqlistPushBack(&seq, 4);
seqlistPushBack(&seq, 6);
PrintSeqList(&seq);
}
void Test_PopBack()//测试顺序表尾删元素
{
print_seqlist("*****尾删顺序表中的1个元素*****");
seqlist seq;
seqlistInit(&seq);
seqlistPushBack(&seq, 2);
seqlistPushBack(&seq, 4);
seqlistPushBack(&seq, 6);
PrintSeqList(&seq);
seqlistPopBack(&seq);
PrintSeqList(&seq);
}
void Test_PushHead()//头插元素
{
print_seqlist("*****头插顺序表中的3个元素*****");
seqlist seq;
seqlistInit(&seq);
seqlistPushHead(&seq, 2);
seqlistPushHead(&seq, 4);
seqlistPushHead(&seq, 6);
PrintSeqList(&seq);
}
void Test_PopHead()//头删元素
{
print_seqlist("*****头删顺序表中的1个元素*****");
seqlist seq;
seqlistInit(&seq);
seqlistPushHead(&seq, 2);
seqlistPushHead(&seq, 4);
seqlistPushHead(&seq, 6);
PrintSeqList(&seq);
seqlistPopHead(&seq);
PrintSeqList(&seq);
}
void Test_Find()//查找任意元素
{
print_seqlist("*****查找顺序表中的任意元素*****");
seqlist seq;
seqlistInit(&seq);
seqlistPushBack(&seq, 2);
seqlistPushBack(&seq, 4);
seqlistPushBack(&seq, 6);
PrintSeqList(&seq);
int temp = 0;
temp = seqlistFind(&seq,2);
printf("查找到的元素是%d\n", temp);
printf("\n");
}
void Test_Read_pos()//读取任意位置元素
{
print_seqlist("*****查找顺序表中的任意位置的元素*****");
seqlist seq;
seqlistInit(&seq);
seqlistPushBack(&seq, 2);
seqlistPushBack(&seq, 4);
seqlistPushBack(&seq, 6);
PrintSeqList(&seq);
int temp = 0;
temp=seqlistRead_pos(&seq, 1);
printf("下标为1的元素是%d\n", temp);
printf("下标为4的元素是:");
seqlistRead_pos(&seq, 4);
printf("\n");
}
void Test_seqlistFind_pos()//测试查找指定元素的下标
{
print_seqlist("*****查找顺序表中指定元素的下标*****");
seqlist seq;
seqlistInit(&seq);
seqlistPushBack(&seq, 2);
seqlistPushBack(&seq, 4);
seqlistPushBack(&seq, 6);
seqlistPushBack(&seq, 7);
PrintSeqList(&seq);
size_t pos = seqlistFind_pos(&seq, 4);
size_t pos1 = seqlistFind_pos(&seq, 9);
printf("元素4的下标为:%d\n", pos);
printf("元素9的下标为:%d,元素不存在\n", pos1);
printf("\n");
}
void Test_seqlistModify()//修改任意位置元素
{
print_seqlist("*****修改顺序表中任意元素*****");
seqlist seq;
seqlistInit(&seq);
seqlistPushBack(&seq, 2);
seqlistPushBack(&seq, 4);
seqlistPushBack(&seq, 6);
seqlistPushBack(&seq, 7);
PrintSeqList(&seq);
int temp = seqlistModify(&seq, 1, 3);
int temp1 = seqlistModify(&seq, 2, 8);
PrintSeqList(&seq);
printf("修改下标为1的元素为:%d\n", temp);
printf("修改下标为2的元素为:%d\n", temp1);
}
void Test_seqlistInsert()//在任意位置插入元素
{
print_seqlist("*****在顺序表中任意位置插入元素*****");
seqlist seq;
seqlistInit(&seq);
seqlistPushBack(&seq, 2);
seqlistPushBack(&seq, 4);
seqlistPushBack(&seq, 5);
seqlistPushBack(&seq, 6);
PrintSeqList(&seq);
seqlistInsert_pos(&seq, 2, 3);
PrintSeqList(&seq);
seqlistInsert_pos(&seq, 8, 9);
}
void Test_seqlistErase_pos()//删除任意位置元素
{
print_seqlist("*****在顺序表中删除任意位置元素*****");
seqlist seq;
seqlistInit(&seq);
seqlistPushBack(&seq, 2);
seqlistPushBack(&seq, 4);
seqlistPushBack(&seq, 5);
seqlistPushBack(&seq, 6);
PrintSeqList(&seq);
seqlistErase_pos(&seq, 1);
seqlistErase_pos(&seq, 4);
PrintSeqList(&seq);
}
void Test_Remove()//删除任意元素
{
print_seqlist("*****在顺序表中删除任意元素*****");
seqlist seq;
seqlistInit(&seq);
seqlistPushBack(&seq, 2);
seqlistPushBack(&seq, 4);
seqlistPushBack(&seq, 5);
seqlistPushBack(&seq, 6);
PrintSeqList(&seq);
seqlistRemove(&seq, 4);
PrintSeqList(&seq);
}
int main()
{
Test_PushBack();
Test_PopBack();
Test_PushHead();
Test_PopHead();
Test_Find();
Test_Read_pos();
Test_seqlistFind_pos();
Test_seqlistModify();
Test_seqlistInsert();
Test_seqlistErase_pos();
Test_Remove();
return 0;
}
您可能感兴趣的文章:C++实现动态顺序表(vector)C++实现动态顺序表C++顺序表的基本操作(使用模版类)C++顺序表的实例代码利用C++简单实现顺序表和单链表的示例代码C++实现顺序表的常用操作(插入删出查找输出)C++实现顺序表的方法如何在C++中建立一个顺序表