顺序表:
顺序表是在计算机内存中以数组的形式保存的线性表,线性表的顺序存储是指用一组地址连续的存储单元依次存储线性表中的各个元素、使得线性表中在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中,即通过数据元素物理存储的相邻关系来反映数据元素之间逻辑上的相邻关系,采用顺序存储结构的线性表通常称为顺序表。 ————百度百科
又因为数组有动态和静态的,所以顺序表也有动态和静态之分,这里只介绍静态的顺序表,贴出动态顺序表的代码。
顺序表基本操作:
由于顺序表基本操作都是对数组进行操作,可直接看代码整理。
顺序表构成:
typedef struct SeqList
{
int array[MAX_SIZE];
int size; //表示顺序表中有效元素的个数
}SeqList, *PSeqList;
Test.c
#include <stdio.h>
#include "SeqList.h"
int main()
{
TestSeqList();
system("pause");
return 0;
}
SeqList.h
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
#include <windows.h>
#define MAX_SIZE 10
typedef int(*Compare)(int left, int right); //返回值为整形,接收两个参数的函数指针类型
typedef int DataType;
typedef struct SeqList
{
int array[MAX_SIZE];
int size; //表示顺序表中有效元素的个数
}SeqList, *PSeqList;
void TestSeqList();
void InitSeqList(PSeqList pSeq); //初始化
void SeqListPushBack(PSeqList pSeq, DataType data); //尾插
void SeqListPopBack(PSeqList pSeq); //尾删
void SeqListPushFront(PSeqList pSeq, DataType data);//
void SeqListPopFront(PSeqList pSeq);
void SeqListInsert(PSeqList pSeq, int pos, DataType data);
void SeqListErase(PSeqList pSeq, int pos);
void Remove(PSeqList pSeq, DataType data); //删除表中第一个值为data的元素
void RemoveAll(PSeqList pSeq, DataType data); //删除表中所有值为data的元素
int Less(int left, int right);
void SelectSort(PSeqList pSeq, Compare com); //选择排序
void SelectSort_p(PSeqList pSeq); //优化选排
int BinarySearch(PSeqList pSeq, DataType data);
int Empty(PSeqList pSeq);
int Find(PSeqList pSeq, DataType data);
void PrintSeqList(PSeqList pSeq);
#endif //_SEQLIST_H_
SeqList.c
#include <stdio.h>
#include <assert.h>
#include "SeqList.h"
void InitSeqList(PSeqList pSeq)
{
assert(pSeq);
pSeq->size = 0;
}
void SeqListPushBack(PSeqList pSeq, DataType data)
{
assert(pSeq);
if (pSeq->size >= MAX_SIZE)
{
printf("顺序表已满!\n");
return;
}
pSeq->array[pSeq->size++] = data;
}
void SeqListPopBack(PSeqList pSeq)
{
assert(pSeq);
if (Empty(pSeq))
{
printf("顺序表为空!\n");
}
pSeq->size--;
}
int Empty(PSeqList pSeq)
{
assert(pSeq);
if (0 == pSeq->size)
{
return 1;
}
return 0;
}
void SeqListPushFront(PSeqList pSeq, DataType data)
{
int i = 0;
assert(pSeq);
if (MAX_SIZE <= pSeq->size)
{
printf("顺序表已满!\n");
}
for (i = pSeq->size - 1; i >= 0; --i)
{
pSeq->array[i + 1] = pSeq->array[i];
}
pSeq->array[0] = data;
pSeq->size++;
}
void SeqListPopFront(PSeqList pSeq)
{
int i = 0;
assert(pSeq);
if (Empty(pSeq))
{
printf("顺序表为空!\n");
}
for (i = 0; i < pSeq->size - 1; i++)
{
pSeq->array[i] = pSeq->array[i + 1];
}
pSeq->size--;
}
void SeqListInsert(PSeqList pSeq, int pos, DataType data)
{
int i = 0;
assert(pSeq);
if (MAX_SIZE == pSeq->size)
{
printf("顺序表已满!\n");
}
if (pos < 0 && pos > pSeq->size) //检测pos
{
printf("%d 位置非法!\n", pos);
return;
}
for (i = pSeq->size - 1; i >= pos - 1; --i)
{
pSeq->array[i + 1] = pSeq->array[i];
}
pSeq->array[pos - 1] = data;
pSeq->size++;
}
void SeqListErase(PSeqList pSeq, int pos)
{
int i = 0;
assert(pSeq);
if (Empty(pSeq))
{
printf("顺序表为空!\n");
}
if (pos < 0 || pos >= pSeq->size)
{
printf("%d 位置非法!\n");
return;
}
for (i = pos; i < pSeq->size - 1; i++)
{
pSeq->array[i] = pSeq->array[i + 1];
}
pSeq->size--;
}
void Remove(PSeqList pSeq, DataType data)
{
if (NULL == pSeq)
{
return;
}
SeqListErase(pSeq, Find(pSeq, data));
}
void RemoveAll(PSeqList pSeq, DataType data)
{
//int pos = 0;
//while (-1 != (pos = Find(pSeq, data))) //依次找,逐个删除,效率低
//{
// SeqListErase(pSeq, pos);
//}
assert(pSeq);
int i = 0;
int count = 0;
DataType *pTemp = (DataType *)malloc(sizeof(DataType)*pSeq->size);// 申请新内存,将不是data的数拷贝
for (; i < pSeq->size; i++)
{
if (pSeq->array[i] != data)
pTemp[count++] = pSeq->array[i];
}
memcpy(pSeq->array, pTemp, sizeof(DataType)*count);
pSeq->size = count;
free(pTemp);
}
int Less(int left, int right)
{
return left < right;
}
int Greater(int left, int right)
{
return left > right;
}
void SelectSort(PSeqList pSeq, Compare com)
{
int i = 0;
int j = 0;
int temp;
int maxpos;
for (i = 0; i < pSeq->size - 1; i++)
{
maxpos = 0;
for (j = 1; j < pSeq->size - i; j++)
{
if (com(pSeq->array[j], pSeq->array[maxpos]))
{
maxpos = j;
}
}
temp = pSeq->array[maxpos];
pSeq->array[maxpos] = pSeq->array[pSeq->size - i - 1];
pSeq->array[pSeq->size - i - 1] = temp;
}
}
void SelectSort_p(PSeqList pSeq)
{
int begin = 0;
int end = 0;
int minPos = 0;
int maxPos = 0;
int i = 1;
assert(pSeq);
end = pSeq->size - 1;
while (begin < end)
{
minPos = begin;
maxPos = begin;
DataType temp;
i = begin ;
while (i <= end)
{
if (pSeq->array[i] > pSeq->array[maxPos])
maxPos = i;
if (pSeq->array[i] < pSeq->array[minPos])
minPos = i;
i++;
}
if (maxPos != end)
{
temp = pSeq->array[maxPos];
pSeq->array[maxPos] = pSeq->array[end];
pSeq->array[end] = temp;
}
if (minPos != begin)
{
temp = pSeq->array[minPos];
pSeq->array[minPos] = pSeq->array[begin];
pSeq->array[begin] = temp;
}
begin++;
end--;
}
}
int BinarySearch(PSeqList pSeq, DataType data)
{
assert(pSeq);
int left = 0;
int right = pSeq->size - 1;
int mid = 0;
while (left <= mid)
{
mid = left + ((right - left) >> 1);
if (pSeq->array[mid] == data)
{
return mid;
}
else if (pSeq->array[mid] > mid)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return 0;
}
int Find(PSeqList pSeq, DataType data)
{
int i = 0;
assert(pSeq);
for (i = 0; i < pSeq->size; i++)
{
if (pSeq->array[i] == data)
{
return i;
}
}
return -1;
}
void PrintSeqList(PSeqList pSeq)
{
int i = 0;
assert(pSeq);
for (; i < pSeq->size; i++)
{
printf("%d ", pSeq->array[i]);
}
printf("\n");
}
void TestSeqList()
{
int ret;
SeqList s;
InitSeqList(&s);
PrintSeqList(&s);
SeqListPushBack(&s, 71);
SeqListPushBack(&s, 22);
SeqListPushBack(&s, 3);
SeqListPushBack(&s, 41);
SeqListPushBack(&s, 5);
SeqListPopBack(&s);
PrintSeqList(&s);
SeqListPushFront(&s, 0);
PrintSeqList(&s);
SeqListPopFront(&s);
PrintSeqList(&s);
SeqListInsert(&s, 2, 2);
PrintSeqList(&s);
SelectSort(&s,Greater);
PrintSeqList(&s);
SelectSort(&s, Less);
PrintSeqList(&s);
ret = BinarySearch(&s, 77);
if (ret)
{
printf("find!-->%d\n", ret+1);
}
else
{
printf("not find!\n");
}
}
动态顺序表完整代码:
SeqList.h
#ifndef _SEQLISTD_H_
#define _SEQLISTD_H_
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
typedef int DataType;
typedef struct SeqListD
{
DataType* _pData;
int _size; //有效元素个数
int _capacity; //表中可容纳元素个数
}SeqListD, *PSeqListD;
void SeqListD_mian();
void SeqListDInit(PSeqListD pSeq);
void SeqListDPushBack(PSeqListD pSeq, DataType data);
void SeqListDPopBack(PSeqListD pSeq);
int SeqListDSize(PSeqListD pSeq);
int SeqListDCapacity(PSeqListD pSeq);
void SeqListDClear(PSeqListD pSeq); //元素个数清为0
void SeqListDDestroy(PSeqListD pSeq); //销毁
int SeqListEmpty(PSeqListD pSeq); //判空
int CheckCapacity(PSeqListD pSeq);
void SeqListDPrint(PSeqListD pSeq);
#endif //_SEQLISTD_H_
SeqList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqListD.h"
void SeqListDInit(PSeqListD pSeq)
{
assert(pSeq);
pSeq->_pData = (DataType*)malloc(sizeof(DataType)* 3);
if (NULL == pSeq->_pData)
{
printf("内存分配失败!\n");
exit(0);
}
pSeq->_capacity = 3;
pSeq->_size = 0;
}
void SeqListDPushBack(PSeqListD pSeq, DataType data)
{
assert(pSeq);
if (CheckCapacity(pSeq)) //是否需要增容?
{
pSeq->_pData[pSeq->_size++] = data;
}
}
int CheckCapacity(PSeqListD pSeq) //容量检测
{
if (pSeq->_size >= pSeq->_capacity)
{
int NewCapacity = pSeq->_capacity * 2;
DataType *pTemp = (DataType *)malloc(sizeof(DataType)* NewCapacity);
if (NULL == pTemp)
{
printf("内存分配失败!\n");
return 0;
}
memcpy(pTemp, pSeq, sizeof(DataType)*pSeq->_size);
free(pSeq->_pData);
pSeq->_pData = pTemp;
pSeq->_capacity = NewCapacity;
}
return 1; //返回1,无论是否增容,都可以继续插元素
}
void SeqListDPopBack(PSeqListD pSeq)
{
assert(pSeq);
if (SeqListEmpty(pSeq))
{
return;
}
pSeq->_size--;
}
int SeqListEmpty(PSeqListD pSeq)
{
assert(pSeq);
return (0 == pSeq->_size);
}
int SeqListDSize(PSeqListD pSeq)
{
assert(pSeq);
return pSeq->_size;
}
int SeqListDCapacity(PSeqListD pSeq)
{
assert(pSeq);
return pSeq->_capacity;
}
void SeqListDClear(PSeqListD pSeq)
{
assert(pSeq);
pSeq->_size = 0;
}
void SeqListDDestroy(PSeqListD pSeq)
{
assert(pSeq);
if (pSeq->_pData)
{
free(pSeq->_pData);
pSeq->_pData = NULL;
pSeq->_capacity = 0;
pSeq->_size = 0;
}
}
void SeqListD_mian()
{
SeqListD s;
SeqListDInit(&s);
SeqListDPushBack(&s, 1);
SeqListDPushBack(&s, 2);
SeqListDPushBack(&s, 3);
printf("size = %d\n", SeqListDSize(&s));
printf("capacity = %d\n", SeqListDCapacity(&s));
SeqListDPushBack(&s, 4);
SeqListDPushBack(&s, 5);
SeqListDPushBack(&s, 6);
SeqListDPushBack(&s, 7);
printf("size = %d\n", SeqListDSize(&s));
printf("capacity = %d\n", SeqListDCapacity(&s));
SeqListDPopBack(&s);
SeqListDPopBack(&s);
SeqListDPopBack(&s);
printf("size = %d\n", SeqListDSize(&s));
printf("capacity = %d\n", SeqListDCapacity(&s));
SeqListDClear(&s);
printf("size = %d\n", SeqListDSize(&s));
printf("capacity = %d\n", SeqListDCapacity(&s));
}