#define _CRT_SECURE_NO_WARNINGS 1
#include"stdio.h"
#include"string.h"
#include"assert.h"
#include"stdlib.h"
#pragma warning(disable:4996)
#define MAX_SIZE 10
typedef int DataType;
typedef struct SeqList
{
DataType arr[MAX_SIZE];
size_t size;
}SeqList,*pSeqList;
void InitSeqList(SeqList* seqList)
{
assert(seqList);
memset(seqList,0,sizeof(DataType)*MAX_SIZE);
seqList->size = 0;
}
void PushBack(SeqList* seqList, DataType data)
{
assert(seqList);
if (seqList->size == MAX_SIZE)
{
printf("顺序表已满,不能再插入其他的元素!");
return;
}
seqList->arr[seqList->size] = data;
seqList->size++;
}
void PopBack(SeqList* seqList)
{
assert(seqList);
if (seqList->size==0)
{
printf("顺序表为空,不能进行删除元素!\n");
return;
}
seqList->size--;
}
void PushFront(SeqList* seqList, DataType data)
{
assert(seqList);
size_t index = seqList->size - 1;
if (seqList->size >= MAX_SIZE)
{
printf("顺序表已满,不能再插入其他元素!");
return;
}
for (index = seqList->size - 1; index >= 0; index--)
{
seqList->arr[index + 1] = seqList->arr[index];
}
seqList->arr[0] = data;
seqList->size++;
}
void PopFront(SeqList *seqList)
{
size_t index = 0;
assert(seqList);
if (0 == seqList->size)
{
printf("顺序表为空,不能进行删除!");
return;
}
for (index = 0; index < seqList->size; index++)
{
seqList->arr[index] = seqList->arr[index+1];
}
seqList->size--;
}
int Find(SeqList *seqList, DataType data)
{
assert(seqList);
size_t index = 0;
for (index = 0; index < seqList->size; index++)
{
if (seqList->arr[index]==data)
{
return index + 1;
}
}
return -1;
}
void Insert(SeqList *seqList,size_t pos,DataType data)
{
assert(seqList);
size_t index = 0;
if (pos > seqList->size)
{
printf("位置不正确,请重新选择位置!");
return;
}
if (seqList->size>=MAX_SIZE)
{
printf("顺序表已满,不能再插入其他元素!");
return;
}
for (index = seqList->size - 1; index >= pos; index--)
{
seqList->arr[index+1] = seqList->arr[index];
}
seqList->arr[index] = data;
seqList->size++;
}
void Erase(SeqList* seqList, size_t pos)
{
assert(seqList);
size_t index = 0;
if (pos > seqList->size)
{
printf("位置不正确,请重新输入位置!");
return;
}
for (index = pos - 1; index < seqList->size - 1; index++)
{
seqList->arr[index] = seqList->arr[index+1];
}
seqList->size--;
}
void Remove(SeqList* seqList, DataType data)
{
assert(seqList);
size_t index = 0;
size_t temp = 0;
for (index = 0; index < seqList->size; index++)
{
if (data == seqList->arr[index])
{
for (temp = index; temp < seqList->size - 1; temp++)
{
seqList->arr[temp] = seqList->arr[temp + 1];
}
}
seqList->size--;
return;
}
}
void RemoveAll(SeqList* seqList, DataType data)
{
assert(seqList);
size_t index = 0;
size_t rem = 0;
for (; index < seqList->size; index++)
{
if (seqList->arr[index] == data)
{
for (rem = index; rem < seqList->size - 1; rem++)
{
seqList->arr[rem] = seqList->arr[rem + 1];
}
seqList->size--;
index--;
}
}
}
void Swap(int *num1, int *num2)
{
int temp = *num1;
*num1 = *num2;
*num2 = temp;
}
void SelectSort(pSeqList seqList)
{
assert(seqList);
size_t num = 0;
size_t index = 0;
size_t end = seqList->size;
for (; num < (seqList->size) / 2; num++)
{
size_t min = num;
size_t max = num;
for (index = num + 1; index < end; index++)
{
min = seqList->arr[min] < seqList->arr[index] ? min : index;
max = seqList->arr[max] > seqList->arr[index] ? max : index;
}
Swap(&seqList->arr[min], &seqList->arr[num]);
Swap(&seqList->arr[max], &seqList->arr[--end]);
}
}
void BorbbleSort(pSeqList seqList)
{
size_t index = 0;
size_t num = 0;
int flag = 1;
assert(seqList);
while (flag == 1)
{
for (num = 0; num < seqList->size - 1; num++)
{
for (index = 0; index < seqList->size - num - 1; index++)
{
if (seqList->arr[index]>seqList->arr[index + 1])
{
Swap(&seqList->arr[index], &seqList->arr[index + 1]);
flag = 1;
}
}
}
}
}
int BinarySearch(SeqList* seqList, DataType data)
{
int left = 0;
int right = seqList->size - 1;
assert(seqList);
if (seqList->size==0)
{
return -1;
}
while (left <= right)
{
int mid = left + (right - left) / 2;
if (seqList->arr[mid] == data)
{
return mid;
}
else if (seqList->arr[mid] > data)
{
right = mid - 1;
}
else
{
left = mid + 1;
}
}
return -1;
}
void PrintSeqList(SeqList* seqList)
{
size_t index = 0;
for (index=0; index < seqList->size; index++)
{
printf("%d ", seqList->arr[index]);
return;
}
printf("\n");
}
SeqList seqList;
void TestFun1()
{
InitSeqList(&seqList);
PushBack(&seqList, 1);
PushBack(&seqList, 2);
PushBack(&seqList, 3);
PushBack(&seqList, 4);
PrintSeqList(&seqList);
PopBack(&seqList);
PopBack(&seqList);
PrintSeqList(&seqList);
}
void TestFun2()
{
InitSeqList(&seqList);
PushFront(&seqList, 1);
PushFront(&seqList, 2);
PushFront(&seqList, 3);
PushFront(&seqList, 4);
PrintSeqList(&seqList);
PopFront(&seqList);
PopFront(&seqList);
PrintSeqList(&seqList);
}
void TestFun3()
{
int ret = 0;
InitSeqList(&seqList);
PushFront(&seqList, 1);
PushFront(&seqList, 2);
PushFront(&seqList, 3);
PushFront(&seqList, 4);
ret = Find(&seqList, 4);
Insert(&seqList, 3, 5);
Erase(&seqList, 5);
printf("%d\n", ret);
PrintSeqList(&seqList);
}
void TestFun4()
{
InitSeqList(&seqList);
PushFront(&seqList, 1);
PushFront(&seqList, 2);
PushFront(&seqList, 2);
PushFront(&seqList, 4);
PushFront(&seqList, 1);
PushFront(&seqList, 2);
PushFront(&seqList, 2);
PushFront(&seqList, 4);
Remove(&seqList, 2);
PrintSeqList(&seqList);
RemoveAll(&seqList, 2);
PrintSeqList(&seqList);
}
void TestFun5()
{
int ret = 0;
InitSeqList(&seqList);
PushFront(&seqList, 3);
PushFront(&seqList, 9);
PushFront(&seqList, 1);
PushFront(&seqList, 4);
PushFront(&seqList, 2);
PushFront(&seqList, 8);
PushFront(&seqList, 7);
PushFront(&seqList, 0);
SelectSort(&seqList);
ret = BinarySearch(&seqList, 1);
printf("%d\n", ret);
PrintSeqList(&seqList);
}
int main()
{
TestFun5();
system("pause");
return 0;
}