线性表(List):零个或多个数据元素的有限序列
1. 首先它是一个序列。也就是说,元素之间是有顺序的,若元素存在多个,则第一个元素无前驱,最后一个元素无后继,其他每个元素都有且只有一个前驱和后继。
2. 然后,线性表强调是有限的。事实上,在计算机中处理的对象都是有限的,那种无限的数列,只存在于数学的概念中。
如果用数学语言来进行定义:
若将线性表记为(a1, ……, ai-1, ai, ai+1, ……,an),则表中ai-1领先于ai,ai领先于ai+1,称ai-1是ai的直接前驱元素,ai+1是ai的直接后继元素。当丨=1, 2, ……, n-1时,ai有且仅有一个直接后继,当i=2, 3, ……, n时,ai有且仅有一个直接前驱。
所以线性表元素的个数n (n>0)定义为线性表的长度,当n=0时,称为空表。
在非空表中的每个数据元素都有一个确定的位置,如a1是第一个数据元素,an是最后一个数据元素,ai是第i个数据元素,称i为数据元素ai在线性表中的位序。
这就是线性表的定义。
这篇文章主要介绍顺序存储结构,有了上面的定义,下一步就看它有什么具体的操作,也就是接口。
//SeqList.h
#pragma once
#define MAX_SIZE 1024
typedef int DataType;
typedef struct SeqList {
DataType array[MAX_SIZE];
int size; // size两个含义 1、表示数组中有多少个元素
// 2、表示当前可操作下表
}SeqList;
//接口
//初始化/销毁
//增删改查
void SeqListInit(SeqList* pSeq);
void SeqListDestroy(SeqList* pSeq);
void SeqListPushFront(SeqList* pSeq, DataType data);
void SeqListPushBack(SeqList* pSeq, DataType data);
void SeqListInsert(SeqList* pSeq, DataType data, int pos);
void SeqListPopFront(SeqList * pSeq);
void SeqListPopBack(SeqList* pSeq);
void SeqListEarse(SeqList* pSeq, int pos);
int SeqListFind(SeqList* pSeq, DataType Data);
void SeqListPrint(SeqList* pSeq);
//SeqList.c
#include "SeqList.h"
#include <assert.h>
#include <stdio.h>
void SeqListInit(SeqList* pSeq) {
assert(pSeq != NULL);
pSeq->size = 0;
}
void SeqListDestroy(SeqList* pSeq) {
assert(pSeq != NULL);
pSeq->size = 0;
}
void SeqListPushFront(SeqList* pSeq, DataType data) {
assert(pSeq != NULL);
//特殊情况 满了
if (pSeq->size >= MAX_SIZE)
{
printf("满了\n");
assert(0); // ??????
return 0;
}
int i;
for (i = pSeq->size-1; i >= 0; i--)
{
pSeq->array[i+1] = pSeq->array[i];
}
pSeq->array[0] = data;
pSeq->size++;
}
void SeqListPushBack(SeqList* pSeq, DataType data) {
assert(pSeq != NULL);
if (pSeq->size >= MAX_SIZE)
{
printf("满了\n");
assert(0);
return 0;
}
pSeq->array[pSeq->size] = data;
pSeq->size++;
}
void SeqListInsert(SeqList* pSeq, DataType data, int pos) {
assert(pSeq != NULL);
assert(pos > 0 && pos < pSeq->size - 1);
if (pSeq->size >= MAX_SIZE)
{
printf("满了\n");
assert(0);
return 0;
}
//i 表示位置
for (int i = pSeq->size; i > pos; i--)
{
pSeq->array[i] = pSeq->array[i - 1];
}
pSeq->array[pos] = data;
pSeq->size++;
}
void SeqListPopFront(SeqList * pSeq) {
assert(pSeq != NULL);
if (pSeq->size == 0)
{
printf("空了\n");
assert(0);
return 0;
}
for (int i = 0; i < pSeq->size - 1; i++)
{
pSeq->array[i] = pSeq->array[i + 1];
}
pSeq->size--;
}
void SeqListPopBack(SeqList* pSeq) {
assert(pSeq != NULL);
if (pSeq->size == 0)
{
printf("空了\n");
assert(0);
return 0;
}
pSeq->size--;
}
void SeqListEarse(SeqList* pSeq, int pos) {
assert(pSeq != NULL);
assert(pos > 0 && pos < pSeq->size - 1);
if (pSeq->size == 0)
{
printf("空了\n");
assert(0);
return 0;
}
for (int i = pos; i <= pSeq->size - 2; i++)
{
pSeq->array[i] = pSeq->array[i + 1];
}
pSeq->size--;
}
int SeqListFind(SeqList* pSeq, DataType data) {
assert(pSeq != NULL);
for (int i = 0; i < pSeq->size; i++)
{
if (pSeq->array[i] == data)
return i;
}
return 0;
}
void SeqListPrint(SeqList* pSeq) {
assert(pSeq != NULL);
for (int i = 0; i < pSeq->size; i++)
{
printf("%d ", pSeq ->array[i]);
}
printf("\n");
}
//main.c
#include "SeqList.h"
void test()
{
SeqList s1;
SeqListInit(&s1);
SeqListPushBack(&s1, 1);
SeqListPushBack(&s1, 2);
SeqListPushBack(&s1, 3);
SeqListPushBack(&s1, 5);
SeqListPushBack(&s1, 6);
SeqListPushBack(&s1, 7);
SeqListInsert(&s1, 4, 3);
SeqListPushFront(&s1, 0);
SeqListPrint(&s1);
SeqListPopFront(&s1);
SeqListPrint(&s1);
SeqListPopBack(&s1);
SeqListPrint(&s1);
SeqListEarse(&s1, 3);
SeqListPrint(&s1);
}
int main()
{
test();
getchar();
return 0;
}