動態鏈表

DynSeqlist.h


#ifndef __DYNSEQLIST_H__
#define __DYNSEQLIST_H__

#include<stdio.h>
#include<malloc.h>
#include<assert.h>
#include<string.h>
#define SIZE 5
typedef int DataType;
typedef struct seqlist
{
	DataType* array;
	size_t size;	//有效值
	size_t capacity;//總容量
}seqlist;
typedef enum tag
{
	one,//刪去第一個
	more,//全部都刪去
}Tag;
void Init(seqlist* pSeq);
void CheckExpand(seqlist* pSeq);
void Destory(seqlist* pSeq);
void Print(seqlist* pSeq);
void PushBack(seqlist* pSeq, DataType x);
void PushFront(seqlist* pSeq, DataType x);
void Insert(seqlist* pSeq, size_t index, DataType x);
void PopBack(seqlist* pSeq);
void PopFront(seqlist*pSeq);
void removed(seqlist* pSeq, size_t index);
void Modify(seqlist* pSeq, size_t index, DataType x);
void Erase(seqlist* pSeq, DataType x, Tag all);
int Find(seqlist* pSeq, DataType x,size_t index);
void swap(DataType* x, DataType* y);
void BubbleSort(seqlist* pSeq);//冒泡排序升序
void SelectSort(seqlist* pSeq);//選擇排序降序
int BinarySearch(seqlist* pSeq, DataType x);//二分查找時用升序測試
#endif		//__DYNSEQLIST_H__

DynSeqlist.c


