首先了解一下什么是顺序表?
顺序表:用一段地址连续的存储单元依次存储数据元素的线性结构。
地址连续的空间,一般情况下采用数组,但数组有静态数组和动态数组,所以顺序表分为静态顺序表和动态顺序表。
下面请看顺序表的基本操作:
SeqList.h
#pragma once
typedef int DataType;//定义数据类型
#define N 10//顺序表的大小
typedef struct SeqList
{
DataType _a[N];
size_t _size;
}SeqList;
//增删查改
void SeqListInit(SeqList* pSeq);//初始化
void SeqListPushBack(SeqList* pSeq, DataType x);//尾插
void SeqListpushFront(SeqList* pSeq, DataType x);//头插
void SeqListpopFront(SeqList* pSeq);//头删
void SeqListpopBack(SeqList* pSeq);//尾删
void SeqListInsert(SeqList*pSeq, size_t pos, DataType x);//在pos位置插入x
void SeqListErase(SeqList*pSeq, size_t pos);//在pos位置删除x
int SeqListFind(SeqList* pSeq, DataType x);//查找x
void SeqListModify(SeqList* pSeq, size_t pos, DataType x);//把pos位置数据修改为x
SeqList.c
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "SeqList.h"
void SeqListInit(SeqList* s)
{
memset(s->_a, 0, sizeof(DataType)*N);
s->_size = 0;
}
void SeqListPrint(SeqList* pSeq)
{
size_t i = 0;
for (i = 0; i < pSeq->_size; ++i)
{
printf("%d ", pSeq->_a[i]);
}
printf("\n");
}
void SeqListPushBack(SeqList* pSeq, DataType x)
{
assert(pSeq);
if (pSeq->_size >= N)
{
printf("SeqList is full!\n");
return;
}
else
{
pSeq->_a[pSeq->_size] = x;
pSeq->_size++;
}
}
void SeqListpushFront(SeqList* pSeq, DataType x)
{
assert(pSeq);
if (pSeq->_size >= N)
{
printf("SeqList is full!\n");
}
else
{
int end = pSeq->_size - 1;
while (end >= 0)
{
pSeq->_a[end + 1] = pSeq->_a[end];
--end;
}
pSeq->_a[0] = x;
pSeq->_size++;
}
}
void SeqListpopBack(SeqList* pSeq)
{
assert(pSeq);
if (pSeq->_size == 0)
{
printf("SeqList is empty!\n");
}
else
{
//pSeq->_a[pSeq->_size - 1] = 0;有可能类型不匹配
//pSeq->_size--;
pSeq->_size--;
}
}
void SeqListpopFront(SeqList* pSeq)
{
size_t i = 0;
assert(pSeq);
for (i = 0; i < pSeq->_size - 1; i++)
{
pSeq->_a[i] = pSeq->_a[i + 1];
}
pSeq->_size--;
}
void SeqListInsert(SeqList*pSeq, size_t pos, DataType x)
{
assert(pSeq);
assert(pos <= pSeq->_size);
if (pSeq->_size >= N)
{
printf("SeqList is full!\n");
}
else
{
int end = pSeq->_size - 1;
for (; end >= (int)pos; --end)//存在类型提升
{
pSeq->_a[end + 1] = pSeq->_a[end];
}
pSeq->_a[pos] = x;
pSeq->_size++;
}
}
void SeqListErase(SeqList* pSeq, size_t pos)
{
assert(pSeq);
assert(pos < pSeq->_size);
if (pSeq->_size == 0)
{
printf("SeqList is empty!\n");
}
else
{
int i = pos;
while (i < (int)pSeq->_size - 1)
{
pSeq->_a[i] = pSeq->_a[i + 1];
i++;
}
pSeq->_size--;
}
}
int SeqListFind(SeqList* pSeq, DataType x)
{
assert(pSeq);
int i = 0;
for (i = 0; i < (int)pSeq->_size; ++i)
{
if (pSeq->_a[i] == x)
{
return i;
}
}
return -1;
}
void SeqListModify(SeqList* pSeq, size_t pos, DataType x)
{
assert(pSeq && pos < pSeq->_size);
pSeq->_a[pos] = x;
}
void Test1()
{
SeqList s;
SeqListInit(&s);
SeqListPushBack(&s, 1);
SeqListPushBack(&s, 2);
SeqListPushBack(&s, 3);
SeqListPushBack(&s, 4);
SeqListPrint(&s);
SeqListpushFront(&s, 4);
SeqListpushFront(&s, 3);
SeqListpushFront(&s, 2);
SeqListpushFront(&s, 1);
SeqListPrint(&s);
SeqListpopBack(&s);
SeqListpopBack(&s);
SeqListPrint(&s);
SeqListpopFront(&s);
SeqListpopFront(&s);
SeqListPrint(&s);
SeqListInsert(&s, 2, 5);
SeqListInsert(&s, 0, 10);
SeqListPrint(&s);
SeqListErase(&s, 2);
SeqListPrint(&s);
SeqListErase(&s, 0);
SeqListPrint(&s);
SeqListFind(&s, 3);
SeqListPrint(&s);
}
void Test2()
{
SeqList s;
SeqListInit(&s);
SeqListPushBack(&s, 1);
SeqListPushBack(&s, 2);
SeqListPushBack(&s, 3);
SeqListPushBack(&s, 4);
SeqListPrint(&s);
printf("%d\n", SeqListFind(&s, 3));
SeqListModify(&s, 2, 10);
SeqListPrint(&s);
}
test.c
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "SeqList.h"
int main()
{
//Test1();
Test2();
return 0;
}