静态顺序表操作较为简单,空间利用率高,支持随机访问,但头部中间插入元素删除数据效率很低,时间复杂度O(N)(除尾插尾删),里面需要注意的是排序的实现,以及二分查找,这些都是面试中常考的点。代码如下:
#pragma once
#define MAX_SIZE 100
typedef int DataType;
typedef struct SeqList
{
DataType array[MAX_SIZE];
size_t size;
}SeqList;
void InitSeqList(SeqList* pSeq);
void PrintSeqList(SeqList* pSeq);
void PushBack(SeqList* pSeq, DataType x);
void PopBack(SeqList* pSeq);
void PushFront(SeqList* pSeq, DataType x);
void PopFront(SeqList* pSeq);
int Find(SeqList* pSeq, DataType x);
void Insert(SeqList* pSeq, size_t index, DataType x);
void Modified(SeqList* pSeq, size_t index, DataType x);
void Erase(SeqList* pSeq, size_t index);
bool Remove(SeqList* pSeq, DataType x);
void RemoveaAll(SeqList* pSeq, DataType x);
void Swap(DataType* left, DataType* right);
void BubbleSort(SeqList* pSeq);
void SelectSort(SeqList* pSeq);
int BinarySearch(SeqList* pSep, DataType x);
#include"SeqList.h"
#include<iostream>
#include<assert.h>
#include<memory.h>
using namespace std;
void InitSeqList(SeqList* pSeq)
{
assert(pSeq);
memset(pSeq->array, 0, MAX_SIZE*sizeof(DataType));
pSeq->size = 0;
}
void PrintSeqList(SeqList* pSeq)
{
size_t i;
assert(pSeq);
for (i = 0; i < pSeq->size; ++i)
{
cout << pSeq->array[i] << " ";
cout << endl;
}
}
void PushBack(SeqList* pSeq, DataType x)
{
assert(pSeq);
if (pSeq->size >= MAX_SIZE)
{
cout << "SeqList is Full" << " ";
cout << endl;
}
pSeq->array[pSeq->size] = x;
pSeq->size++;
}
void PopBack(SeqList* pSeq)
{
assert(pSeq);
if (pSeq->size < 1)
{
cout << "SeqList is empty" << " ";
cout << endl;
}
--pSeq->size;
}
void PushFront(SeqList* pSeq, DataType x)
{
assert(pSeq);
int i = pSeq->size - 1;
if (pSeq->size >= MAX_SIZE)
{
cout << "SeqList is full" << " ";
cout << endl;
}
for (; i >= 0; --i)
{
pSeq->array[i+1] = pSeq->array[i];
}
pSeq->array[0] = x;
++pSeq->size;
}
void PopFront(SeqList* pSeq)
{
assert(pSeq);
size_t i = 1;
if (pSeq->size < 1)
{
cout << "SeeqList is empty" << " ";
cout << endl;
}
for (; i < pSeq->size; ++i)
{
pSeq->array[i - 1] = pSeq->array[i];
}
pSeq->size--;
}
int Find(SeqList* pSeq, DataType x)
{
assert(pSeq);
size_t i = 0;
for (; i < pSeq->size; i++)
{
if (x == pSeq->array[i])
{
return i;
}
}
return - 1;
}
void Insert(SeqList* pSeq, size_t index, DataType x)
{
assert(pSeq);
assert(index <= pSeq->size);
size_t end = pSeq->size;
if (pSeq->size >= MAX_SIZE)
{
cout << "SeqList is full" << " ";
cout << endl;
}
for (; end > index; end--)
{
pSeq->array[end] = pSeq->array[end - 1];
}
pSeq->array[index] = x;
pSeq->size++;
}
void Modified(SeqList* pSeq, size_t index, DataType x)
{
assert(pSeq);
assert(index < pSeq->size);
pSeq->array[index] = x;
}
void Erase(SeqList* pSeq, size_t index)
{
assert(pSeq);
assert(index < pSeq->size);
for (; index < pSeq->size - 1; index++)
{
pSeq->array[index] = pSeq->array[index + 1];
}
}
bool Remove(SeqList* pSeq, DataType x)
{
size_t ret = Find(pSeq, x);
if (ret != -1)
{
Erase(pSeq, x);
}
return ret != -1;
}
void RemoveaAll(SeqList* pSeq, DataType x)
{
assert(pSeq);
size_t count = 0;
size_t index = 0;
size_t begin = 0;
for (; index < pSeq->size; ++index)
{
if (pSeq->array[index] == x)
{
++count;
}
else
{
pSeq->array[begin++] = pSeq->array[index];
}
}
pSeq->size -= count;
}
void Swap(DataType* left, DataType* right)
{
DataType tmp = *left;
*left = *right;
*right = tmp;
}
void BubbleSort(SeqList* pSeq)
{
assert(pSeq);
int exchange = 0;
size_t index, end;
for (end = pSeq->size - 1; end > 0; --end)
{
//交换标志位进行优化
exchange = 0;
for (index = 0; index < end; ++index)
{
if (pSeq->array[index]>pSeq->array[index + 1])
{
Swap(pSeq->array+index, pSeq->array+index + 1);
exchange = 1;
}
}
if (exchange == 0)
{
break;
}
}
}
void SelectSort(SeqList* pSeq)
{
assert(pSeq);
size_t minIndex, index, begin;
for (begin = 0; begin < pSeq->size-1; ++begin)
{
minIndex = begin;
for (index = begin + 1; index < pSeq->size; index++)
{
if (pSeq->array[minIndex]>pSeq->array[index])
{
minIndex = index;
}
}
if (minIndex != begin)
{
Swap(pSeq->array + minIndex, pSeq->array + begin);
}
}
}
int BinarySearch(SeqList* pSeq, DataType x)
{
assert(pSeq);
size_t left = 0;
size_t right = pSeq->size;
while (left < right)
{
// 注意越界问题
size_t mid = left + (right - left) / 2;
if (pSeq->array[mid] == x)
{
return mid;
}
else if (pSeq->array[mid] > x)
{
right = mid;
}
else
{
left = mid + 1;
}
}
return -1;
}
void Test1()
{
SeqList s;
InitSeqList(&s);
PushBack(&s, 1);
PushBack(&s, 2);
PushBack(&s, 3);
PushBack(&s, 4);
PrintSeqList(&s);
PopBack(&s);
PopBack(&s);
PopBack(&s);
PopBack(&s);
PopBack(&s);
}
void Test2()
{
SeqList s;
InitSeqList(&s);
PushBack(&s, 1);
PushBack(&s, 2);
PushBack(&s, 3);
PushBack(&s, 4);
PushBack(&s, 5);
PrintSeqList(&s);
SelectSort(&s);
BinarySearch(&s, 3);
PrintSeqList(&s);
}
int main()
{
Test1();
Test2();
}
下面,我们来实现动态的顺序表,与静态不同的是,动态顺序表结构体中增加了容量,以及增加了容量的判断函数。其余函数实现基本相同,不过是插入元素的时候,调用了容量判断函数。下面来看代码实现(只给出了不同于静态顺序表的部分)。
typedef int DataType;
typedef struct SeqList
{
DataType* array;
size_t size;
size_t capacity;
}SeqList;
void InitSeqList(SeqList* pSeq)
{
assert(pSeq);
pSeq->capacity = 10;
pSeq->array = (DataType*)malloc(sizeof(SeqList)*pSeq->capacity);
memset(pSeq->array, 0, sizeof(DataType));
pSeq->size = 0;
}
void CheckCapacity(SeqList* pSeq)
{
assert(pSeq);
if (pSeq->capacity == pSeq->size)
{
DataType* tmp;
pSeq->capacity *= 2;
tmp = (DataType*)malloc(sizeof(SeqList)*pSeq->capacity);
memcpy(tmp, pSeq->array, pSeq->size*sizeof(DataType));
free(pSeq->array);
pSeq->array = tmp;
}
}
void PushBack(SeqList* pSeq, DataType x)
{
assert(pSeq);
CheckCapacity(pSeq);
pSeq->array[pSeq->size] = x;
pSeq->size++;
}
void PopBack(SeqList* pSeq)
{
assert(pSeq);
if (0 == pSeq->size)
{
printf("this SeqList is empty\n");
}
pSeq->size--;
}
void PushFront(SeqList* pSeq,DataType x)
{
assert(pSeq);
CheckCapacity(pSeq);
for (size_t index = 1; index <=pSeq->size; ++index)
{
pSeq->array[index] = pSeq->array[index - 1];
}
pSeq->array[0] = x;
pSeq->size++;
}
void PopFront(SeqList* pSeq)
{
assert(pSeq);
for (size_t index =1; index<pSeq->size; --index)
{
pSeq->array[index - 1] = pSeq->array[index];
}
pSeq->size--;
}