博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
动态链表增删改查及排序功能
阅读量:6080 次
发布时间:2019-06-20

本文共 5799 字,大约阅读时间需要 19 分钟。

主要功能的实现:

#include "SeqList.h"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)//打印{	assert(pSeq);	size_t i = 0;	for (; i < pSeq->size; i++)	{		printf("%d", pSeq->array[i]);	}	printf("\n");}void CheckExpandCapicity(SeqList* pSeq)//检查容量{	assert(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 PushFront(SeqList* pSeq, DataType x)//头插{	int i = 0;	assert(pSeq);	CheckExpandCapicity(pSeq);	for (i = pSeq->size; i >= 1; i--)	{		pSeq->array[i] = pSeq->array[i - 1];	}	pSeq->array[0] = x;	pSeq->size++;}void PopFront(SeqList* pSeq)//头删{	size_t i = 0;	assert(pSeq);	for (; i < pSeq->size - 1; i++)	{		pSeq->array[i] = pSeq->array[i + 1];	}	pSeq->size--;}void PushBack(SeqList* pSeq, DataType x)//尾插{	assert(pSeq);	CheckExpandCapicity(pSeq);	pSeq->array[pSeq->size] = x;	pSeq->size++;}void PopBack(SeqList* pSeq)//尾删{	assert(pSeq);	pSeq->size--;}void Insert(SeqList* pSeq, size_t index, DataType x)//在index位置插入{	size_t i = pSeq->size;	assert(pSeq);	assert(index < pSeq->size);	CheckExpandCapicity(pSeq);	for (; i >index; i--)	{		pSeq->array[i] = pSeq->array[i - 1];	}	pSeq->array[index] = x;	pSeq->size++;}void Modify(SeqList* pSeq, size_t index, DataType x)//改动{	assert(pSeq);	assert(index < pSeq->size);	pSeq->array[index] = x;}void Remove(SeqList* pSeq, size_t index)//删除index位置的数{	size_t i = index;	assert(pSeq);	assert(index < pSeq->size);	for (; i < pSeq->size - 1; i++)	{		pSeq->array[i] = pSeq->array[i + 1];	}	pSeq->size--;}void Swap(DataType* left, DataType* right){	DataType tmp = *left;	*left = *right;	*right = tmp;}void BubbleSort(SeqList* pSeq)//冒泡排序{	size_t index, end;	int exchange = 0;	assert(pSeq);	for (end = pSeq->size - 1; end > 0; --end)	{		exchange = 0;		for (index = 0; index < end; index++)		{			if (pSeq->array[index]>pSeq->array[index + 1])			{				Swap(pSeq->array + index, pSeq->array + index + 1);				exchange = 1;			}		}		if (exchange == 0)		{			break;		}	}}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* pSeq, DataType x)//二分查找{	size_t left=0;	size_t right = pSeq->size - 1;;	size_t middle;	FindRet ret;	ret.isFind = FALSE;	assert(pSeq);	while (left<=right)	{		middle = (left + right) / 2;		if (x == pSeq->array[middle])		{			ret.isFind = TRUE;			ret.index = middle;			return ret;		}		else if (x>pSeq->array[middle])			left = middle + 1;		else			right = middle - 1;	}	return ret;}
头文件:

