顺序表的实现

一、Sysuti.h//系统头文件

#ifndef _SYSUTIL_H_
#define _SYSUTIL_H_

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

#include<vld.h>
#pragma warning (disable:4996)

#endif

二、Seqlist.h//函数功能实现头文件

#include"Sysutil.h"

#define ElemType int
#define SEQLIST_DEFAULT_SIZE 8
#define SEQLIST_INC_SIZE 4

typedef struct SeqList
{
	ElemType *base;
	size_t    capacity;
	size_t    size;
}SeqList;

bool IsFull(SeqList *pst){
	return pst->size == pst->capacity;
}
bool IsEmpty(SeqList *pst){
	return pst->size == 0;
}

void SeqListInit(SeqList *pst);
void SeqListPushBack(SeqList *pst, ElemType x);
void SeqListPushFront(SeqList *pst, ElemType x);
void SeqListShow(SeqList *pst);
void SeqListPopBack(SeqList *pst);
void SeqListPopFront(SeqList *pst);
int SeqListLength(SeqList *pst);
int SeqListCapacity(SeqList *pst);
void SeqListClear(SeqList *pst);
int SeqListFind(SeqList *pst, ElemType x);
void SeqListSort(SeqList *pst);
void SeqListDeleteByVal(SeqList *pst,ElemType x);
void SeqListDeleteByPos(SeqList *pst, ElemType pos);
void SeqListDestroy(SeqList *pst);
void SeqListReserve(SeqList *pst);
void SeqListInsertByVal(SeqList *pst, ElemType x);
void SeqListInsertByPos(SeqList *pst, ElemType item, ElemType pos);
int SeqListFindByBinary(SeqList *pst, ElemType key);
void SeqListRemoveAll(SeqList *pst, ElemType key);

bool _Inc(SeqList *pst)
{
	ElemType *new_base = (ElemType*)realloc(pst->base, sizeof(ElemType)*(pst->capacity + SEQLIST_INC_SIZE));
	if (new_base == NULL)
	{
		printf("扩容失败.\n");
		return false;
	}
	pst->base = new_base;
	pst->capacity += SEQLIST_INC_SIZE;
	return true;
}
//bool _Inc(SeqList *pst)
//{
//	//申请新空间
//	//ElemType *new_base = (ElemType*)malloc(sizeof(ElemType)*(pst->capacity+SEQLIST_INC_SIZE));
//	ElemType *new_base = (ElemType*)malloc(sizeof(ElemType)*(pst->capacity * 2));
//	if (new_base == NULL)
//	{
//		printf("扩容失败.\n");
//		return false;
//	}
//	//拷贝数据
//	memcpy(new_base, pst->base, sizeof(ElemType)*pst->capacity);
//
//	//释放旧空间
//	free(pst->base);
//
//	//更改base的指向
//	pst->base = new_base;
//	pst->capacity += SEQLIST_INC_SIZE;
//	return true;
//}

