seqlist.h
#include
#include
#include
#pragma once
#define SeqListArrMax 100
typedef char SeqListType;
typedef struct SeqList
{
SeqListType SeqListArr[SeqListArrMax];
size_t size;
} SeqList;
void SeqListInit(SeqList *seqlist) ;//初始化顺序表
void SeqListPrint(SeqList *seqlist,char *c); //打印顺序表
void SeqListPushEnd(SeqList *seqlist,SeqListType value);//尾插
void SeqListPopEnd(SeqList *seqlist);//尾删
void SeqListPushStart(SeqList *seqlist ,SeqListType value); //头插
void SeqListPopStart(SeqList *seqlist);//头删
void SeqListRead(SeqList *seqlist ,size_t pos); //读取元素信息
void SeqListSet(SeqList *seqlist ,size_t pos,SeqListType value);//修改元素信息
void SeqListSetPos(SeqList *seqlist ,SeqListType value); //查找元素下标
void SeqListPushPos(SeqList *seqlist ,size_t pos,SeqListType value ) ; //任意下标插入元素
seqlist.c
#include
#include"seqlist.h"
#include
//初始化顺序表
void SeqListInit(SeqList *seqlist)
{
assert(seqlist);
seqlist->size = 0;
}
//打印顺序表
void SeqListPrint(SeqList *seqlist,char *ch)
{
assert(seqlist);
int i = 0;
printf("%s\n",ch);
for (i = 0;isize;i++)
{
printf("下标为%d的元素为%c\n",i,seqlist->SeqListArr[i]);
}
printf("size = %d \n",seqlist->size);
}
//尾插
void SeqListPushEnd(SeqList *seqlist,SeqListType value)
{
assert(seqlist);
if (seqlist->size >= SeqListArrMax)
{
printf("顺序表已满!\n");
}
seqlist->SeqListArr[seqlist->size] = value;
seqlist->size++;
}
//尾删
void SeqListPopEnd(SeqList *seqlist)
{
assert(seqlist);
if (seqlist->size == 0)
{
printf("空顺序表!\n");
return;
}
seqlist->size--;
}
//头插
void SeqListPushStart(SeqList *seqlist,SeqListType value)
{
assert(seqlist);
if (seqlist->size >= SeqListArrMax)
{
printf("顺序表已满!\n");
return ;
}
int i = seqlist->size;
for (;i>0;i--)
{
seqlist->SeqListArr[i+1] = seqlist->SeqListArr[i];
}
seqlist->SeqListArr[0] = value;
seqlist->size++;
}
//头删
void SeqListPopStart(SeqList *seqlist)
{
assert(seqlist);
if (seqlist->size == 0)
{
printf("空顺序表!\n");
return;
}
int i = 0;
for (i = 0;isize-1;i++)
{
seqlist->SeqListArr[i] = seqlist->SeqListArr[i+1];
}
seqlist->size--;
}
//读取信息
void SeqListRead(SeqList *seqlist,size_t pos)
{
assert(seqlist);
if(pos >= seqlist->size)
{
printf("输入有误!\n");
return;
}
printf("该元素为 %c \n",seqlist->SeqListArr[pos]);
}
//修改信息
void SeqListSet(SeqList *seqlist ,size_t pos,SeqListType value)
{
assert(seqlist);
if (pos>=seqlist->size)
{
printf("输入有误!\n");
return;
}
seqlist->SeqListArr[pos] = value;
}
//查找下标
void SeqListSetPos(SeqList *seqlist ,SeqListType value)
{
assert(seqlist);
int i = 0;
for (;isize;i++)
{
if(seqlist->SeqListArr[i] == value)
{
printf("元素下标为:%d \n",i);
}
}
printf("没有该元素!\n");
}
//任意位置插入元素
void SeqListPushPos(SeqList *seqlist ,size_t pos,SeqListType value )
{
assert(seqlist);
if(pos>=seqlist->size)
{
printf("输入有误!\n");
return;
}
if(seqlist->size>=SeqListArrMax)
{
printf("顺序表已满\n");
return;
}
int i = seqlist->size-1;
for (;i>pos;i--)
{
seqlist->SeqListArr[i+1] = seqlist->SeqListArr[i];
}
seqlist->SeqListArr[pos] = value;
seqlist->size++;
}
//test code
void TestSeqListInit()
{
SeqList seqlist;
SeqListInit(&seqlist);
SeqListPrint(&seqlist,"\n********Init************");
}
void TestSeqListPushEnd()
{
SeqList seqlist;
SeqListInit(&seqlist);
SeqListPushEnd(&seqlist,'a');
SeqListPushEnd(&seqlist,'b');
SeqListPushEnd(&seqlist,'c');
SeqListPrint(&seqlist,"\n********PushEnd************");
}
void TestSeqListPopEnd()
{
SeqList seqlist;
SeqListInit(&seqlist);
SeqListPushEnd(&seqlist,'a');
SeqListPushEnd(&seqlist,'b');
SeqListPushEnd(&seqlist,'c');
SeqListPopEnd(&seqlist);
SeqListPrint(&seqlist,"\n********PopEnd************");
}
void TestSeqListPushStart()
{
SeqList seqlist;
SeqListInit(&seqlist);
SeqListPushEnd(&seqlist,'c');
SeqListPushEnd(&seqlist,'d');
SeqListPushEnd(&seqlist,'e');
SeqListPushStart(&seqlist,'a');
SeqListPushStart(&seqlist,'b');
SeqListPrint(&seqlist,"\n**********PushStart************");
}
void TestSeqListPopStart()
{
SeqList seqlist;
SeqListInit(&seqlist);
SeqListPushEnd(&seqlist,'a');
SeqListPushEnd(&seqlist,'b');
SeqListPushEnd(&seqlist,'c');
SeqListPushEnd(&seqlist,'d');
SeqListPopStart(&seqlist);
SeqListPrint(&seqlist,"\n**********PushStart************");
}
void TestSeqListRead()
{
SeqList seqlist;
SeqListInit(&seqlist);
SeqListPushEnd(&seqlist,'a');
SeqListPushEnd(&seqlist,'b');
SeqListPushEnd(&seqlist,'c');
SeqListRead(&seqlist,2);
SeqListPrint(&seqlist,"\n**********Read************");
}
void TestSeqListSet()
{
SeqList seqlist;
SeqListInit(&seqlist);
SeqListPushEnd(&seqlist,'a');
SeqListPushEnd(&seqlist,'b');
SeqListPushEnd(&seqlist,'c');
SeqListPushEnd(&seqlist,'d');
SeqListPushEnd(&seqlist,'e');
SeqListSet(&seqlist,3,'g');
SeqListPrint(&seqlist,"\n**********Set************");
}
void TestSeqListSetPos()
{
SeqList seqlist;
SeqListInit(&seqlist);
SeqListPushEnd(&seqlist,'a');
SeqListPushEnd(&seqlist,'b');
SeqListPushEnd(&seqlist,'c');
SeqListPushEnd(&seqlist,'d');
SeqListPushEnd(&seqlist,'e');
SeqListSetPos(&seqlist,'c');
SeqListPrint(&seqlist,"\n**********SetPos************");
}
void TestSeqListPushPos()
{
SeqList seqlist;
SeqListInit(&seqlist);
SeqListPushEnd(&seqlist,'a');
SeqListPushEnd(&seqlist,'b');
SeqListPushEnd(&seqlist,'c');
SeqListPushEnd(&seqlist,'d');
SeqListPushEnd(&seqlist,'e');
SeqListPushEnd(&seqlist,'f');
SeqListPushPos(&seqlist,3,'w');
SeqListPrint(&seqlist,"\n**********PustPos************");
}
int main()
{
TestSeqListInit();
TestSeqListPushEnd();
TestSeqListPopEnd();
TestSeqListPushStart();
TestSeqListPopStart();
TestSeqListRead();
TestSeqListSet;
TestSeqListSetPos();
TestSeqListPushPos();
return 0;
}