顺序表的定义
顺序表是用一段物理地址连续的存储单元一次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
顺序表一般可以分为:
1、静态顺序表:使用定长数组存储
2、动态顺序表:使用动态开辟的数组存储
顺序表的缺陷
顺序表的缺陷:
1、动态增容有性能消耗,并且伴随一定程度的空间浪费。
2、如果需要头部插入数据,需要挪动数据。
代码实现
静态顺序表实现
#pragma once
#define N 100
//静态顺序表的存储
struct SeqList
{
int a[N];
int size; //有效数据
};
动态顺序表实现
//SeqList.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SeqDataType;
typedef struct SeqList
{
SeqDataType*a; //指向动态开辟的数组
int size; //有效数据个数
int capacity; //容量空间的大小
}SeqList,SEQ;
//内存中管理数据的结构增删查改的接口
//初始化
void SeqListInit(SeqList* pq);
//销毁
void SeqListDestory(SeqList* pq);
//打印
void SeqListPrint(SeqList* pq);
//尾插
void SeqListPushBack(SeqList* pq, SeqDataType x);
//头插
void SeqListPushFront(SeqList* pq, SeqDataType x);
//尾删
void SeqListPopBack(SeqList* pq);
//头删
void SeqListPopFront(SeqList* pq);
//查找
int SeqListFind(SeqList*pq, SeqDataType x);
//插入
void SeqListInsert(SeqList*pq,int pos, SeqDataType x);
//删除
void SeqListErase(SeqList*pq, int pos);
//修改
void SeqListModify(SeqList* pq, int pos, SeqDataType x);
//SeqList.c
#include "SeqList.h"
void SeqListInit(SeqList* pq)
{
assert(pq);
pq->a = NULL;
pq->size = pq->capacity = 0;
}
void SeqListDestory(SeqList* pq)
{
free(pq->a);
pq->a = NULL;
pq->capacity = pq->size = 0;
}
void SeqListPrint(SeqList* pq)
{
assert(pq);
for (int i = 0; i < pq->size; i++)
{
printf("%d ", pq->a[i]);
}
printf("\n");
}
void SeqCheckCapacity(SeqList* pq)
{//满了,需要增容
if (pq->size == pq->capacity)
{
int newcapacity = pq->capacity ==0?4:pq->capacity* 2;
SeqDataType*newA = realloc(pq->a, sizeof(SeqDataType)*newcapacity);
if (newA == NULL)
{
printf("realloc fail\n");
exit(-1);
}
pq->a = newA;
pq->capacity = newcapacity;
}
}
void SeqListPushBack(SeqList* pq, SeqDataType x)
{
/*assert(pq);
SeqCheckCapacity(pq);
pq->a[pq->size] = x;
pq->size++;*/
SeqListInsert(pq, pq->size,x);
}
void SeqListPushFront(SeqList* pq, SeqDataType x)
{
/*assert(pq);
SeqCheckCapacity(pq);
int end = pq->size - 1;
while (end >= 0)
{
pq->a[end + 1] = pq->a[end];
end--;
}
pq->a[0] = x;
pq->size++;*/
SeqListInsert(pq, 0, x);
}
void SeqListPopBack(SeqList* pq)
{
/*assert(pq);
assert(pq->size>0);
pq->size--;*/
SeqListErase(pq, pq->size - 1);
}
void SeqListPopFront(SeqList* pq)
{
/*assert(pq);
assert(pq->size > 0);
int begin = 0;
while (begin < pq->size - 1)
{
pq->a[begin] = pq->a[begin + 1];
begin++;
}
pq->size--;*/
SeqListErase(pq, 0);
}
int SeqListFind(SeqList*pq, SeqDataType x)
{
assert(pq);
for (int i = 0; i < pq->size; i++)
{
if (pq->a[i] == x)
{
return i;
}
}
return -1;
}
void SeqListInsert(SeqList*pq, int pos, SeqDataType x)
{
assert(pq);
assert(pos >= 0 && pos <= pq->size);
SeqCheckCapacity(pq);
int end = pq->size - 1;
while (end >= pos)
{
pq->a[end + 1] = pq->a[end];
end--;
}
pq->a[pos] = x;
pq->size++;
}
void SeqListErase(SeqList*pq, int pos)
{
assert(pq);
assert(pos >= 0 && pos < pq->size);
int begin = pos;
while (begin <= pq->size - 1)
{
pq->a[begin] = pq->a[begin + 1];
begin++;
}
pq->size--;
}
void SeqListModify(SeqList* pq, int pos, SeqDataType x)
{
assert(pq);
assert(pos >= 0 && pos < pq->size);
pq->a[pos] = x;
}
//test.c
#include "SeqList.h"
void TestSeqList1()
{
SeqList s;
SeqListInit(&s);
SeqListPushBack(&s,1);
SeqListPushBack(&s,2);
SeqListPushBack(&s,3);
SeqListPushBack(&s,4);
SeqListPushBack(&s, 5);
SeqListPushFront(&s, 0);
SeqListPushFront(&s, 0);
SeqListPushFront(&s, 0);
SeqListPushFront(&s, 0);
SeqListPrint(&s);
SeqListPopBack(&s);
SeqListPrint(&s);
SeqListPopBack(&s);
SeqListPrint(&s);
SeqListPopFront(&s);
SeqListPrint(&s);
SeqListPopFront(&s);
SeqListPrint(&s);
SeqListDestory(&s);
}
void TestSeqList2()
{
SeqList s;
SeqListInit(&s);
SeqListPushBack(&s, 1);
SeqListPushBack(&s, 2);
SeqListPushBack(&s, 3);
SeqListPushBack(&s, 4);
SeqListPushBack(&s, 5);
SeqListPrint(&s);
SeqListInsert(&s, 2, 30);
SeqListPrint(&s);
SeqListErase(&s, 2);
SeqListErase(&s, 0);
SeqListPrint(&s);
SeqListModify(&s, 0, -1);
SeqListPrint(&s);
SeqListDestory(&s);
}
void menu()
{
printf("***************************************\n");
printf("****1、尾插数据 2、头插数据 ********\n");
printf("****3、尾删数据 4、头删数据*********\n");
printf("****5、查找数据 6、打印数据*********\n");
printf("**** 0、退出 ********\n");
printf("***************************************\n");
}
int main()
{
//TestSeqList1();
//TestSeqList2();
SeqList s;
SeqListInit(&s);
int option = 0;
do
{
menu();
scanf("%d", &option);
int x;
switch (option)
{
case 1:
printf("请输入数据:");
scanf("%d", &x);
SeqListPushBack(&s, x);
break;
case 2:
printf("请输入数据:");
scanf("%d", &x);
SeqListPushFront(&s, x);
break;
case 3:
SeqListPopBack(&s);
break;
case 4:
SeqListPopFront(&s);
break;
case 5:
printf("请输入数据:");
scanf("%d", &x);
SeqListFind(&s,x);
break;
case 6:
SeqListPrint(&s);
break;
case 0:
exit(-1);
break;
default:
break;
}
} while (option != 0);
SeqListDestory(&s);
return 0;
}