顺序表

1.SeqList.h

#pragma once

//#ifndef _SEQ_LIST_H_
//#define _SEQ_LIST_H_

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

typedef int DataType;
#define MAX_SIZE (100)

typedef struct SeqList {
	int arr[MAX_SIZE];//数据空间
	int size;//1:数据个数 2:当前可用的数组下标 
}SeqList;


//1.初始化
void SeqListInit(SeqList *pSL);
//2.销毁
void SeqListDestroy(SeqList *pSL);

///
//3.增 删 改 查
//增
//尾插
void SeqListPushBack(SeqList *pSL, DataType data);
//头插
void SeqListPushFront(SeqList *pSL, DataType data);
//根据下标插入
void SeqListInsert(SeqList *pSL, int pos, DataType data);

///
//删
//尾删
void SeqListPopBack(SeqList *pSL);
//头删
void SeqListPopFront(SeqList *pSL);
//根据下标删
void SeqListPopErase(SeqList *pSL, int pos);


//根据数据删除,只删除遇到的第一个
void SeqListRemove(SeqList *pSL, DataType data);
//根据数据删除,删除所遇到的
//1 2 3 5 3 3 4  -> 1 2 5 4(删3后)
void SeqListRemoveAll(SeqList *pSL, DataType data);


//根据下标更新
void SeqListUpData(SeqList *pSL, int pos, DataType data);


//查询
//返回遇到的第一个下标,如果没有遇到。返回-1
int SeqListFind(SeqList *pSL, DataType data);

//排序
void SeqListSort(SeqList *pSL);



//#endif

2.SeqList.c

#include "seqList.h"

//初始化
void SeqListInit(SeqList *pSL)
{
	//内容初始化
	//size = 0
	assert(pSL != NULL);

	memset(pSL->arr, 0, MAX_SIZE * sizeof(DataType));	//	arr[]数组初始化
	pSL->size = 0;				//size 初始化
}


//销毁
void SeqListDestroy(SeqList *pSL)
{
	assert(pSL != NULL);

	memset(pSL->arr, 0, MAX_SIZE * sizeof(DataType));
	pSL->size = 0;
}

///

//增:尾插
void SeqListPushBack(SeqList *pSL, DataType data)
{
	assert(pSL != NULL);
	assert(pSL->size < MAX_SIZE);

	pSL->arr[pSL->size] = data;
	pSL->size++;
}

//头插
void SeqListPushFront(SeqList *pSL, DataType data)
{
	assert(pSL != NULL);
	assert(pSL->size < MAX_SIZE);

	int i;
	for (i = pSL->size; i > 0; i--) {
		pSL->arr[i] = pSL->arr[i - 1];
	}
	pSL->arr[0] = data;
	pSL->size++;
}

//根据下标插入
void SeqListInsert(SeqList *pSL, int pos, DataType data)
{
	assert(pSL != NULL);
	assert(pSL->size < MAX_SIZE);
	assert(pos >= 0 && pos < pSL->size);

	// 把 [pos, size) 数据往后搬一格
	int i;
	for (i = pSL->size; i > pos; i--) {
		pSL->arr[i] = pSL->arr[i - 1];
	}
	pSL->arr[pos] = data;
	pSL->size++;
}

///

//删
//尾删
void SeqListPopBack(SeqList *pSL)
{
	assert(pSL != NULL);
	assert(pSL->size > 0);

	pSL->size--;
}

//头删
void SeqListPopFront(SeqList *pSL)
{
	assert(pSL != NULL);
	assert(pSL->size > 0);

	// 把 [1, pSL->size) 的数据往前搬移一格
	int i;
	for (i = 1; i < pSL->size; i++) {
		pSL->arr[i - 1] = pSL->arr[i];
	}
	pSL->size--;
}

//根据下标删
void SeqListPopErase(SeqList *pSL, int pos)
{
	assert(pSL != NULL);
	assert(pSL->size > 0);
	assert(pos >= 0 && pos < pSL->size);

	//1.
	/*
	for (; pos < pSL->size; pos++) {
		pSL->arr[pos] = pSL->arr[pos + 1];
	}
	pSL->size--;
	*/

	//2.
	int space;
	for (space = pos + 1; space < pSL->size; space++) {
		pSL->arr[space - 1] = pSL->arr[space];
	}
	pSL->size--;
	
}

//根据数据删除,只删除遇到的第一个
void SeqListRemove(SeqList *pSL, DataType data)
{
	assert(pSL != NULL);

	//int pos = SeqListFind(pSL, data);
	//if (pos != -1) {
	//	// 如果找到了
	//	SeqListPopErase(pSL, pos);
	//}

	int pos = 0;
		while (pSL->arr[pos] != data) {
			if(pos < pSL->size)
				pos++;
		}
	SeqListPopErase(pSL, pos);//根据下标删
}


