(一)静态顺序表的实现
SeqList.h文件
#pragma once
#define MAX_SIZE 1000
typedef int DataType;
typedef unsigned int size_t;
typedef struct SeqList
{
DataType array[MAX_SIZE];
size_t size;
}SeqList;
void InitSeqList(SeqList* pSeq);
void PushBack(SeqList* pSeq, DataType x);
void PopBack(SeqList* pSeq);
void PushFront(SeqList* pSeq, DataType x);
void PopFront(SeqList* pSeq);
void Insert(SeqList* pSeq, size_t pos, DataType x);
int Find(SeqList* pSeq, DataType x);
void Erase(SeqList* pSeq, size_t x);
int Remove(SeqList* pSeq, DataType x);
int RemoveAll(SeqList* pSeq, DataType x);
void BubbleSort(SeqList* pSeq);
void SelectSort(SeqList* pSeq);
int BinarySearch(SeqList* pSeq, DataType x);
void PrintSeqList(SeqList* pSeq);
SeqList.c文件
#include "SeqList.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
void InitSeqList(SeqList* pSeq)
{
memset(pSeq->array, 0, sizeof(DataType) * MAX_SIZE);
pSeq->size = 0;
}
//
//1.检查参数
//2.检查边界条件
//3.实现函数功能
//
void PushBack(SeqList* pSeq,DataType x)
{
assert(pSeq);
if(pSeq ->size >= MAX_SIZE)
{
printf("SeqList is fulled\n");
return;
}
else
{
pSeq ->array[pSeq -> size++] = x;
}
}
void PopBack(SeqList* pSeq)
{
assert(pSeq);
if(pSeq ->size == 0)
{
printf("SeqList is Empty\n");
return;
}
else
{
pSeq -> array[pSeq -> size] = 0;
pSeq -> size --;
}
}
void PushFront(SeqList* pSeq, DataType x)
{
size_t i = pSeq ->size;
assert(pSeq);
while(i > 0)
{
pSeq -> array[i] = pSeq->array[i - 1];
i--;
}
pSeq ->array[0] = x;
pSeq ->size++;
}
void PopFront(SeqList* pSeq)
{
size_t i = 1;
assert(pSeq);
while(i <= pSeq->size - 1)
{
pSeq -> array[i - 1] = pSeq->array[i];
i++;
}
pSeq ->array[pSeq->size - 1] = 0;
pSeq ->size--;
}
void Insert(SeqList* pSeq, size_t pos, DataType x)
{
int i = (int)pSeq ->size - 1;
assert(pSeq);
assert(pos >= 0 && pos <= pSeq->size);
while(pos <= i && i >= 0) //i >= 0;解决问题 :0位置插入
{
pSeq ->array[i + 1] = pSeq ->array[i];
i--;
}
pSeq ->array[pos] = x;
pSeq ->size++;
}
int Find(SeqList* pSeq, DataType x)
{
int i = pSeq->size - 1;
assert(pSeq);
while(i >= 0)
{
if(pSeq->array[i] == x)
{
return i;
}
i--;
}
return -1;
}
void Erase(SeqList* pSeq, size_t x) //将第几个数删除 0始
{
int i = x;
assert(pSeq);
assert(x >= 0 && x < pSeq->size);
while(i < pSeq->size - 1)
{
pSeq->array[i] = pSeq->array[i + 1];
i++;
}
pSeq->size--;
}
int Remove(SeqList* pSeq, DataType x)
{
int i = 0;
assert(pSeq);
while(i < pSeq->size)
{
if(pSeq->array[i] == x)
{
Erase(pSeq, i);
return i;
}
i++;
}
return -1;
}
int RemoveAll(SeqList* pSeq, DataType x)
{
int count = 0;
int i = 0;
while(i < pSeq->size)
{
if(pSeq->array[i] == x)
{
count++;
}
else
{
pSeq->array[i - count] = pSeq->array[i];
}
i++;
}
pSeq->size -= count;
return count;
}
void BubbleSort(SeqList* pSeq)
{
int i = 0;
int j = 0;
assert(pSeq);
for(i = 0;i < pSeq->size - 1; i++)
for(j = 0; j < pSeq->size - 1 - i; j++)
{
if(pSeq->array[j] > pSeq->array[j + 1])
{
int tmp = pSeq->array[j + 1];
pSeq->array[j + 1] = pSeq->array[j];
pSeq->array[j] = tmp;
}
}
}
void SelectSort(SeqList* pSeq)//一次选出最大最小的数据分别放在序列两边
{
int left = 0;
int right = pSeq->size - 1;
}
int BinarySearch(SeqList* pSeq, DataType x)
{
int left = 0;
int right = pSeq->size - 1;
assert(pSeq);
while(left <= right)
{
int mid = left + (right - left) / 2;
if(x < pSeq->array[mid])
{
right = mid - 1;
}
else if(x > pSeq->array[mid])
{
left = mid + 1;
}
else
return mid;
mid = left + (right - left) / 2;
}
return -1;
}
void PrintSeqList(SeqList* pSeq)
{
int i = 0;
assert(pSeq);
while(i < pSeq ->size)
{
printf("%d -> ",pSeq -> array[i]);
i++;
}
}
text.c文件
#include "SeqList.h"
#include <stdio.h>
#include <stdlib.h>
void test1()
{
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PopBack(&seq);
PopBack(&seq);
PopBack(&seq);
PopBack(&seq);
PopBack(&seq);
PrintSeqList(&seq);
}
void test2()
{
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PushFront(&seq, 1);
PopFront(&seq);
PrintSeqList(&seq);
}
void test3()
{
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
//Insert(&seq,0,5);
Insert(&seq,4,5);
PrintSeqList(&seq);
}
void test4()
{
int ret;
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
ret = Find(&seq, 4);
if(ret >= 0)
{
printf("pos:%d\n", ret);
}
else
{
printf("No Exit!\n");
}
}
void test5()
{
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
//Erase(&seq, 0);
Erase(&seq, 3);
PrintSeqList(&seq);
}
void test6()
{
int ret;
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
//ret = Remove(&seq, 3);
ret = Remove(&seq, 5);
if(ret < 0)
{
printf("No Exit!\n");
}
PrintSeqList(&seq);
}
void test7()
{
int ret;
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 2);
PushBack(&seq, 4);
PushBack(&seq, 2);
ret = RemoveAll(&seq, 2);
//ret = RemoveAll(&seq, 5);
if(ret == 0)
{
printf("No Exit!\n");
}
PrintSeqList(&seq);
}
void test8()
{
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 7);
PushBack(&seq, 2);
PushBack(&seq, 6);
PushBack(&seq, 3);
PushBack(&seq, 5);
PushBack(&seq, 4);
PushBack(&seq, 1);
BubbleSort(&seq);
PrintSeqList(&seq);
}
void test9()
{
int ret = 0;
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PushBack(&seq, 5);
PushBack(&seq, 6);
PushBack(&seq, 7);
//ret = BinarySearch(&seq, 1);
//ret = BinarySearch(&seq, 8);
//ret = BinarySearch(&seq, 4);
ret = BinarySearch(&seq, 5);
if(ret < 0)
printf("No Exit!\n");
else
printf("%d\n", ret);
PrintSeqList(&seq);
}
int main()
{
//test1();
//test2();
//test3();
//test4();
//test5();
//test6();
//test7();
//test8();
test9();
system("pause");
return 0;
}
(二)动态顺序表的实现(简略:只有部分主要功能)
SeqList_D.h文件
#pragma once
typedef int DataType;
typedef unsigned size_t;
typedef struct SeqList_D
{
DataType* array;
//指向数据块的指针
size_t size; //当前数据数
size_t capacity;
//总容量
}SeqList_D;
void InitSeqList_D(SeqList_D* pseq_d);
int IsFullCapacity(SeqList_D* pseq_d);
int IsEmptyCapacity(SeqList_D* pseq_d);
void PushBack(SeqList_D* pseq_d,DataType data);
void PopBack(SeqList_D* pseq_d);
void PrintSeq(SeqList_D* pseq_d);
SeqList_D.c
#include "SeqList_D.h"
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
void InitSeqList_D(SeqList_D* pseq_d)
{
assert(pseq_d);
pseq_d->array = (DataType*)malloc(sizeof(DataType) * 3);
pseq_d->size = 0;
pseq_d->capacity = 3;
}
void PushBack(SeqList_D* pseq_d,DataType data)
{
assert(pseq_d);
if(IsFullCapacity(pseq_d))//full
{
pseq_d->capacity = pseq_d->capacity * 2 + 3;
pseq_d->array = (DataType*)realloc(pseq_d->array, sizeof(DataType) * pseq_d->capacity);
}
pseq_d->array[pseq_d->size++] = data;
}
int IsFullCapacity(SeqList_D* pseq_d)
{
if(pseq_d->size >= pseq_d->capacity)
{
return 1; //full
}
else
{
return 0;
}
}
int IsEmptyCapacity(SeqList_D* pseq_d)
{
if(pseq_d->size <= 0)
{
return 1; //empty
}
else
return 0;
}
void PopBack(SeqList_D* pseq_d)
{
if(IsEmptyCapacity)
{
printf("SeqList is Eempty");
return;
}
else
{
pseq_d->size--;
}
}
void PrintSeq(SeqList_D* pseq_d)
{
int i = 0;
for(;i < pseq_d->size; i++)
{
printf("%d->", pseq_d->array[i]);
}
}
test.c 文件
#include <stdio.h>
#include <stdlib.h>
#include "SeqList_D.h"
void text1()
{
SeqList_D s1;
InitSeqList_D(&s1);
PushBack(&s1, 1);
PushBack(&s1, 2);
PushBack(&s1, 3);
PushBack(&s1, 4);
PushBack(&s1, 5);
PrintSeq(&s1);
}
int main()
{
text1();
system("pause");
return 0;