**seqlit.h**
#pragma once
#define _SEQ_LIST_
#ifdef _SEQ_LIST_
#include<stdio.h>
#include<assert.h>
#include<string.h>
#define MAX_LEN 100
typedef int DataType;
typedef struct SeqList
{
DataType arr[MAX_LEN];
size_t size;
}SeqList;
typedef enum TAG
{
TURE,
FALSE,
}TAG;
typedef struct Findret
{
TAG Isfind;
size_t index;
}Findret;
void InitSeqList(SeqList* pSeq);
void PrintSeqList(SeqList* pSeq);
void PushBack(SeqList* pSeq, DataType x);
void PopBack(SeqList* pSeq);
void PushFront(SeqList* pSeq, DataType x);
void PopFront(SeqList* pSeq);
void Insert(SeqList* pSeq, size_t index, DataType x);
void Modified(SeqList* pSeq, size_t index, DataType x);
void removed(SeqList* pSeq, size_t index);
Findret Find(SeqList* pSeq, DataType x, size_t index);
TAG Erase(SeqList* pSeq, DataType x, TAG all);
#endif //_SEQ_LIST_
**seqlist.c**
#include"seqlist.h"
void InitSeqList(SeqList* pSeq)
{
assert(pSeq);
memset(pSeq->arr, 0, MAX_LEN * sizeof(DataType));
pSeq->size = 0;
}
void PrintSeqList(SeqList* pSeq)
{
int i = 0;
assert(pSeq);
for (; i < pSeq->size; i++)
{
printf("%d ", pSeq->arr[i]);
}
printf("\n");
}
void PushBack(SeqList* pSeq, DataType x)
{
assert(pSeq);
if ((pSeq->size) > MAX_LEN -1)
{
printf("SeqList is full\n");
return;
}
pSeq->arr[(pSeq->size)++] = x;
}
void PopBack(SeqList* pSeq)
{
assert(pSeq);
if (pSeq->size == 0)
{
printf("SeqList is empty\n");
return;
}
pSeq->arr[--(pSeq->size)] = 0;
}
void PushFront(SeqList* pSeq, DataType x)
{
int i = 0;
assert(pSeq);
if ((pSeq->size) > MAX_LEN - 1)
{
printf("SeqList is full\n");
return;
}
for (i = pSeq->size; i > 0; i--)
{
pSeq->arr[i] = pSeq->arr[i - 1];
}
pSeq->arr[0] = x;
++(pSeq->size);
}
void PopFront(SeqList* pSeq)
{
int i = 0;
assert(pSeq);
if (pSeq->size == 0)
{
printf("SeqList is empty\n");
return;
}
for (; i < pSeq->size; i++)
{
pSeq->arr[i] = pSeq->arr[i + 1];
}
(pSeq->size)--;
}
void Insert(SeqList* pSeq, size_t index, DataType x)
{
int i = 0;
assert(pSeq);
assert(index <=pSeq->size);
if ((pSeq->size) > MAX_LEN - 1)
{
printf("SeqList is full\n");
return;
}
for (i = pSeq->size; i > index; i--)
{
pSeq->arr[i] = pSeq->arr[i - 1];
}
pSeq->arr[index] = x;
(pSeq->size)++;
}
void Modified(SeqList* pSeq, size_t index, DataType x)
{
assert(pSeq);
assert(index <= pSeq->size);
pSeq->arr[index] = x;
}
void removed(SeqList* pSeq, size_t index)
{
int i = 0;
assert(pSeq);
assert(index <= pSeq->size);
for (i = index; i < pSeq->size; i++)
{
pSeq->arr[i] = pSeq->arr[i + 1];
}
(pSeq->size)--;
}
Findret Find(SeqList* pSeq, DataType x, size_t index)
{
Findret ret;
assert(pSeq);
for (; index < pSeq->size; index ++)
{
if (pSeq->arr[index] == x)
{
ret.Isfind = TURE;
ret.index = index;
return ret;
}
}
ret.Isfind = FALSE;
return ret;
}
TAG Erase(SeqList* pSeq, DataType x, TAG all)
{
TAG success = FALSE;
Findret ret;
assert(pSeq);
ret = Find(pSeq, x, 0);
while (ret.Isfind == TURE)
{
success = TURE;
removed(pSeq, ret.index);
if (all == FALSE)
{
break;
}
ret = Find(pSeq, x, ret.index);
}
return success;
}
**main.c**
#include"seqlist.h"
void test()
{
SeqList s;
InitSeqList(&s);
PushBack(&s, 1);
PushBack(&s, 2);
PushBack(&s, 3);
PushBack(&s, 4);
PopBack(&s);
PushFront(&s, 10);
PopFront(&s);
Insert(&s, 2, 10);
Modified(&s, 3, 11);
removed(&s, 1);
PrintSeqList(&s);
}
void test2()
{
SeqList s;
InitSeqList(&s);
Insert(&s, 0, 0);
Insert(&s, 1, 1);
Insert(&s, 2, 2);
Insert(&s, 3, 2);
Insert(&s, 4, 3);
Insert(&s, 5, 2);
PrintSeqList(&s);
Erase(&s, 2, FALSE);
PrintSeqList(&s);
Erase(&s, 2, TURE);
PrintSeqList(&s);
}
int main()
{
test();
test2();
getchar();
return 0;
}