#include"DynSeqlist.h"
void Init(seqlist* pSeq)
{
	assert(pSeq);
	pSeq->array = (DataType *)malloc(SIZE*sizeof(DataType));
	memset(pSeq->array, 0, SIZE*sizeof(DataType));
	pSeq->size = 0;
	pSeq->capacity = SIZE;
}
void CheckExpand(seqlist* pSeq)
{
	assert(pSeq);
	DataType* S;
	if (pSeq->size == pSeq->capacity)
	{
		S = (DataType*)malloc(2 * pSeq->capacity *sizeof(DataType));
		memcpy(S, pSeq->array, SIZE*sizeof(DataType));
		free(pSeq->array );
		pSeq->array = S;
		pSeq->capacity = 2 * pSeq->capacity;
	}
}
void Destory(seqlist* pSeq)
{
	assert(pSeq);
	if (pSeq->array != NULL)
	{
		free(pSeq->array );
	}
}
void Print(seqlist* pSeq)
{
	assert(pSeq);
	size_t i=0;
	for (; i < pSeq->size; i++)
	{
		printf("%d ", pSeq->array[i]);
	}
	printf("\n");
}
void PushBack(seqlist* pSeq, DataType x)
{
	assert(pSeq);
	assert(pSeq->size < pSeq->capacity);
	pSeq->array[pSeq->size] = x;
	pSeq->size++;
}
void PushFront(seqlist* pSeq, DataType x)
{
	assert(pSeq);
	assert(pSeq->size < pSeq->capacity);
	int i = pSeq->size - 1;
	for (; i >= 0; i--)
	{
		pSeq->array[i + 1] = pSeq->array[i];
	}
	pSeq->array[0] = x;
	pSeq->size++;
}
void Insert(seqlist* pSeq, size_t index, DataType x)
{
	assert(pSeq);
	assert(index < pSeq->size);
	size_t i = pSeq->size;
	for (; i > index; i--)
	{
		pSeq->array[i] = pSeq->array[i - 1];
	}
	pSeq->array[index] = x;
	pSeq->size++;
}
void PopBack(seqlist* pSeq)
{
	assert(pSeq);
	pSeq->size--;
}
void PopFront(seqlist*pSeq)
{
	assert(pSeq);
	size_t i = 0;
	for (; i < pSeq->size; i++)
	{
		pSeq->array[i] = pSeq->array[i + 1];
	}
	pSeq->size--;
}
void removed(seqlist* pSeq, size_t index)
{
	assert(pSeq);
	assert(index <  pSeq->size);
	size_t i = index;
	for (; i <pSeq->size ; i++)
	{
		pSeq->array[i] = pSeq->array[i + 1];
	}
	pSeq->size--;
}
void Modify(seqlist* pSeq, size_t index, DataType x)
{
	assert(pSeq);
	assert(index < pSeq->size);
	pSeq->array[index] = x;
}
//返回的值爲-1時,找不到此元素;否則返回的值爲找到的元素的下標。
int Find(seqlist* pSeq, DataType x, size_t index)
{
	assert(pSeq);
	size_t i = 0;
	for (; i < pSeq->size; i++)
	{
		if (pSeq->array[i] == x)
		{
			return i;
		}
	}
	return -1;
}
void Erase(seqlist* pSeq, DataType x, Tag all)
{
	assert(pSeq);
	size_t i = 0;
	int ret ;
	if (pSeq->size == 0)
	{
		printf("鏈表爲空\n");
		return;
	}
	ret = Find(pSeq, x, 0);
	if (ret == -1)
	{
		printf("表中找不到這個元素\n");
	}
	else if (all == one)
	{
		removed(pSeq, ret);
	}
	else
	{
		while (ret != -1)
		{
			removed(pSeq, ret);
			ret = Find(pSeq, x, ret);

		}
	}
}
void swap(DataType* x, DataType* y)
{
	DataType temp =*x;
	*x = *y;
	*y = temp;
}
void BubbleSort(seqlist* pSeq)
{
	assert(pSeq);
	size_t i = 0; 
	size_t j = 0;
	for (i = 0; i < pSeq->size - 1; i++)
	{
		for (j = 0; j < pSeq->size-i-1; j++)
		{
			if (pSeq->array[j]>pSeq->array[j+1])
			{
				swap(&(pSeq->array[j]), &(pSeq->array[j + 1]));
			}
		}
	}
}
void SelectSort(seqlist* pSeq)
{
	assert(pSeq);
	size_t i = 0;
	size_t j = 0;
	size_t max;
	for (i = 0; i < pSeq->size-1; i++)
	{
		max = i;
		for (j = i + 1; j < pSeq->size; j++)
		{
			if (pSeq->array [max]< pSeq->array[j])
			{
				max = j;
			}
		}
		if (max != i)
		{
			swap(&(pSeq->array[max]), &(pSeq->array[i]));
		}
	}
}
//返回的值爲-1時,找不到此元素;否則返回的值爲要找的元素的下標
int BinarySearch(seqlist* pSeq, DataType x)
{
	assert(pSeq);
	DataType left = 0;
	DataType right = pSeq->size - 1;
	DataType mid = left + (right - left) / 2;//這種寫法的原因
	while (left <= right)
	{
		DataType mid = left + (right - left) / 2;
		if (pSeq->array[mid] == x)
		{
			return mid;
		}
		if (pSeq->array[mid] < x)
		{
			left = mid + 1;
		}
		else
		{
			right = mid - 1;
		}
	}
	return -1;
}
	



void Test()
{
	seqlist s;
	int ret = 0;
	Init(&s);
	PushBack(&s, 1);
	PushBack(&s, 2);
	PushBack(&s, 3);
	PushBack(&s, 4);
	PushBack(&s, 3);
	CheckExpand(&s);
	PushBack(&s, 5);
	PushBack(&s, 3);
	PushBack(&s, 6);
	Print(&s);
	PushFront(&s, 99);
	Print(&s);
	Insert(&s, 0, 120);
	Print(&s);
	PopBack(&s);
	Print(&s);
	PopFront(&s);
	Print(&s);
	removed(&s, 5);
	Print(&s);
	Modify(&s, 1, 24);
	Print(&s);
	Erase(&s, 3, more); 
	Print(&s);
	SelectSort(&s);
	Print(&s);
	BubbleSort(&s);
	Print(&s);
	ret=BinarySearch(&s, 2);
	printf("ret=%d\n", ret);
}

void main()
{
	Test();
}


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