静态顺序表:
- 定 义:存储的是一个静态数组
- 实现功能:顺序表的初始化、尾插、尾删、头插、头删、任意位置插入、任意位置删除、查找数据、移除某个元素、判空以及有效元素个数
代码实现:
头文件 SeqList.h
#pragma once
#define MAX_SIZE 10000
typedef unsigned int size_t;
typedef int DataType;//顺序表元素类型
typedef struct SeqList
{
DataType _array[MAX_SIZE];
int _size;//有效元素个数
}SeqList, *PSeqList;
void SeqListInit(PSeqList ps);//顺序表的初始化
void SeqListPushBack(PSeqList ps, DataType data);//尾插
void SeqListPopBack(PSeqList ps);//尾删
void SeqListPushFront(PSeqList ps, DataType data);//头插
void SeqListPopFront(PSeqList ps);//头删
void SeqListInsert(PSeqList ps, size_t pos, DataType data);//任意位置插入
void SeqListErase(PSeqList ps, size_t pos);//任意位置删除
int SeqListFind(PSeqList ps, DataType data);//查找数据
void SeqListRemove(PSeqList ps, DataType data);//移除某个元素
int SeqListEmpty(PSeqList ps);//判空
int SeqListSize(PSeqList ps);//有效元素个数
SeqList.c
#pragma once
#include<assert.h>
#include<stdio.h>
#include "SeqList.h"
void SeqListInit(PSeqList ps)//顺序表的初始化
{
if (NULL == ps)
return;
ps->_size = 0;
}
void SeqListPushBack(PSeqList ps, DataType data)//尾插
{
if (NULL == ps)
return;
if (ps->_size == MAX_SIZE)
return;
ps->_array[ps->_size++] = data;
}
void SeqListPopBack(PSeqList ps)//尾删
{
if (SeqListEmpty(ps))
return;
ps->_size--;
}
void SeqListPushFront(PSeqList ps, DataType data)//头插
{
int i;;
assert(ps);
if (ps->_size >= MAX_SIZE)
{
printf("顺序表已经满了!!!\n");
return;
}
for (i = ps->_size; i > 0; --i)
ps->_array[i] = ps->_array[i - 1];
ps->_array[0] = data;
ps->_size++;
}
void SeqListPopFront(PSeqList ps)//头删
{
int i = 0;
assert(ps);
if (SeqListEmpty(ps))
return;
for (i = 0; i < ps->_size - 1; ++i)
ps->_array[i] = ps->_array[i + 1];
ps->_size--;
}
void SeqListInsert(PSeqList ps, size_t pos, DataType data)//任意位置插入
{
int i;
assert(ps);
if (pos>ps->_size)
return;
if (ps->_size >= MAX_SIZE)
{
printf("顺序表已经满了!!!\n");
return;
}
for (i = ps->_size; i > pos; --i)
ps->_array[i] = ps->_array[i - 1];
ps->_array[pos] = data;
ps->_size++;
}
void SeqListErase(PSeqList ps, size_t pos)//任意位置删除
{
int i;
assert(ps);
if (pos >= ps->_size)
return;
if (SeqListEmpty(ps))
return;
for (i = pos + 1; i < ps->_size; ++i)
ps->_array[i - 1] = ps->_array[i];
ps->_size--;
}
int SeqListEmpty(PSeqList ps)//判空
{
assert(ps);
return 0 == ps->_size;
}
int SeqListSize(PSeqList ps)//有效元素个数
{
assert(ps);
return ps->_size;
}
int SeqListFind(PSeqList ps, DataType data)//查找数据
{
int i = 0;
assert(ps);
for (; i < ps->_size; ++i)
{
if (data == ps->_array[i])
return i;
}
return -1;
}
void SeqListRemove(PSeqList ps, DataType data)//移除某个元素
{
SeqListErase(ps, SeqListFind(ps, data));
}
void PrintSeqList(PSeqList ps)//打印顺序表
{
int i = 0;
assert(ps);
for (; i < ps->_size; ++i)
printf("%d", ps->_array[i]);
printf("\n");
}
main.c
#include"SeqList.h"
void TestSeqList()
{
SeqList s;
SeqListInit(&s);
SeqListPushBack(&s, 1);
SeqListPushBack(&s, 2);
SeqListPushBack(&s, 3);
SeqListPushBack(&s, 4);
SeqListPushBack(&s, 5);
PrintSeqList(&s);
SeqListRemove(&s, 2);
PrintSeqList(&s);
SeqListInsert(&s, 4, 7);
PrintSeqList(&s);
SeqListErase(&s, 3);
PrintSeqList(&s);
SeqListPushFront(&s, 0);
PrintSeqList(&s);
SeqListPopFront(&s);
PrintSeqList(&s);
SeqListPopBack(&s);
SeqListPopBack(&s);
PrintSeqList(&s);
}
int main()
{
TestSeqList();
return 0;
}