SeqList.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//#define N 200
//typedef int SLDataType;
//
静态顺序表 -- N太小,可能不够用,N太大,可能浪费空间
//struct SeqList
//{
// SLDataType a[N];
// int size;
//};
typedef int SLDataType;
// 动态顺序表
typedef struct SeqList
{
SLDataType* a; // 指向动态数组指针
int size; // 数据个数
int capacity; // 容量-空间大小
}SL;
void SLPrint(SL* ps);
// 增删查改
//void SeqListInit(SL s);
void SLInit(SL* ps);
void SLDestory(SL* ps);
void SLCheckCapacity(SL* ps);
// 头插/头删/尾插/尾插
// O(1)
void SLPushBack(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
// O(N)
void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);
// 任意位置插入删除
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
// 查找和修改
int SLFind(SL* ps, SLDataType x);
void SLModify(SL* ps, int pos, SLDataType x);
SeqList.c
#include "SeqList.h"
void SLPrint(SL* ps)
{
assert(ps != NULL);
//assert(ps);
for (int i = 0; i < ps->size; ++i)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void SLInit(SL* ps)
{
assert(ps != NULL);
ps->a = NULL;
ps->size = ps->capacity = 0;
}
void SLDestory(SL* ps)
{
assert(ps != NULL);
if (ps->a)
{
free(ps->a);
ps->a = NULL;
ps->capacity = ps->size = 0;
}
}
void SLCheckCapacity(SL* ps)
{
assert(ps != NULL);
// 检查容量空间,满了扩容
if (ps->size == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapacity*sizeof(SLDataType));
if (tmp == NULL)
{
printf("realloc fail\n");
//exit(-1);
return;
}
ps->a = tmp;
ps->capacity = newCapacity;
}
}
void SLPushBack(SL* ps, SLDataType x)
{
/*assert(ps != NULL);
SLCheckCapacity(ps);
ps->a[ps->size] = x;
ps->size++;*/
SLInsert(ps, ps->size, x);
}
void SLPushFront(SL* ps, SLDataType x)
{
//assert(ps != NULL);
//SLCheckCapacity(ps);
挪动数据
//int end = ps->size - 1;
//while (end >= 0)
//{
// ps->a[end + 1] = ps->a[end];
// --end;
//}
//ps->a[0] = x;
//ps->size++;
SLInsert(ps, 0, x);
}
void SLPopBack(SL* ps)
{
//assert(ps != NULL);
ps->a[ps->size - 1] = 0;
温柔检查
///*if (ps->size == 0)
//{
// printf("SeqList is empty\n");
// return;
//}*/
暴力检查
//assert(ps->size > 0);
//ps->size--;
SLErase(ps, ps->size - 1);
}
void SLPopFront(SL* ps)
{
/*assert(ps != NULL);
assert(ps->size > 0);
int begin = 1;
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
++begin;
}
ps->size--;*/
SLErase(ps, 0);
}
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
// 挪动数据
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[pos] = x;
ps->size++;
}
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
//int begin = pos;
//while (begin < ps->size-1)
//{
// ps->a[begin] = ps->a[begin + 1];
// ++begin;
//}
int begin = pos + 1;
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
++begin;
}
ps->size--;
}
int SLFind(SL* ps, SLDataType x)
{
assert(ps);
for (int i = 0; i < ps->size; ++i)
{
if (ps->a[i] == x)
return i;
}
return -1;
}
void SLModify(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
ps->a[pos] = x;
}
test.c
#include "SeqList.h"
void TestSeqList1()
{
SL sl;
SLInit(&sl);
SLPushBack(&sl, 1);
SLPushBack(&sl, 2);
SLPushBack(&sl, 3);
SLPushBack(&sl, 4);
SLPrint(&sl);
SLPushBack(&sl, 5);
SLPrint(&sl);
SLPushBack(&sl, 5);
SLPushBack(&sl, 5);
SLPushBack(&sl, 5);
SLPushBack(&sl, 5);
SLPrint(&sl);
}
void TestSeqList2()
{
SL sl;
SLInit(&sl);
SLPushFront(&sl, 1);
SLPushFront(&sl, 2);
SLPushFront(&sl, 3);
SLPushFront(&sl, 4);
SLPushFront(&sl, 5);
SLPrint(&sl);
SLPushBack(&sl, 5);
SLPushBack(&sl, 5);
SLPrint(&sl);
}
void TestSeqList3()
{
SL sl;
SLInit(&sl);
SLPushFront(&sl, 1);
SLPushFront(&sl, 2);
SLPushFront(&sl, 3);
SLPushFront(&sl, 4);
SLPushFront(&sl, 5);
SLPrint(&sl);
SLPopBack(&sl);
SLPopBack(&sl);
SLPopBack(&sl);
SLPopBack(&sl);
SLPopBack(&sl);
//SLPopBack(&sl);
SLPrint(&sl);
SLPushBack(&sl, 1);
SLPushBack(&sl, 2);
SLPushBack(&sl, 3);
SLPrint(&sl);
SLDestory(&sl);
}
void TestSeqList4()
{
SL sl;
SLInit(&sl);
//SL* psl = NULL;
//SLInit(psl);
SLPushFront(&sl, 1);
SLPushFront(&sl, 2);
SLPushFront(&sl, 3);
SLPushFront(&sl, 4);
SLPushFront(&sl, 5);
SLPrint(&sl);
SLPopFront(&sl);
SLPrint(&sl);
SLPopFront(&sl);
SLPopFront(&sl);
SLPopFront(&sl);
SLPopFront(&sl);
//SLPopFront(&sl);
SLPrint(&sl);
SLPushBack(&sl, 1);
SLPushBack(&sl, 2);
SLPushBack(&sl, 3);
SLPrint(&sl);
SLDestory(&sl);
}
void TestSeqList5()
{
SL sl;
SLInit(&sl);
SLPushFront(&sl, 1);
SLPushFront(&sl, 2);
SLPushFront(&sl, 3);
SLPushFront(&sl, 4);
SLPushFront(&sl, 5);
SLPrint(&sl);
SLInsert(&sl, 3, 40);
SLPrint(&sl);
SLInsert(&sl, 0, 40);
SLPrint(&sl);
SLPushBack(&sl, 1000);
SLPrint(&sl);
SLDestory(&sl);
}
void TestSeqList6()
{
SL sl;
SLInit(&sl);
SLPushFront(&sl, 1);
SLPushFront(&sl, 2);
SLPushFront(&sl, 3);
SLPushFront(&sl, 4);
SLPushFront(&sl, 5);
SLPrint(&sl);
SLErase(&sl, 2);
SLPrint(&sl);
SLErase(&sl, 0);
SLPrint(&sl);
SLErase(&sl, 2);
SLPrint(&sl);
SLPopFront(&sl);
SLPrint(&sl);
SLPopBack(&sl);
SLPrint(&sl);
}
void TestSeqList7()
{
SL sl;
SLInit(&sl);
SLPushFront(&sl, 1);
SLPushFront(&sl, 2);
SLPushFront(&sl, 2);
SLPushFront(&sl, 3);
SLPushFront(&sl, 2);
SLPushFront(&sl, 4);
SLPushFront(&sl, 5);
SLPrint(&sl);
//int x = 0;
//printf("请输入你要删除的值:");
//scanf("%d", &x);
//int pos = SLFind(&sl, x);
//if (pos != -1)
//{
// SLErase(&sl, pos);
//}
//else
//{
// printf("没找到:%d\n", x);
//}
//SLPrint(&sl);
int x = 0;
printf("请输入你要删除的值:");
scanf("%d", &x);
int pos = SLFind(&sl, x);
while (pos != -1)
{
SLErase(&sl, pos);
pos = SLFind(&sl, x);
}
SLPrint(&sl);
int y, z;
printf("请输入你要修改的值和修改后的值:");
scanf("%d%d", &y, &z);
pos = SLFind(&sl, y);
if (pos != -1)
{
SLModify(&sl, pos, z);
}
else
{
printf("没找到:%d\n", y);
}
SLPrint(&sl);
}
//int main()
//{
// TestSeqList7();
//
// // 越界是不一定报错的
// // 系统对越界的检查,设岗抽查。
// //int a[10];
// //a[0] = 0;
// a[10] = 1;
// a[11] = 1;
// //a[12] = 1;
// //a[15] = 1;
//
// return 0;
//}
void menu()
{
printf("****************************************\n");
printf("1、尾插 2、头插\n");
printf("5、查找 6、删除\n");
printf("7、修改 8、打印\n");
printf("-1、退出\n");
printf("****************************************\n");
}
int main()
{
SL sl;
SLInit(&sl);
int option = -1;
do
{
menu();
if (scanf("%d", &option) == EOF)
{
printf("scanf输入错误\n");
break;
}
int val, pos;
int y, z;
switch (option)
{
case 1:
printf("请连续输入你要尾插的数据,以0结束:>");
scanf("%d", &val);
while (val != 0)
{
SLPushBack(&sl, val);
scanf("%d", &val);
}
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
printf("请输入你要修改的值和修改后的值:");
scanf("%d%d", &y, &z);
pos = SLFind(&sl, y);
if (pos != -1)
{
SLModify(&sl, pos, z);
}
else
{
printf("没找到:%d\n", y);
}
break;
case 8:
SLPrint(&sl);
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (option != -1);
SLDestory(&sl);
return 0;
}