顺序表的增删改查(包括顺序表的冒泡排序,顺序表的二分查找)

顺序表的增删改查(包括顺序表的冒泡排序,顺序表的二分查找)

seqlist.h

#ifndef _SEQLIST_H_
#define _SEQLIST_H_


#define _CRT_SECURE_NO_WARNINGS

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

typedef int SLDataType;

// 顺序表的动态存储
typedef struct SeqList
{
	SLDataType* array; // 指向动态开辟的数组
	size_t size; // 有效数据个数
	size_t capicity; // 容量空间的大小
}SeqList;
// 基本增删查改接口
void SeqListInit(SeqList* psl, size_t capacity);
void SeqListDestory(SeqList* psl);
void CheckCapacity(SeqList* psl);
void SeqListPushBack(SeqList* psl, SLDataType x);
void SeqListPopBack(SeqList* psl);
void SeqListPushFront(SeqList* psl, SLDataType x);
void SeqListPopFront(SeqList* psl);
int SeqListFind(SeqList* psl, SLDataType x);
void SeqListInsert(SeqList* psl, size_t pos, SLDataType x);
void SeqListErase(SeqList* psl, size_t pos);
void SeqListRemove(SeqList* psl, SLDataType x);
void SeqListModify(SeqList* psl, size_t pos, SLDataType x);
void SeqListPrint(SeqList* psl);
void SeqListBubbleSort(SeqList* psl);
int SeqListBinaryFind(SeqList* psl, SLDataType x);
void SeqListRemoveAll(SeqList* psl, SLDataType x);

#endif // _SEQLIST_H_

seqlist.c

#include "seqlist.h"
//将顺序表置空
void SeqListInit(SeqList* psl, size_t capacity) {
	assert(psl);
	psl->capicity = capacity;
	psl->array = (SLDataType*)malloc(capacity*sizeof(SLDataType));
	assert(psl->array);
	psl->size = 0;
}
//将顺序表销毁
void SeqListDestory(SeqList* psl) {
	assert(psl);
	if (psl->array) {
		free(psl->array);
		psl->capicity = 0;
		psl->size = 0;
		psl->array = NULL;
	}
}
//顺序表扩容
void CheckCapacity(SeqList* psl) {
	assert(psl);
	if (psl->size == psl->capicity) {
		psl->capicity *= 2;
		psl->array = (SLDataType*)realloc(psl->size, psl->capicity*sizeof(SLDataType));
	}
}
//顺序表尾插
void SeqListPushBack(SeqList* psl, SLDataType x) {
	assert(psl);
	CheckCapacity(psl);
	psl->array[psl->size] = x;
	psl->size++;
}
//顺序表尾删
void SeqListPopBack(SeqList* psl) {
	assert(psl || psl->size);
	psl->size--;
}
//顺序表头插
void SeqListPushFront(SeqList* psl, SLDataType x) {
	assert(psl);
	CheckCapacity(psl);
	for (int i = psl->size - 1; i >=0; i--) {
		psl->array[i + 1] = psl->array[i];
	}
	psl->array[0] = x;
	psl->size++;
}
//顺序表头删
void SeqListPopFront(SeqList* psl) {
	assert(psl);
	psl->size--;
	for (int i = 0; i < psl->size; i++) {
		psl->array[i] = psl->array[i + 1];
	}
}
//顺序表查找元素x
int SeqListFind(SeqList* psl, SLDataType x) {
	assert(psl);
	for (int i = 0; i < psl->size; i++) {
		if (psl->array[i] == x) {
			return i;
		}
	}
	return -1;
}
//在顺序表pos位置插入x
void SeqListInsert(SeqList* psl, size_t pos, SLDataType x) {
	assert(psl || pos <= psl->size);
	CheckCapacity(psl);
	for (int i = psl->size - 1; i >= pos; i--) {
		psl->array[i + 1] = psl->array[i];
	}
	psl->array[pos] = x;
	psl->size++;
}
//在顺序表删除pos号元素
void SeqListErase(SeqList* psl, size_t pos) {
	assert(psl);
	psl->size--;
	for (int i = pos; i <= psl->size; i++) {
		psl->array[i] = psl->array[i + 1];
	}
}
//删除顺序表的元素x
void SeqListRemove(SeqList* psl, SLDataType x) {
	assert(psl);
	int pos = SeqListFind(psl, x);
	if (pos) {
		SeqListErase(psl, pos);
	}
}
//将顺序表pos位置的元素改为x
void SeqListModify(SeqList* psl, size_t pos, SLDataType x) {
	assert(psl || pos <= psl->size);
	psl->array[pos] = x;
}
//打印顺序表
void SeqListPrint(SeqList* psl) {
	assert(psl);
	for (int i = 0; i <= psl->size; i++) {
		printf("%d ", psl->array[i]);
	}
	putchar('\n');
}
//顺序表的冒泡排序
void SeqListBubbleSort(SeqList* psl) {
	assert(psl);
	SLDataType tmp;
	for (int i = 0; i < psl->size - 1; i++) {
		for (int j = 0; j < psl->size - 1 - i; j++) {
			if (psl->array[j]>psl->array[j + 1]) {
				tmp = psl->array[j];
				psl->array[j] = psl->array[j + 1];
				psl->array[j + 1] = tmp;
			}
		}
	}
}
//顺序表的二分查找
int SeqListBinaryFind(SeqList* psl, SLDataType x) {
	int left = 0;
	int right = psl->size - 1;
	int mid;
	while (left <= right) {
		mid = (left + right) / 2;
		if (x > psl->array[mid]) {
			left++;
		}
		else if (x < psl->array[mid]) {
			right--;
		}
		else {
			return mid;
		}
	}
	return -1;
}
// 时间复杂度:O(N) 空间复杂度: O(1)
void SeqListRemoveAll(SeqList* psl, SLDataType x) {
	assert(psl);
	int pos = SeqListFind(psl, x);
	if (pos) {
		SeqListErase(psl, pos);
	}
}

main.c

#include "seqlist.h"

int main() {
	SeqList test;
	SeqListInit(&test, 10);
	SeqListPushBack(&test, 1);
	SeqListPushBack(&test, 2);
	SeqListPushBack(&test, 3);
	SeqListPushBack(&test, 4);
	SeqListPushBack(&test, 5);
	SeqListPushBack(&test, 6);
	SeqListPushBack(&test, 7);
	SeqListPushBack(&test, 8);
	SeqListPushBack(&test, 9);
	SeqListPopBack(&test);
	SeqListPopBack(&test);
	SeqListPushFront(&test, 0);
	SeqListPopFront(&test);
	SeqListInsert(&test, 1, 16);
	SeqListErase(&test, 2);
	SeqListBubbleSort(&test);
	SeqListPrint(&test);
	SeqListDestory(&test);
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值