时间:2021-05-20
本文实例为大家分享了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;}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
C++数据结构线性表-数组实现线性表的数组实现,实现几个核心的功能,语言是C++,如果有更好的想法和意见,欢迎留言~~~/*Author:Moyiii*线性表的
本文以实例分析了C++语言中关于[]静态数组和new分配的动态数组的区别,可以帮助大家加深对C++语言数组的理解。具体区别如下:一、对静态数组名进行sizeof
C语言实现静态顺序表的实例详解线性表定义一张顺序表也就是在内存中开辟一段连续的存储空间,并给它一个名字进行标识。只有定义了一个顺序表,才能利用该顺序表存放数据元
本文实例为大家分享了C++实现顺序表的具体代码,供大家参考,具体内容如下#includeusingnamespacestd;typedefintDataType
C++实现静态单链表的实例利用数组实现的静态单链表,与严蔚敏书实现略有不同,不另设回收空间。有任何BUG或错误,希望各位朋友多多反馈~~感激不尽/*Author