#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define MAX_SIZE 20
typedef int DataType;
typedef unsigned size_t;
//建立一个静态顺序表
typedef struct SeqList
{
DataType array[MAX_SIZE];
int size;
}SeqList;
void SeqListInit(SeqList *pSeq)
{
assert(pSeq);
pSeq->size = 0;
}
//查
int SeqListFind(SeqList *pSeq, DataType data)
{
assert(pSeq);
int i = 0;
for (; i < pSeq->size; i++)
{
if (pSeq->array[i] == data){
return 1;
}
}
return -1;
}
//改
void SeqListUpdata(SeqList *pSeq, DataType data1, DataType data2)
{
assert(pSeq);
int i = 0;
for (; i < pSeq->size; i++)
{
if (pSeq->array[i] == data1)
pSeq->array[i] = data2;
}
}
//删
void SeqListErase(SeqList *pSeq, size_t pos)
{
assert(pSeq);
assert(pos >= 0 && pos < MAX_SIZE);
int i = 0;
for (i = (int)pos; i < pSeq->size-1; i++)
{
pSeq->array[i] = pSeq->array[i + 1];
}
pSeq->size--;
}
#if 0
void SeqListDelall(SeqList *pSeq, DataType data)
{
int i = 0;
for (i = 0; i < pSeq->size; i++)
{
if (pSeq->array[i] == data){
SeqListErase(pSeq, i);
}
}
}
#endif
#if 1
void SeqListDelall(SeqList *pSeq, DataType data)
{
int pback = 0;
int pfront = 0;
int i = 0;
int count = 0;
for (; i < pSeq->size; i++)
{
if (pSeq->array[i] == data){
count++;
pback++;
}
else
{
pSeq->array[pfront] = pSeq->array[pback];
pfront++;
pback++;
}
}
pSeq->size -= count;
}
#endif
//增
void SeqListPrint(SeqList *pSeq)
{
assert(pSeq);
int i = 0;
for (; i < pSeq->size; i++)
{
printf("%d ",pSeq->array[i]);
}
printf("\n");
}
void SeqLlistCreate(SeqList *pSeq,DataType data)
{
assert(pSeq);
pSeq->array[pSeq->size] = data;
pSeq->size++;
}
void SeqListPushFront(SeqList *pSeq,DataType data)
{
assert(pSeq);
int i = 0;
for (i = pSeq->size; i > 0; i--)
{
pSeq->array[i] = pSeq->array[i - 1];
}
pSeq->array[0] = data;
pSeq->size++;
}
#if 0
void SeqListInsert(SeqList *pSeq, size_t pos, DataType data)
{
assert(pSeq);
int i = 0;
for (i = 0; i < pSeq->size - (int)pos; i++)
{
pSeq->array[pSeq->size - i] = pSeq->array[pSeq->size - i - 1];
}
pSeq->array[pos] = data;
pSeq->size++;
}
#endif
void SeqListInsert(SeqList *pSeq, size_t pos, DataType data)
{
assert(pSeq);
int i = 0;
for (i = pSeq->size; i > (int)pos;i--)
{
pSeq->array[i] = pSeq->array[i - 1];
}
pSeq->array[pos] = data;
pSeq->size++;
}
void Swap(DataType *p1, DataType *p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
#if 0
void SeqListBubbleSort(SeqList *pSeq)
{
int flag = 0;
int i = 0;
for (; i<pSeq->size; i++)
{
int j = 0;
for (; j < pSeq->size - i - 1; j++)
{
if (pSeq->array[j]>pSeq->array[j + 1])
{
flag = 1;
Swap(&pSeq->array[j], &pSeq->array[j + 1]);
}
}
if (flag == 0)
break;
}
}
#endif
//冒泡排序
void SeqListBubbleSort(SeqList *pSeq)
{
assert(pSeq);
int end = pSeq->size - 1;
while (end > 0)
{
int i = 0;
for (; i < end; i++)
{
if (pSeq->array[i] > pSeq->array[i + 1])
Swap(&(pSeq->array[i]), &(pSeq->array[i + 1]));
}
end--;
}
}
#if 0
void SeqListBinaryFind(SeqList *pSeq, DataType data)
{
int left = 0;
int right = pSeq->size - 1;
int mid = 0;
int flag = 0;
while (left <= right)
{
mid = left + ((right - left) >> 1);
if (pSeq->array[mid] > data)
{
right = mid - 1;
}
else if (pSeq->array[mid] < data)
{
left = mid + 1;
}
else
{
printf("%d ", mid);
flag = 1;
right = mid - 1;
left = mid + 1;
}
}
printf("\n");
if (flag == 0)
printf("-1\n");
}
#endif
//二分查找
void SeqListBinaryFind(SeqList *pSeq, DataType data)
{
int left = 0;
int right = pSeq->size;
int mid = 0;
int flag = 0;
while (left < right)
{
mid = left + ((right - left) >> 1);
if (pSeq->array[mid] > data)
{
right = mid;
}
else if (pSeq->array[mid] < data)
{
left = mid + 1;
}
else
{
printf("%d ", mid);
flag = 1;
right = mid - 1;
left = mid + 1;
}
}
printf("\n");
if (flag == 0)
printf("-1\n");
}
//销毁
void SeqListDestory(SeqList *pSeq)
{
assert(pSeq);
pSeq->size = 0;
}
void Test2(SeqList *L)
{
SeqListPrint(L);
SeqListBubbleSort(L);
SeqListBinaryFind(L, 1);
SeqListPrint(L);
//SeqListDelall(L, 3);
//SeqListPrint(L);
//SeqListFind(L, 1000);
}
void Test1(SeqList *L)
{
SeqLlistCreate(L, 1);
SeqLlistCreate(L, 4);
SeqLlistCreate(L, 2);
SeqLlistCreate(L, 10);
SeqLlistCreate(L, 0);
SeqLlistCreate(L, 1);
SeqLlistCreate(L, 6);
}
int main()
{
SeqList L;
SeqListInit(&L);
Test1(&L);
Test2(&L);
SeqListDestory(&L);
system("pause");
return 0;
}
动态顺序表
#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int DataType;
typedef unsigned size_t;
//定义一个动态顺序表
typedef struct Seqlist
{
DataType *array;
size_t size; //有效元素个数
size_t capicity; //最多能存储多少个元素
}SeqList;
//打印
void SeqlistPrint(SeqList *pSeq)
{
assert(pSeq);
int i = 0;
for (; i < (int)pSeq->size; i++)
{
printf("%d ", pSeq->array[i]);
}
printf("\n");
}
//初始化
void SeqListInit(SeqList *pSeq,size_t capicity)
{
assert(pSeq);
DataType*p = (DataType*)malloc(sizeof(DataType)*capicity); //
if (!p)
{
perror("malloc");
exit(EXIT_FAILURE);
}
else
{
pSeq->array = p;
pSeq->size = 0;
pSeq->capicity = capicity;
}
}
//检查是否需要扩容
//使用realloc扩容
#if 0
void CheckCapicity(SeqList *pSeq)
{
assert(pSeq);
if(pSeq->size < pSeq->capicity)
{
return;
}
if (pSeq->size == pSeq->capicity)
{
DataType *p = (DataType*)realloc(pSeq->array, sizeof(DataType)*pSeq->capicity * 2);
assert(p);
pSeq->array = p;
pSeq->capicity *= 2;
}
}
#endif
//使用malloc扩容
//搬数据
//需要释放老空间
//把新空间挂上去
//
void CheckCapicity(SeqList *pSeq)
{
assert(pSeq);
DataType *NewArray = NULL;
if (pSeq->size < pSeq->capicity)
{
return;
}
if (pSeq->size == pSeq->capicity)
{
NewArray = (DataType *)malloc(sizeof(DataType)*pSeq->capicity * 2);
assert(NewArray);
pSeq->capicity *= 2;// 伙伴算法
int i = 0;
for (; i < (int)pSeq->size; i++)
{
*(NewArray + i) = *(pSeq->array + i);
//NewArray[i] = pSeq->array[i];
}
free(pSeq->array);
pSeq->array = NewArray;
//free(NewArray);
}
}
//尾插
void SeqListPushBack(SeqList *pSeq, DataType x)
{
assert(pSeq);
CheckCapicity(pSeq);
pSeq->array[pSeq->size] = x;
pSeq->size++;
}
//销毁
void SeqListDestory(SeqList *pSeq)
{
assert(pSeq);
free(pSeq->array);
pSeq->array = NULL;
pSeq->size = pSeq->capicity = 0;
}
int main()
{
SeqList L;
SeqListInit(&L, 1);
SeqListPushBack(&L, 1);
SeqListPushBack(&L, 2);
SeqListPushBack(&L, 3);
SeqListPushBack(&L, 4);
SeqListPushBack(&L, 5);
SeqListPushBack(&L, 6);
SeqlistPrint(&L);
SeqListDestory(&L);
//SeqlistPrint(&L);
system("pause");
return 0;
}