动态顺序表

本文介绍了一个基于C语言实现的顺序表数据结构,包括初始化、增删查改等基本操作及冒泡排序、选择排序算法的实现。通过交互式菜单进行功能选择,便于理解和实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、SeqList.h

#ifndef __SeqList_h__
#define __SeqList_h__

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <windows.h>

typedef int DataType;

typedef struct SeqList
{
	DataType* _a;
	size_t _size; //有效数据个数 
	size_t _capacity; //容量 
}SeqList;

void SeqInit(SeqList* pSeq);
void CheckCapacity(SeqList* pSeq);
void SeqPrint(SeqList* pSeq);
void SeqDestroy(SeqList* pSeq);

void SeqPushBack(SeqList* pSeq, DataType x);
void SeqPopBack(SeqList* pSeq);
void SeqPushFront(SeqList* pSeq, DataType x);
void SeqPopFront(SeqList* pSeq);

void SeqInsert(SeqList* pSeq, size_t pos, DataType x);
void SeqErase(SeqList* pSeq, size_t pos);

int SeqFind(SeqList* pSeq, DataType x);
void SeqModify(SeqList* pSeq, size_t pos, DataType x);

void BubbleSort(SeqList* pSeq);
void SelectSort(SeqList* pSeq);
int BinarySearch(SeqList* pSeq, DataType x);

#endif //__SeqList_h__

二、SeqList.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "SeqList.h"

//初始化
void SeqInit(SeqList* pSeq)
{
	assert(pSeq);
	pSeq->_size = 0;
	pSeq->_capacity = 5;
	pSeq->_a = (DataType *)malloc(pSeq->_capacity * sizeof(DataType));
	if (pSeq->_a == NULL)
	{
		perror("SeqInit");
		return;
	}
	memset(pSeq->_a, 0, pSeq->_capacity * sizeof(DataType));
	printf("初始化成功\n");
}

//扩容
void CheckCapacity(SeqList* pSeq)
{
	assert(pSeq);
	if (pSeq->_size >= pSeq->_capacity)
	{
		DataType* ptr = (DataType *)realloc(pSeq->_a, (pSeq->_capacity + 2) * sizeof(DataType));
		if (ptr == NULL)
		{
			perror("CheckCapacity");
		}
		else
		{
			pSeq->_a = ptr;
			pSeq->_capacity += 2;
		}
	}
}

//输出
void SeqPrint(SeqList* pSeq)
{
	assert(pSeq);
	if (pSeq->_size == 0)
	{
		printf("该顺序表为空\n");
	}
	else
	{
		for (int i = 0; i < (int)pSeq->_size; i++)
		{
			printf("%d ", pSeq->_a[i]);
		}
		printf("\n");
	}
}

//销毁
void SeqDestroy(SeqList* pSeq)
{
	assert(pSeq);
	free(pSeq->_a);
	pSeq->_a = NULL;
	pSeq->_capacity = 0;
	pSeq->_size = 0;
	printf("销毁成功\n");
}

//尾插
void SeqPushBack(SeqList* pSeq, DataType x)
{
	assert(pSeq);
	CheckCapacity(pSeq);
	pSeq->_a[pSeq->_size] = x;
	pSeq->_size++;
}

//头插
void SeqPushFront(SeqList* pSeq, DataType x)
{
	assert(pSeq);
	CheckCapacity(pSeq);
	for (int i = pSeq->_size; i > 0; i--)
	{
		pSeq->_a[i] = pSeq->_a[i - 1];
	}
	pSeq->_a[0] = x;
	pSeq->_size++;
}

//尾删
void SeqPopBack(SeqList* pSeq)
{
	assert(pSeq);
	if (pSeq->_size == 0)
	{
		printf("该顺序表为空\n");
	}
	else
	{
		pSeq->_size--;
		printf("删除成功\n");
	}
}

//头删
void SeqPopFront(SeqList* pSeq)
{
	assert(pSeq);
	if (pSeq->_size == 0)
	{
		printf("该顺序表为空\n");
	}
	else
	{
		for (int i = 1; i < (int)pSeq->_size; i++)
		{
			pSeq->_a[i - 1] = pSeq->_a[i];
		}
		pSeq->_size--;
		printf("删除成功\n");
	}
}

//插入
void SeqInsert(SeqList* pSeq, size_t pos, DataType x)
{
	assert(pSeq);
	assert(pos <= pSeq->_size);
	CheckCapacity(pSeq);
	for (int i = (int)pSeq->_size; i > (int)pos; i--)
	{
		pSeq->_a[i] = pSeq->_a[i - 1];
	}
	pSeq->_a[pos] = x;
	pSeq->_size++;
}

//删除
void SeqErase(SeqList* pSeq, size_t pos)
{
	assert(pSeq);
	if (pSeq->_size == 0)
	{
		printf("该顺序表为空\n");
	}
	else
	{
		assert(pos < pSeq->_size);
		for (int i = pos + 1; i < (int)pSeq->_size; i++)
		{
			pSeq->_a[i - 1] = pSeq->_a[i];
		}
		pSeq->_size--;
		printf("删除成功\n");
	}
}

//查找
int SeqFind(SeqList* pSeq, DataType x)
{
	assert(pSeq);
	if (pSeq->_size == 0)
	{
		printf("该顺序表为空\n");
		return -1;
	}
	else
	{
		for (int i = 0; i < (int)pSeq->_size; i++)
		{
			if (pSeq->_a[i] == x)
			{
				return i;
			}
		}
		return -1;
	}
}