//根据数据删除,删除所遇到的
//1 2 3 5 3 3 4  -> 1 2 5 4(删3后)
void SeqListRemoveAll(SeqList *pSL, DataType data)
{
	assert(pSL != NULL);

	//方法1
	/*int pos = 0;
	while ((pos = SeqListFind(pSL, data)) != -1) {
		SeqListPopErase(pSL, pos);
	}*/

	//2.
	DataType *newArr = (DataType *)malloc(sizeof(DataType)* pSL->size);
	assert(newArr);

	int i, j, k;

	for (i = 0, j = 0; i < pSL->size; i++) {
		if (pSL->arr[i] != data) {
			newArr[j] = pSL->arr[i];
			j++;
		}
	}

	pSL->size = j;

	// 把数据从 newArray 放回到 arr
	for (k = 0; k < pSL->size; k++) {
		pSL->arr[k] = newArr[k];
	}

	free(newArr);

}


//打印
void SeqListPrint(SeqList *pSL)
{
	int i;
	for (i = 0; i < pSL->size; i++) {
		printf("%d ", pSL->arr[i]);
	}
	printf("\n");
}
///

//根据下标更新:将数组下标为pos处的数据更新
void SeqListUpData(SeqList *pSL, int pos, DataType data)
{
	assert(pSL != NULL);
	assert(pos >= 0 && pos < pSL->size);

	pSL->arr[pos] = data;
}

//查询
//返回遇到的第一个下标,如果没有遇到。返回-1
int SeqListFind(SeqList *pSL, DataType data)
{
	assert(pSL != NULL);

	int pos;
	for (pos = 0; pos < pSL->size; pos++) {
		if (pSL->arr[pos] == data)
			return pos;	
	}
	return -1;
}

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

void SeqListSort(SeqList *pSL)
{
	/*
	minSpace 用来放找到的最小的数的下标
	maxSpace 用来放找到的最大的数的下标

	minIndex 整个数列中([minSpace,maxSpace])找到的最小数的下标
	maxIndex 整个数列中([minSpace,maxSpace])找到的最大数的下标
	*/
	int minSpace = 0;
	int maxSpace = pSL->size - 1;
	int i;
	int minIndex, maxIndex;

	while (minSpace < maxSpace) {
		minIndex = minSpace;
		maxIndex = minSpace;

		for (i = minSpace; i <= maxSpace; i++) {
			// 遍历 [minSpace, maxSpace],找到最小数的下标,找到最大数的下标

			if (pSL->arr[i] < pSL->arr[minIndex]) {
				minIndex = i;
			}

			if (pSL->arr[i] > pSL->arr[maxIndex]) {
				maxIndex = i;
			}
		}

		// 到这里后,minIndex 就是找到的最小数的下标
		// 到这里后,maxIndex 就是找到的最大数的下标
		Swap(pSL->arr + minIndex, pSL->arr + minSpace);
		if (minSpace == maxIndex) {
			// 特殊情况处理,例如	9	1	7	3	2
			maxIndex = minIndex;
		}
		Swap(pSL->arr + maxIndex, pSL->arr + maxSpace);

		minSpace++;
		maxSpace--;
	}
}

3.Main.c

#include "seqList.h"
int main()
{
	SeqList sl;
	SeqListInit(&sl);//初始化

	//头插
	SeqListPushFront(&sl, 1);
	SeqListPushFront(&sl, 2);
	SeqListPushFront(&sl, 1);

	//尾插
	SeqListPushBack(&sl, 520);
	SeqListPushBack(&sl, 1314);

	//按下标插
	SeqListInsert(&sl, 2, 666);
	SeqListInsert(&sl, (&sl)->size - 1, 666);
	//打印
	SeqListPrint(&sl);

	//排序
	SeqListSort(&sl);
	SeqListPrint(&sl);


	尾删
	//SeqListPopBack(&sl);
	//SeqListPrint(&sl);

	头删
	//SeqListPopFront(&sl);
	//SeqListPrint(&sl);

	按下标删
	//SeqListPopErase(&sl,2);
	//SeqListPrint(&sl);

	//删除所遇到的
	/*
	SeqListRemoveAll(&sl, 666);
	SeqListPrint(&sl);
	*/

	//删除遇到的第一个
	/*
	SeqListRemove(&sl, 1);
	SeqListPrint(&sl);*/



	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值