7.在指定位置删除元素
Seqlist.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include
#define SeqlistMaxsize 100
typedef char SeqlistType;
typedef struct Seqlist{
SeqlistType data[SeqlistMaxsize];
size_t size;
}Seqlist;
void SeqlistInit(Seqlist *seqlist);//初始化
void pushback(Seqlist *seqlist, SeqlistType value);//尾插
void deleteback(Seqlist *seqlist);//尾删
void SeqlistpushFront(Seqlist *seqlist, SeqlistType value);//头插
void Seqlistpopback(Seqlist *seqlist);//头部删除
void SeqlistInsert(Seqlist *seqlist, size_t num, SeqlistType value);//在指定位置插入元素
void SeqlistDelete(Seqlist *seqlist, size_t num);//在指定位置删除元素
Seqlist.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "seqlist.h"
#include
#define PrintHeader printf("\n=================%s=================\n",__FUNCTION__)
void SeqlistInit(Seqlist *seqlist)
{
if (seqlist == NULL)
{
return;
}
seqlist->size = 0;
}
void pushback(Seqlist *seqlist, SeqlistType value)
{
if (seqlist == NULL)
{
return;
}
if (seqlist->size >= SeqlistMaxsize)
{
printf("内存已满\n");
return;
}
seqlist->data[seqlist->size] = value;
++seqlist->size;
return;
}
void deleteback(Seqlist *seqlist)
{
if (seqlist == NULL)
{
return;
}
if (seqlist->size == 0)
{
return;
}
--seqlist->size;
}
void printSeqlist(Seqlist *seqlist,char *msg)
{
if (seqlist == NULL)
{
return;
}
printf("%s\n", msg);
SeqlistType i = 0;
for (; i < seqlist->size; i++)
{
printf("%c ", seqlist->data[i]);
}
printf("\n");
}
void SeqlistpushFront(Seqlist *seqlist, SeqlistType value)
{
if (seqlist == NULL){
return;
}
if (seqlist->size >= SeqlistMaxsize){
return;
}
++seqlist->size;
size_t i = seqlist->size - 1;
for (; i > 0; --i)
{
seqlist->data[i] = seqlist->data[i-1];
}
seqlist->data[0] = value;
return;
}
void Seqlistpopback(Seqlist *seqlist)
{
if (seqlist == NULL){
return;
}
size_t i =0;
for (; isize-1; i++)
{
seqlist->data[i] = seqlist->data[i+1];
}
--seqlist->size;
return;
}
void SeqlistInsert(Seqlist *seqlist, size_t num, SeqlistType value)
{
if (seqlist == NULL){
return;
}
++seqlist->size;
size_t i = seqlist->size-1;
for (; i > num; i--)
{
seqlist->data[i] = seqlist->data[i - 1];
}
seqlist->data[num] = value;
return;
}
void SeqlistDelete(Seqlist *seqlist, size_t num)
{
if (seqlist == NULL){
return;
}
if (seqlist->size == 0){
return;
}
size_t i = num;
for (; i < seqlist->size-1; i++)
{
seqlist->data[i] = seqlist->data[i + 1];
}
--seqlist->size;
return;
}
测试代码//
void testinit()
{
PrintHeader;
Seqlist seqlist;
SeqlistInit(&seqlist);
printf("%lu\n", seqlist.size);
}
void testpushback()
{
PrintHeader;
Seqlist seqlist;
SeqlistInit(&seqlist);
pushback(&seqlist, 'a');
pushback(&seqlist, 'b');
pushback(&seqlist, 'c');
printSeqlist(&seqlist,"尾部插入三个元素");
}
void testdeleteback()
{
PrintHeader;
Seqlist seqlist;
SeqlistInit(&seqlist);
deleteback(&seqlist);
printSeqlist(&seqlist, "对空顺序表进行尾删");
pushback(&seqlist, 'a');
pushback(&seqlist, 'b');
pushback(&seqlist, 'c');
deleteback(&seqlist);
//deleteback(&seqlist);
printSeqlist(&seqlist, "对顺序表删除1个");
}
void testSeqlistpushFront()
{
PrintHeader;
Seqlist seqlist;
SeqlistInit(&seqlist);
SeqlistpushFront(&seqlist, 'O');
SeqlistpushFront(&seqlist, 'L');
printSeqlist(&seqlist, "对顺序表头插入两个元素");
}
void testSeqlistpopback()
{
PrintHeader;
Seqlist seqlist;
SeqlistInit(&seqlist);
pushback(&seqlist, 'a');
pushback(&seqlist, 'b');
pushback(&seqlist, 'c');
Seqlistpopback(&seqlist);
printSeqlist(&seqlist, "对顺序表头部删除一个元素");
}
void testSeqlistInsert()
{
PrintHeader;
Seqlist seqlist;
SeqlistInit(&seqlist);
pushback(&seqlist, 'a');
pushback(&seqlist, 'b');
pushback(&seqlist, 'c');
SeqlistInsert(&seqlist, 1, 'p');
printSeqlist(&seqlist, "对顺序表指定位置插入一个元素");
}
void testSeqlistDelete()
{
PrintHeader;
Seqlist seqlist;
SeqlistInit(&seqlist);
pushback(&seqlist, 'a');
pushback(&seqlist, 'b');
pushback(&seqlist, 'c');
SeqlistDelete(&seqlist, 1);
printSeqlist(&seqlist, "对顺序表指定位置删除一个元素");
}
///
int main()
{
testinit();
testpushback();
testdeleteback();
testSeqlistpushFront();
testSeqlistpopback();
testSeqlistInsert();
testSeqlistDelete();
system("pause");
return 0;
}