動態順序表

// 動態順序表

#pragma once

#define __SEQ_LIST__

#ifdef __SEQ_LIST__

#include <stdio.h>

#include <assert.h>

#include <string.h>

#include <malloc.h>

typedef int DataType;


#define DEFAULT_CAPICITY 3


typedef struct SeqList

{

DataType* array;  // 數據塊指針

size_t size;  // 當前的有效數據個數 

size_t capicity;  // 容量

}SeqList;


//typedef struct SeqList SeqList;


typedef enum Tag

{

TRUE, // 真

FALSE, // 假

}Tag;


typedef struct FindRet

{

Tag isFind; // 是否找到的標示

size_t index; // 找到數據的下標

}FindRet;


// 初始化/銷燬/打印/檢查擴容

void InitSeqList(SeqList* pSeq);

void DestorySeqList(SeqList* pSeq);

void PrintSeqList(SeqList* pSeq);

void CheckExpandCapicity(SeqList* pSeq);


// 頭插/頭刪/尾插/尾刪

void PushBack(SeqList* pSeq, DataType x);

void PopBack(SeqList* pSeq);

void PushFront(SeqList* pSeq, DataType x);

void PopFront(SeqList* pSeq);


// 插入/修改/刪除/查找

void Insert(SeqList* pSeq, size_t index, DataType x);

void Modified(SeqList* pSeq, size_t index, DataType x);

void Remove(SeqList* pSeq, size_t index);

FindRet Find(SeqList* pSeq, DataType x, size_t index);

Tag Erase(SeqList* pSeq, DataType x, Tag all);


// 冒泡排序/選擇排序/二分查找

void Swap(DataType* left, DataType* right);

void BubbleSort(SeqList* pSeq);

void SelectSort(SeqList* pSeq);

//FindRet BinarySearch(DataType* array, size_t begin, size_t end, DataType x);

FindRet BinarySearch(SeqList* pSep, DataType x);


//////////////////////////////////////////////////////////////////////////////

// 實現

void InitSeqList(SeqList* pSeq)

{

assert(pSeq);

pSeq->array = (DataType*)malloc(sizeof(DataType)*DEFAULT_CAPICITY);

pSeq->size = 0;

pSeq->capicity = DEFAULT_CAPICITY;

}


void PrintSeqList(SeqList* pSeq)

{

int i = 0;

for (; i < pSeq->size; ++i)

{

printf("%d ", pSeq->array[i]);

}


printf("\n", pSeq->array[i]);

}


void CheckExpandCapicity(SeqList* pSeq)

{

if (pSeq->size == pSeq->capicity)

{

DataType* tmp = (DataType*)malloc(pSeq->capicity * 2 * sizeof(DataType));

memcpy(tmp, pSeq->array, sizeof(DataType)* pSeq->size);


free(pSeq->array);

pSeq->array = tmp;

pSeq->capicity = pSeq->capicity * 2;

}

}


void DestorySeqList(SeqList* pSeq)

{

if (pSeq)

{

free(pSeq->array);

pSeq->size = 0;

pSeq->capicity = 0;

}

}



void PushBack(SeqList* pSeq, DataType x)

{

assert(pSeq);

CheckExpandCapicity(pSeq);


pSeq->array[pSeq->size++] = x;

}


void PopBack(SeqList* pSeq)

{

assert(pSeq);

if (pSeq->size == 0)

{

printf("SeqList Is Empty\n");

}


--pSeq->size;

}


void PushFront(SeqList* pSeq, DataType x)

{

int i = pSeq->size;

assert(pSeq);

CheckExpandCapicity(pSeq);


for (; i > 0; --i)

{

pSeq->array[i] = pSeq->array[i - 1];

}


pSeq->array[0] = x;

pSeq->size++;

}


void Modified(SeqList* pSeq, size_t index, DataType x)

{

assert(pSeq);

assert(index < pSeq->size);


pSeq->array[index] = x;

}


void Swap(DataType* left, DataType* right)

{

DataType tmp = *left;

*left = *right;

*right = tmp;

}


void BubbleSort(SeqList* pSeq)

{

int count = 0;

int exchange = 0;

size_t index, end;

assert(pSeq);

for (end = pSeq->size - 1; end > 0; --end)

{

//交換標誌進行優化

exchange = 0;

for (index = 0; index < end; ++index)

{

count++;

if (pSeq->array[index] > pSeq->array[index + 1])

{

Swap(pSeq->array + index, pSeq->array + index + 1);

exchange = 1;

}

}


if (exchange == 0)

{

break;

}

}


printf("count:%d\n", count);

}


// 選擇排序

void SelectSort(SeqList* pSeq)

{

size_t minIndex, index, begin;

assert(pSeq);


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);

}

}

}


FindRet BinarySearch(SeqList* pSep, DataType x)

{

size_t left, right, mid;

FindRet ret;

ret.isFind = FALSE;


assert(pSep);


left = 0;

right = pSep->size - 1;


// 注意這裏使用<=, 否則可能存在死循環問題

while (left <= right)

{

// 注意越界問題

//mid = (left + right) / 2;

mid = left + (right - left) / 2;

if (pSep->array[mid] == x)

{

ret.isFind = TRUE;

ret.index = mid;

return ret;

}

else if (pSep->array[mid] > x)

{

right = mid - 1;

}

else

{

left = mid + 1;

}

}


return ret;

}


#endif // __SEQ_LIST__


void Test1()

{

SeqList s;

InitSeqList(&s);


PushBack(&s, 1);

PushBack(&s, 2);

PushBack(&s, 3);

PushBack(&s, 4);

PushFront(&s, 0);


PrintSeqList(&s);


DestorySeqList(&s);

}


void Test2()

{

SeqList s;

InitSeqList(&s);


PushBack(&s, 6);

PushBack(&s, 3);

PushBack(&s, 5);

PushBack(&s, 8);

PushBack(&s, 4);

PushBack(&s, 2);

PushBack(&s, 1);

PrintSeqList(&s);


BubbleSort(&s);

PrintSeqList(&s);


BubbleSort(&s);

PrintSeqList(&s);


Swap(s.array + (s.size - 1), s.array);

PrintSeqList(&s);

BubbleSort(&s);

PrintSeqList(&s);


DestorySeqList(&s);

}


void Test3()

{

DataType x;

FindRet ret;

SeqList s;

InitSeqList(&s);


PushBack(&s, 6);

PushBack(&s, 3);

PushBack(&s, 5);

PushBack(&s, 8);

PushBack(&s, 4);

PushBack(&s, 2);

PushBack(&s, 1);

PrintSeqList(&s);


SelectSort(&s);

PrintSeqList(&s);


x = 1;

ret = BinarySearch(&s, x);

if (ret.isFind == TRUE)

{

printf("Binary Search %d Success: %d\n", x, ret.index);

}


x = 8;

ret = BinarySearch(&s, x);

if (ret.isFind == TRUE)

{

printf("Binary Search %d Success: %d\n", x, ret.index);

}


x = 5;

ret = BinarySearch(&s, x);

if (ret.isFind == TRUE)

{

printf("Binary Search %d Success: %d\n", x, ret.index);

}


x = 20;

ret = BinarySearch(&s, x);

if (ret.isFind == FALSE)

{

printf("Binary Search %d Failed\n", x);

}


DestorySeqList(&s);

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章