//修改
void SeqModify(SeqList* pSeq, size_t pos, DataType x)
{
	assert(pSeq);
	if (pSeq->_size == 0)
	{
		printf("该顺序表为空\n");
	}
	else
	{
		assert(pos < pSeq->_size);
		pSeq->_a[pos] = x;
		printf("修改成功\n");
	}
}

//交换
void Swap(DataType *a, DataType *b)
{
	DataType tmp = *a;
	*a = *b;
	*b = tmp;
}

//冒泡排序
void BubbleSort(SeqList* pSeq)
{
	assert(pSeq);
	if (pSeq->_size == 0)
	{
		printf("该顺序表为空\n");
		return;
	}
	for (int i = 0; i < (int)pSeq->_size - 1; i++)
	{
		int flag = 0;
		for (int j = 0; j < (int)pSeq->_size - i - 1; j++)
		{
			if (pSeq->_a[j]>pSeq->_a[j + 1])
			{
				flag = 1;
				Swap(&pSeq->_a[j], &pSeq->_a[j + 1]);
			}
		}
		if (flag == 0)
		{
			break;
		}
	}
	printf("排序成功\n");
}

//选择排序
void SelectSort(SeqList* pSeq)
{
	assert(pSeq);
	if (pSeq->_size == 0)
	{
		printf("该顺序表为空\n");
		return;
	}
	int left = 0;
	int right = pSeq->_size - 1;
	while (left < right)
	{
		int i = 0;
		int min = left;
		int max = left;
		for (i = left; i <= right; i++)
		{
			if (pSeq->_a[i] < pSeq->_a[min])
			{
				min = i;
			}
			if (pSeq->_a[i] > pSeq->_a[max])
			{
				max = i;
			}
		}
		if (min != left)
		{
			Swap(&pSeq->_a[min], &pSeq->_a[left]);
		}
		if (max == left)
		{
			max = min;
		}
		if (max != right)
		{
			Swap(&pSeq->_a[max], &pSeq->_a[right]);
		}
		left++;
		right--;
	}
	printf("排序成功\n");
}

//二分查找
int BinarySearch(SeqList* pSeq, DataType x)
{
	assert(pSeq);
	if (pSeq->_size == 0)
	{
		printf("该顺序表为空\n");
		return -1;
	}
	int left = 0;
	int right = pSeq->_size - 1;
	while (left <= right)
	{
		int mid = left + ((right - left) >> 1);
		if (pSeq->_a[mid] < x)
		{
			left = mid + 1;
		}
		else if (pSeq->_a[mid] > x)
		{
			right = mid - 1;
		}
		else
		{
			return mid;
		}
	}
	return -1;
}

三、test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "SeqList.h"

void Menu()
{
	printf("********动态顺序表********\n");
	printf("**       0.退出         **\n");
	printf("**       1.初始化       **\n");
	printf("**       2.头插         **\n");
	printf("**       3.尾插         **\n");
	printf("**       4.头删         **\n");
	printf("**       5.尾删         **\n");
	printf("**       6.插入         **\n");
	printf("**       7.删除         **\n");
	printf("**       8.查找         **\n");
	printf("**       9.修改         **\n");
	printf("**       10.冒泡排序    **\n");
	printf("**       11.选择排序    **\n");
	printf("**       12.二分查找    **\n");
	printf("**       13.输出        **\n");
	printf("**       14.销毁        **\n");
	printf("**************************\n");
}

int main()
{
	SeqList pSeq;
	int op = 0;
	do{
		Menu();
		printf("请选择:");
		scanf("%d", &op);
		switch (op)
		{
		case 0: 
			break;
		case 1:
			SeqInit(&pSeq);
			break;
		case 2:
		{
				  DataType x = 0;
				  printf("请输入要插入的值:");
				  scanf("%d", &x);
				  SeqPushFront(&pSeq, x);
				  printf("插入成功\n");
		}
			break;
		case 3:
		{
				  DataType x = 0;
				  printf("请输入要插入的值:");
				  scanf("%d", &x);
				  SeqPushBack(&pSeq, x);
				  printf("插入成功\n");
		}
			break;
		case 4:
			SeqPopFront(&pSeq);
			break;
		case 5:
			SeqPopBack(&pSeq);
			break;
		case 6:
		{
				  DataType x = 0;
				  size_t pos = 0;
				  printf("请输入要插入的位置:");
				  scanf("%d", &pos);
				  printf("请输入要插入的值:");
				  scanf("%d", &x);
				  SeqInsert(&pSeq, pos, x);
				  printf("插入成功\n");
		}
			break;
		case 7:
		{
				  size_t pos = 0;
				  printf("请输入要删除的位置:");
				  scanf("%d", &pos);
				  SeqErase(&pSeq, pos);
		}
			break;
		case 8:
		{       
				  DataType x = 0;	
				  printf("请输入要查找的值:");
				  scanf("%d", &x);
				  printf("%d\n", SeqFind(&pSeq, x));
		}
			break;
		case 9:
		{
				  DataType x = 0;
				  size_t pos = 0;
				  printf("请输入要修改的位置:");
				  scanf("%d", &pos);
				  printf("请输入修改后的值:");
				  scanf("%d", &x);
				  SeqModify(&pSeq, pos, x);
		}
			break;
		case 10:
			BubbleSort(&pSeq);
			break;
		case 11:
			SelectSort(&pSeq);
			break;
		case 12:
		{
				   DataType x = 0;
				   printf("请输入要查找的值:");
				   scanf("%d", &x);
				   printf("%d\n", BinarySearch(&pSeq, x));
		}
			break;
		case 13:
			SeqPrint(&pSeq);
			break;
		case 14:
			SeqDestroy(&pSeq);
			break;
		default:
			printf("请重新选择...\n");
			break;
		}
	} while (op);
	system("pause");
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值