void SeqListInit(SeqList *pst)
{
	assert(pst);
	pst->base = (ElemType*)malloc(sizeof(ElemType) * SEQLIST_DEFAULT_SIZE);
	assert(pst->base != NULL); //开辟空间成功
	memset(pst->base, 0, sizeof(ElemType) * SEQLIST_DEFAULT_SIZE);
	pst->capacity = SEQLIST_DEFAULT_SIZE;
	pst->size = 0;
}
void SeqListPushBack(SeqList *pst, ElemType x){
	assert(pst);
	if (IsFull(pst) && !_Inc(pst)){
		printf("顺序表已满,%d无法进行尾部输入。\n",x);
		return;
	}
	pst->base[pst->size++] = x;
}
void SeqListPushFront(SeqList *pst, ElemType x){
	assert(pst);
	if (IsFull(pst) && !_Inc(pst)){
		printf("顺序表已满,%d无法进行头部插入。\n", x);
		return;
	}
	for (int i = pst->size; i >=0; i--){
		pst->base[i+1] = pst->base[i];
	}
	pst->base[0] = x;
	pst->size++;
	return;
}
void SeqListShow(SeqList *pst){
	assert(pst);
	for (int i = 0; i < pst->size; ++i){
		printf("%d ", pst->base[i]);
	}
	printf("\n");
}
void SeqListPopBack(SeqList *pst){
	assert(pst);
	if (IsEmpty(pst)){
		printf("顺序表已空,不能进行尾部删除。\n");
		return;
	}
	pst->size--;
}
void SeqListPopFront(SeqList *pst){
	assert(pst);
	if (IsEmpty(pst)){
		printf("顺序表已空,不能进行头部删除。\n");
		return;
	}
	for (int i = 1; i < pst->size; i++){
		pst->base[i -1] = pst->base[i];
	}
	pst->size--;
}
int SeqListLength(SeqList *pst){
	return pst->size;
}
int SeqListCapacity(SeqList *pst){
	return pst->capacity;
}
void SeqListClear(SeqList *pst){
	pst->size = 0;
}
void SeqListInsertByVal(SeqList *pst, ElemType x){
	assert(pst);
	if (IsFull(pst) && !_Inc(pst)){
		printf("无法按值插入,表已满。\n");
		return;
	}
	//插入之前要保证原来的数组已经有顺序了
	int pos = 0;
	while (pos<pst->size && (x>pst->base[pos])){
		pos++;
	}
	for (int i = pst->size; i > pos; i--){
		pst->base[i] = pst->base[i-1];
	}
	pst->base[pos] = x;
	pst->size++;

}
void SeqListInsertByPos(SeqList *pst, ElemType item, ElemType pos){
	assert(pst);
	if (IsFull(pst) && !_Inc(pst)){
		printf("无法进行按位置插入,表已慢。\n");
		return;
	}
	if (pos<0 || pos>pst->size ){
		printf("插入的位置不符合要求。\n");
		return;
	}
	for (int i = pst->size; i > pos; i--){
		pst->base[i] = pst->base[i - 1];
	}
	pst->base[pos] = item;
	pst->size++;
}
void SeqListDeleteByVal(SeqList *pst, ElemType x){
	assert(pst);
	int index=SeqListFind(pst, x);
	if (index == -1){
		printf("你要删除的值%d不存在。\n",x);
		return;
	}
	for (int i = index; i < pst->size; i++){
		pst->base[i] = pst->base[i + 1];
	}
	pst->size--;
}
void SeqListDeleteByPos(SeqList *pst, int pos)
{
	assert(pst && (pos >= 0 && pos<pst->size));
	for (int i = pos; i<pst->size; ++i)
		pst->base[i] = pst->base[i + 1];
	pst->size--;
}

int SeqListFind(SeqList *pst, ElemType x){
	assert(pst);
	for (int i = 0; i < pst->size; i++){
		if (pst->base[i] == x){
			return i;
		}
	}
	return -1;
}
void SeqListSort(SeqList *pst){
	for (int i = 0; i < pst->size - 1; i++){
		for (int j = 0; j < pst->size - 1 - i; j++){
			if (pst->base[j]>pst->base[j + 1]){
				ElemType tmp = pst->base[j];
				pst->base[j] = pst->base[j + 1];
				pst->base[j + 1] = tmp;
			}
		}
	}
}
void SeqListReserve(SeqList *pst){
	assert(pst);
	int start = 0;
	int end = pst->size - 1;
	while (start < end){
		int temp = pst->base[start];
		pst->base[start] = pst->base[end];
		pst->base[end] = temp;

		start++;
		end--;
	}
  
}
void SeqListRemoveAll(SeqList *pst, ElemType key){
	assert(pst);
	int index;
	do
	{
		index = SeqListFind(pst, key);
		if (index == -1)
			break;
		SeqListDeleteByPos(pst, index);
	} while (1);
}
int SeqListFindByBinary(SeqList *pst, ElemType key){
	assert(pst);
	SeqListSort(pst);
	int start = 0;
	int end = pst->size - 1;
	int mid;
	while (start <= end){
		mid = (start + end) / 2;
		if (key == pst->base[mid]){
			return mid;
		}
		else if (key < pst->base[mid]){
			end = mid - 1;
		}
		else{
			start = mid + 1;
		}
	}
	return -1;
}

