SeqList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void SeqListInit(SeqList* pSeq)
{
assert(pSeq != NULL);
pSeq->sz = 0;
memset(pSeq->data,0,sizeof(pSeq->data));
}
void SeqListPushBack(SeqList* pSeq,DataType data)
{
assert(pSeq != NULL);
if(pSeq->sz == MAX)
{
printf("顺序表已满,禁止插入\n");
return;
}
pSeq->data[pSeq->sz] = data;
pSeq->sz++;
}
void PrintSeqList(pSeqList pSeq)
{
int i =0;
assert(pSeq != NULL);
for(i=0;i<pSeq->sz;i++)
{
printf("%d ",pSeq->data[i]);
}
printf("\n");
}
void SeqListPushFront(SeqList* pSeq, DataType data)
{
int i = 0;
assert(pSeq != NULL);
for(i=pSeq->sz-1;i>=0;i--)
{
pSeq->data[i+1] = pSeq->data[i];
}
pSeq->data[0] = data;
pSeq->sz++;
}
void SeqListPopFront(SeqList* pSeq)
{
int i = 0;
assert(pSeq != NULL);
for(i=0;i<pSeq->sz-1;i++)
{
pSeq->data[i] = pSeq->data[i+1];
}
pSeq->sz--;
}
void SeqListInsert(SeqList* pSeq, int pos, DataType data)
{
int i = 0;
assert(pSeq != NULL);
for(i=pSeq->sz; i>=pos-1;i--)
{
pSeq->data[i] = pSeq->data[i-1];
}
pSeq->data[pos-1] = data;
pSeq->sz++;
}
void SeqListErase(SeqList* pSeq, int pos)
{
int i =0;
assert(pSeq != NULL);
for(i=pos-1;i<pSeq->sz-1;i++)
{
pSeq->data[i] = pSeq->data[i+1];
}
pSeq->sz--;
}
void SeqListRemove(SeqList* pSeq, DataType data)
{
int ret =0;
assert(pSeq != NULL);
ret = SeqListListFind(pSeq,data);
if (ret != -1)
SeqListErase(pSeq,ret);
}
void SeqListRemoveAll(SeqList* pSeq, DataType data)
{
int i = 0,j=0,count=0;
assert(pSeq != NULL);
for (i = 0; i<pSeq->sz; i++)
{
if (pSeq->data[i] == data)
count++;
else
{
pSeq->data[j] = pSeq->data[i];
j++;
}
}
pSeq->sz -= count;
/*int i = 0;
int ret = 0;
assert(pSeq != NULL);
for(i=0;i<pSeq->sz;i++)
{
while(i != pSeq->sz-1)
SeqListErase(pSeq,SeqListListFind((pSeq->data[i]),data));
}*/
}
int SeqListListFind(SeqList* pSeq, DataType data)
{
int i =0;
assert(pSeq != NULL);
for(i=0;i<pSeq->sz;i++)
{
if(pSeq->data[i] == data)
return i+1;
}
return -1;
}
int SeqListSize(SeqList* pSeq)
{
return pSeq->sz;
/*int i = 0;
int count = 0;
assert(pSeq != NULL);
while(pSeq->data[i])
{
i++;
count++;
}
return ((count-2)*sizeof(DataType));*/
}
SeqList.h
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include<stdio.h>
#include<assert.h>
#include<string.h>
#define MAX 10
typedef int DataType;
typedef struct SeqList
{
DataType data[MAX];
int sz;
}SeqList,*pSeqList;
void SeqListInit(SeqList* pSeq);//初始化
void SeqListPushBack(SeqList* pSeq,DataType data);//尾插
void PrintSeqList(pSeqList pSeq);//打印
void SeqListPushFront(SeqList* pSeq, DataType data);//头插
void SeqListPopFront(SeqList* pSeq);//头删
void SeqListInsert(SeqList* pSeq, int pos, DataType data);//中间插入
void SeqListErase(SeqList* pSeq, int pos);//橡皮擦删除
void SeqListRemove(SeqList* pSeq, DataType data);//删除第一个匹配元素
void SeqListRemoveAll(SeqList* pSeq, DataType data);//删除所有匹配元素
int SeqListListFind(SeqList* pSeq, DataType data);//查找匹配元素并返回序列号
int SeqListSize(SeqList* pSeq);//大小值
#endif //__SEQLIST_H__
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void test()
{
SeqList pSeq;
SeqListInit(&pSeq);
SeqListPushBack(&pSeq,1);
SeqListPushBack(&pSeq,2);
SeqListPushBack(&pSeq,2);
SeqListPushBack(&pSeq,2);
SeqListPushBack(&pSeq,2);
SeqListPushBack(&pSeq,3);
SeqListPushBack(&pSeq,4);
SeqListPushBack(&pSeq,5);
SeqListPushBack(&pSeq,6);
PrintSeqList(&pSeq);
SeqListPushFront(&pSeq,7);
SeqListPushFront(&pSeq,8);
PrintSeqList(&pSeq);
SeqListPopFront(&pSeq);
PrintSeqList(&pSeq);
SeqListInsert(&pSeq,4,66);
PrintSeqList(&pSeq);
SeqListErase(&pSeq,6);
PrintSeqList(&pSeq);
printf("%d\n",SeqListListFind(&pSeq,66));
SeqListRemove(&pSeq,3);
PrintSeqList(&pSeq);
printf("%d\n",SeqListSize(&pSeq));
SeqListRemoveAll(&pSeq,2);
PrintSeqList(&pSeq);
}
int main()
{
test();
return 0;
}
结果演示