#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
#include<string.h>
#define MAX_SIZE 5
typedef int DataType;
typedef struct SeqList
{
DataType array[MAX_SIZE];
size_t size;
} SeqList;
打印顺序表
void PrintfSeqList(SeqList* pSeq)
{
assert(pSeq);
int i = 0;
if (!pSeq->size)
{
return;
}
for (; i < pSeq->size ; i++)
{
printf("%d ", pSeq->array[i]);
}
}
2.初始化顺序表
void InitSeqList(SeqList* pSeq)
{
assert(pSeq);
if (!pSeq->size)
{
return;
}
size_t ret = pSeq->size;
memset(pSeq, 0, sizeof(SeqList));
pSeq->size = ret;
}
3.尾插法
void PushBack(SeqList* pSeq, DataType x)
{
assert(pSeq);
pSeq->array[pSeq->size ] = x;
++pSeq->size;
}
void PopBack(SeqList* pSeq)
{
assert(pSeq);
if (pSeq->size == 0)
{
return;
}
--pSeq->size;
}
4.头插法
void PushFront(SeqList* pSeq, DataType x)
{
int i = pSeq->size-1;
assert(pSeq);
if (pSeq->size == 0)
{
return;
}
for (; i >=0; i--)
{
pSeq->array[i + 1] = pSeq->array[i];
}
pSeq->array[0] = x;
++pSeq->size;
return;
}
5.头出法
void PopFront(SeqList* pSeq)
{
int i = 1;
assert(pSeq);
if (pSeq->size == 0)
{
return;
}
for (; i < pSeq->size; i++)
{
pSeq->array[i - 1] = pSeq->array[i];
}
--pSeq->size;
return;
}
6.在指定的位置插入某个数字
void Inser(SeqList* pSeq, size_t pos, DataType x)
{
assert(pSeq);
int i = pSeq->size;
if (pSeq->size == 0)
{
return;
}
for (; i >pos; i--)
{
pSeq->array[i] = pSeq->array[i - 1];
}
pSeq->array[pos] = x;
++pSeq->size;
}
7.在顺序表中寻找某个数字,若找到则返回该数字,否则返回-1
int Find(SeqList* pSeq, DataType x)
{
assert(pSeq);
int i = 0;
if (pSeq->size == 0)
{
return;
}
for (; i < pSeq->size; i++)
{
if (pSeq->array[i] == x)
{
return i;
break;
}
}
return -1;
}
8.删除顺序表中某个位置的数据
void Erase(SeqList* pSeq, size_t pos)
{
assert(pSeq);
int i = pos;
if (pSeq->size == 0)
{
return;
}
for (; i<pSeq->size; i++)
{
pSeq->array[i-1] = pSeq->array[i];
}
--pSeq->size;
}
9.删除顺序表中某个数字
void Remove(SeqList* pSeq, DataType x)
{
assert(pSeq);
int i = 0;
if (pSeq->size == 0)
{
return;
}
for (; i < pSeq->size; i++)
{
if (pSeq->array[i] == x)
{
int j = 0;
for (j = i; j < pSeq->size-1; j++)
{
pSeq->array[j] = pSeq->array[j + 1];
}
}
break;
}
--pSeq->size;
}
10.删除顺序表中所有的数字
void _RemoveAll(SeqList* pSeq, DataType x)
{
assert(pSeq);
int i = 0,count=0;
if (pSeq->size == 0)
{
return;
}
for (; i < pSeq->size; i++)
{
if (pSeq->array[i] == x)
{
count++;
}
else
{
pSeq->array[i - count] = pSeq->array[i];
}
}
pSeq->size -= count;
}
11.顺序表的冒泡排序
void BubbleSort(SeqList* pSeq)
{
assert(pSeq);
int i = 0, j = 0;
if (pSeq->size == 0)
{
return;
}
for (i = 0; i < pSeq->size; i++)
{
for (j = 0; j < pSeq->size - i - 1; j++)
{
if (pSeq->array[j]>pSeq->array[j + 1])
{
int tmp = pSeq->array[j];
pSeq->array[j] = pSeq->array[j + 1];
pSeq->array[j + 1] = tmp;
}
}
}
}
12.顺序表的选择排序
void SeclectSort(SeqList* pSeq)
{
assert(pSeq);
int i, j;
if (pSeq->size == 0)
{
return;
}
for (i = 0; i < pSeq->size - 1; i++)
{
int num= 0;
for (j = i + 1; j < pSeq->size; j++)
{
if (pSeq->array[j] < pSeq->array[num])
{
num = j;
}
}
int tmp = pSeq->array[num];
pSeq->array[num] = pSeq->array[i];
pSeq->array[i] = tmp;
}
}
13.在顺序表中进行二分搜索
int BinarySearch(SeqList* pSeq, DataType x)
{
assert(pSeq);
int left = 0;
int right = pSeq->size-1;
while (left <= right)
{
int mid = left - (left - right) / 2;
if (pSeq->array[mid] < x)
{
left = mid + 1;
}
else if (pSeq->array[mid] > x)
{
right = mid-1;
}
else
return mid;
}
return -1;
}