void SeqListDestroy(SeqList *pst){
	assert(pst);
	free(pst->base);
	pst->base = NULL;
	pst->capacity = pst->size = 0;
}

三、TestMain.c//源函数

#include"Seqlist.h"

int main(){

	SeqList mylist;
	SeqListInit(&mylist);

	ElemType item,key;
	int pos;

	int select = 1;
	while (select){
		printf("********************************************\n");
		printf("* [1] push_back        [2] push_front      *\n");
		printf("* [3] show_list        [0] quit_system     *\n");
		printf("* [4] pop_back         [5] pop_front       *\n");
		printf("* [6] length           [7] capacity        *\n");
		printf("* [8] insert_val       [9] insert_pos      *\n");
		printf("* [10] delete_val      [11] delete_pos     *\n");
		printf("* [12] find            [13] sort           *\n");
		printf("* [14] reverse         [15] remove_all     *\n");
		printf("* [16] clear           [*17] destroy       *\n");
		printf("* [18] find_binary                         *\n");
		printf("********************************************\n");
		printf("请选择:>");
		scanf("%d", &select);
		switch (select){
		case 1:
			printf("请输入要插入的数据(以-1结束):>");
			while (scanf("%d", &item), item != -1)  //逗号表达式
				SeqListPushBack(&mylist, item);
			break;
		case 2:
			printf("请输入要插入的数据(以-1结束:>");
			while (scanf("%d", &item), item != -1)
				SeqListPushFront(&mylist, item);
			break;
		case 3:
			SeqListShow(&mylist);
			break;
		case 4:
			SeqListPopBack(&mylist);
			break;
		case 5:
			SeqListPopFront(&mylist);
			break;
		case 6:
			printf("表长为:>%d\n",SeqListLength(&mylist));
			break;
		case 7:
			printf("表的容量为:>%d\n",SeqListCapacity(&mylist));
			break;
		case 8:
			printf("请输入你要按值插入的数:>");
			scanf("%d", &item);
			SeqListInsertByVal(&mylist, item);
			break;
		case 9:
			printf("请输入你要插入的位置:>");
			scanf("%d", &pos);
			printf("请输入你要按值插入的数:>");
			scanf("%d", &item);
			SeqListInsertByPos(&mylist, item, pos);
			break;
		case 10:
			printf("请输入你要删除的值:>");
			scanf("%d", &key);
			SeqListDeleteByVal(&mylist, key);
			break;
		case 11:
			printf("请输入你要删除的值的位置:>");
			scanf("%d", &pos);
			SeqListDeleteByPos(&mylist, pos);
			break;
		case 12:
			printf("请输入你要查找的值:>");
			scanf("%d", &key);
			pos = SeqListFind(&mylist, key);
			if (pos == -1){
				printf("你要找的%d不存在。\n", key);
			}
			else {
				printf("你要找的%d在下标为%d处。\n", key, pos);
			}
			break;
		case 13:
			SeqListSort(&mylist);
			break;
		case 14:
			SeqListReserve(&mylist);
			break;
		case 15:
			printf("请输入要删除的值:>");
			scanf("%d", &key);
			SeqListRemoveAll(&mylist, key);
			break;
		case 16:
			SeqListClear(&mylist);
			break;
		case 18:
			printf("请输入你要查找的值:>");
			scanf("%d", &key);
			int pos=SeqListFindByBinary(&mylist,key);
			if (pos == -1){
				printf("你要查找的值不存在。\n");
			}
			else{
				printf("你要查找的值%d在位置%d处。\n", key, pos);
			}
			break;
		}
		system("pause");
		system("cls");
	}
	SeqListDestroy(&mylist);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值