動態順序表(可分配內存空間)

<strong style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">前幾天寫了一個靜態順序表,但是覺得一開始開闢很大一部分空間卻不一定可以完全用得上,會造成很大的內存浪費,於是寫了一個可以自動開闢內存空間的動態順序表作爲改進。</strong>

"DynamicSeqllist.h"

#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_CAPACITY 3

typedef struct SeqList
{
	DataType* array;
	size_t size;	  // 當前的有效數據個數 
	size_t capacity;  // 容量
}SeqList;

typedef enum Tag
{
	TRUE,	// 真
	FALSE,	// 假
}Tag;

typedef struct FindRet
{
	Tag isFind;		// 是否找到的標示
	size_t index;	// 找到數據的下標
}FindRet;

void InitSeqList(SeqList* pSeq);
void PrintSeqList(SeqList* pSeq);
void CheckExpandCapacity(SeqList* pSeq);
void DestorySeqList(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 BubbleSort(SeqList* pSeq);
void SelectSort(SeqList* pSeq);
FindRet BinarySearch(SeqList * pSeq, DataType x);


#endif // __SEQ_LIST__


"DynamicSeqlist.c"

#include "DynamicSeqlist.h"

//對順序表初始化
void InitSeqList(SeqList* pSeq)
{
	assert(pSeq);
	pSeq->array = (DataType*)malloc(sizeof(DataType)*DEFAULT_CAPACITY);
	pSeq->size = 0;
	pSeq->capacity = DEFAULT_CAPACITY;
}

//打印順序表
void PrintSeqList(SeqList* pSeq)
{
	assert(pSeq);
	int i = 0;
	for (; i < pSeq->size; i++)
	{
		printf("%d ",pSeq->array[i]);
	}
	printf("\n");
}

//檢測內存空間是否足夠,若不夠則自動分配二倍空間存儲數據
void CheckExpandCapacity(SeqList* pSeq)
{
	assert(pSeq);
	if (pSeq->size == pSeq->capacity)
	{
		DataType* tmp = (DataType*)malloc(pSeq->capacity * 2*sizeof(DataType));
		memcpy(tmp, pSeq->array, sizeof(DataType)*pSeq->size);

		free(pSeq->array);//當開闢出新空間存儲數據時要將舊空間釋放掉
		pSeq->array = tmp;
		pSeq->capacity = pSeq->capacity * 2;
	}
}

//釋放空間,銷燬順序表
void DestorySeqList(SeqList* pSeq)
{
	if (pSeq)
	{
		free(pSeq->array);
		pSeq->capacity = 0;
		pSeq->size = 0;
	}
}

void PushBack(SeqList* pSeq, DataType x)
{
	assert(pSeq);
	CheckExpandCapacity(pSeq);
	pSeq->array[pSeq->size++] = x;
}

void PopBack(SeqList* pSeq)
{
	assert(pSeq);
	--pSeq->size;
}

void PushFront(SeqList* pSeq, DataType x)
{
	assert(pSeq);
	CheckExpandCapacity(pSeq);
	int i = pSeq->size;
	for (; i >0 ; --i)
	{
		pSeq->array[i] = pSeq->array[i -1];
	}
	pSeq->array[0] = x;
	pSeq->size++;
}

void PopFront(SeqList* pSeq)
{
	assert(pSeq);
	int i = 0 ;
	if (pSeq->size == 0)
	{
		printf("array is empty!");
	}
	for (; i <pSeq->size; ++i)
	{
		pSeq->array[i] = pSeq->array[i + 1];
	}
	pSeq->size--;
}

void Insert(SeqList* pSeq, size_t index, DataType x)
{
	assert(pSeq);
	assert(index<pSeq->size);
	int i = pSeq->size;
	for (; i>index ; --i)
	{
		pSeq->array[i] = pSeq->array[i - 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 Remove(SeqList* pSeq, size_t index)
{
	assert(pSeq);
	assert(index<pSeq->size );
	for (; pSeq->size>index; index++)
	{
		pSeq->array[index] = pSeq->array[index + 1];
	}
	pSeq->size--;
}

FindRet Find(SeqList* pSeq, DataType x, size_t index)
{	
	assert(pSeq);
	FindRet ret = { FALSE, NULL };
	for (; index < pSeq->size; index++)
	{
		if (x == pSeq->array[index])
		{
			Tag isFind = TRUE;
			ret.isFind = TRUE;
			ret.index = index;
			return ret;
		}
	}
	Tag isFind = FALSE;
	return ret;
}

void Swap(DataType *left,DataType *right)
{
	DataType tmp = *left;
	*left = *right;
	*right = tmp;
}
//冒泡排序(升序)
//X1和X2進行比較,如果X1<X2,則交換位置,直到X1是餘下的數中最大的
void BubbleSort(SeqList* pSeq)
{
	assert(pSeq);
	size_t index,end;
	int count = 0;
	int exchange = 0;
	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("%d\n",count);

}

void SelectSort(SeqList* pSeq)
{
	assert(pSeq);
	size_t index, minIndex, begin;
	for (begin = 0; begin < pSeq->size; ++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 * pSeq, DataType x)
{
	assert(pSeq);
	FindRet ret;
	ret.isFind = FALSE;
	size_t right, mid ,left;
	left = 0;
	right = pSeq->size - 1;
	while (left <= right)
	{
		mid = (right + left) / 2;
		if (pSeq->array[mid] == x)
		{
			ret.isFind = TRUE;
			ret.index = mid;
			return ret;
		}
		else if (pSeq->array[mid] > x)
		{
			right = mid - 1;
		}
		else
			left = mid + 1;
	}
	return ret;
}

Tag Erase(SeqList* pSeq, DataType x, Tag all)
{
	Tag success = FALSE;
	FindRet ret;
	assert(pSeq);
	ret = Find(pSeq, x, 0);

	while(ret.isFind == TRUE)
	{
		success = TRUE;
		Remove(pSeq, ret.index);

		if (all == FALSE)
		{
			break;
		}

		ret = Find(pSeq, x, ret.index);
	}

	return success;
}

"main.c"


#include "DynamicSeqlist.h"

void Test1()
{
	SeqList s;
	InitSeqList(&s);
	CheckExpandCapacity(&s);
	PushBack(&s, 1);
	PushBack(&s, 2);
	PushBack(&s, 3);
	PushBack(&s, 4);
	PrintSeqList(&s);
	PopBack(&s);
	PrintSeqList(&s);
	PushFront(&s,0);
	PrintSeqList(&s);
	PopFront(&s);
	PrintSeqList(&s);
	DestorySeqList(&s);
}

void Test2()
{
	SeqList s;
	InitSeqList(&s);
	CheckExpandCapacity(&s);
	PushBack(&s, 1);
	PushBack(&s, 2);
	PushBack(&s, 3);
	PushBack(&s, 4);
	PrintSeqList(&s);
	Insert(&s,1,8);
	PrintSeqList(&s);
	Modified(&s,2,5);
	PrintSeqList(&s);
	Remove(&s, 1);
	PrintSeqList(&s);
	Find(&s,3,0);
	DestorySeqList(&s);
}
void Test3()
{
	SeqList s;
	InitSeqList(&s);
	CheckExpandCapacity(&s);
	PushBack(&s, 9);
	PushBack(&s, 4);
	PushBack(&s, 6);
	PushBack(&s, 2);
	PrintSeqList(&s);
	BubbleSort(&s);
	PrintSeqList(&s);
	SelectSort(&s);
	PrintSeqList(&s);
	DestorySeqList(&s);
}
void Test4()
{
	SeqList s;
	InitSeqList(&s);
	CheckExpandCapacity(&s);
	PushBack(&s, 9);
	PushBack(&s, 4);
	PushBack(&s, 6);
	PushBack(&s, 2);
	PushBack(&s, 4);
	PushBack(&s, 3);
	PushBack(&s, 1);
	PrintSeqList(&s);
	Erase(&s,4,TRUE);
	PrintSeqList(&s);
	DestorySeqList(&s);
}
int main()
{
	Test1();
	printf("\n");
	Test2();
	printf("\n");
	Test3();
	printf("\n");
	Test4();
	getchar();
	return 0;
}


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