#pragma once#define DEFAULT_CAPICITY 3typedef int DataType;#include
#include
#include
#include
typedef struct SeqList{ DataType *array; size_t size; size_t capicity;//当前的容量}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 CheckExpandCapicity(SeqList* pSeq);void PushFront(SeqList *pSeq, DataType x);void PopFront(SeqList *pSeq);void PushBack(SeqList *pSeq, DataType x);void PopBack(SeqList *pSeq);void Insert(SeqList *pSeq, size_t index, DataType x);void Modify(SeqList *pSeq, size_t index, DataType x);void Remove(SeqList *pSeq, size_t index);void Swap(DataType* left, DataType* right);void BubbleSort(SeqList* pSeq);void SelectSort(SeqList* pSeq);FindRet BinarySearch(SeqList* pSep, DataType x);
測试程序部分:

#include "SeqList.h"void test1()//測试初始化、打印、尾插/尾删{	SeqList s;	InitSeqList(&s);	PushBack(&s, 1);	PushBack(&s, 2);	PushBack(&s, 3);	PushBack(&s, 4);	PrintSeqList(&s);	PopBack(&s);	PrintSeqList(&s);}void test2()//測试头插、头删{	SeqList s;	InitSeqList(&s);	PushFront(&s, 3);	PushFront(&s, 4);	PushFront(&s, 5);	PushFront(&s, 6);	PrintSeqList(&s);	PopFront(&s);	PrintSeqList(&s);}void test3()//測试在index位置插入、改动index位置的值、删除index位置的值{	SeqList s;	InitSeqList(&s);	PushBack(&s, 1);	PushBack(&s, 2);	PushBack(&s, 3);	PushBack(&s, 4);	PrintSeqList(&s);	Insert(&s, 2, 8);	PrintSeqList(&s);	Modify(&s,2,5);	PrintSeqList(&s);	Remove(&s, 2);	PrintSeqList(&s);}void test4()//測试冒泡排序{	SeqList s;	InitSeqList(&s);	PushBack(&s, 3);	PushBack(&s, 1);	PushBack(&s, 5);	PushBack(&s, 4);	PushBack(&s, 2);	PrintSeqList(&s);	BubbleSort(&s);	PrintSeqList(&s);}void test5()//測试选择排序{	SeqList s;	InitSeqList(&s);	PushBack(&s, 3);	PushBack(&s, 1);	PushBack(&s, 5);	PushBack(&s, 4);	PushBack(&s, 2);	PrintSeqList(&s);	SelectSort(&s);	PrintSeqList(&s);}void test6()//測试二分搜索{	DataType x;	FindRet ret;	SeqList s;	InitSeqList(&s);	PushBack(&s, 1);	PushBack(&s, 2);	PushBack(&s, 3);	PushBack(&s, 4);	PushBack(&s, 5);	x = 4;	ret = BinarySearch(&s, x);	if (ret.isFind == TRUE)	{		printf("%d %d\n",x,ret.index);	}	else		printf("find failed!\n");	x = 8;	ret = BinarySearch(&s, x);	if (ret.isFind == TRUE)	{		printf("%d %d", x, ret.index);	}	else		printf("find failed!\n");	x = 1;	ret = BinarySearch(&s, x);	if (ret.isFind == TRUE)	{		printf("%d %d\n", x, ret.index);	}	else		printf("find failed!\n");	x = 5;	ret = BinarySearch(&s, x);	if (ret.isFind == TRUE)	{		printf("%d %d\n", x, ret.index);	}	else		printf("find failed!\n");}int main(){	test1();	test2();	//test3();	test4();	test5();	test6();	getchar();	return 0;}

转载于:https://www.cnblogs.com/gavanwanggw/p/6741718.html

你可能感兴趣的文章
以交互方式使用exp/imp的演示
查看>>
Python对文件的操作(转)
查看>>
Codeforces Round #263 (Div. 2)
查看>>
软考概述
查看>>
程序猿打新总结 6月份 新股申购秘籍
查看>>
163源报错Hash Sum mismatch 解决方法
查看>>
使用ECMAscript5中的forEach函数遍历数组
查看>>
Linux之MySQL
查看>>
jekins 持续集成手记
查看>>
Android 为应用创建、删除桌面快捷方式
查看>>
Maven如何手动添加依赖的jar文件到本地Maven仓库
查看>>
Oracle安装部署,版本升级,应用补丁快速参考
查看>>
【Android】13.0 第13章 创建和访问SQLite数据库—本章示例主界面
查看>>
CentOS6.5安装Tab增强版:bash-completion
查看>>
2015华为机试——数字基root
查看>>
Java学习笔记(四):流程控制
查看>>
ios开发--第三方整理
查看>>
【JVM】JVM系列之内存模型(六)
查看>>
MySQL排序原理与案例分析
查看>>
RegexBuddy正则表达式工具
查看>>