静态顺序表的增删改查

头文件seqList.h

#pragma once

typedef int DataType;
#define MAX_SIZE (100)
#include<stdio.h>
#include<stdlib.h> 
#include<assert.h>
#include<string.h>


typedef struct SeqList{
	DataType array[MAX_SIZE];
	int size;
}SeqList;
//初始化
void SeqListInit(SeqList *psl);
//销毁
void SeqListDestroy(SeqList *psl);
//增删改查
//增
//尾插
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 SeqListErase(SeqList *psl, int pos);
//根据数据删除,只删除遇到的第一个
void SeqListRemove(SeqList *psl, DataType data);
//根据数据删除,删除所有遇到的
void SeqListRemoveAll(SeqList *psl, DataType data);
//根据下标更新
void SeqListUpdate(SeqList *psl, int pos, DataType data);
//查询
//返回遇到的第一个下标,如果没有遇到,返回-1
int SeqListFind(SeqList *psl, DataType data);


void SeqListPrint(SeqList *psl);


seqList.c

#include "seqlist.h"
void SeqListInit(SeqList *psl)
{
	assert(psl);
	//memset(psl->array, 0, MAX_SIZE*sizeof(DataType));
	psl->size = 0;
}
//销毁
void SeqListDestroy(SeqList *psl)
{
	assert(psl);
	psl->size = 0;
}
//尾插
void SeqListPushBack(SeqList *psl, DataType data)
{
	assert(psl);
	assert(psl->size < MAX_SIZE);
	psl->array[psl->size] = data;
	psl->size++;
}

//头插

void SeqListPushFront(SeqList *psl,DataType data)
{
	assert(psl);
	assert(psl->size < MAX_SIZE);
	int pos;
	for (pos = psl->size-1; pos >= 0; pos--)
	{
		psl->array[pos+1] = psl->array[pos];
	}
	psl->array[0 ] = data;
	psl->size++;

}


//根据下标插
void SeqListInsert(SeqList *psl, int pos, DataType data)
{
	assert(psl);
	assert(pos >= 0 && pos < psl->size);
	int space;
	for (space = psl->size; space>pos; space--)
	{
		psl->array[space] = psl->array[space - 1];
	}
	psl->array[pos] = data;
	psl->size++;
}

//尾删
void SeqListPopBack(SeqList *psl)
{
	psl->size--;
}
//头删
void SeqListPopFront(SeqList *psl)
{
	assert(psl);
	int pos;
	for (pos = 1; pos < psl->size; pos++)
	{
		psl->array[pos-1] = psl->array[pos];
	}
	psl->size--;

}
//根据下标删
void SeqListErase(SeqList *psl, int pos)
{
	assert(psl);
	assert(pos >= 0 && pos < psl->size);
	int p;
	for (p = pos + 1; p < psl->size; p++)
	{
		psl->array[p - 1] = psl->array[p];
	}
	psl->size--;

}
//根据数据删除,只删除遇到的第一个
void SeqListRmove(SeqList *psl, DataType data)
{
	int pos = SeqListFind(psl, data);
	SeqListErase(psl, pos);
	psl->size--;
}
//根据数据删除,删除所有遇到的
void SeqListRemoveAll(SeqList *psl, DataType data)
{
	//调用函数
	int pos;
	/*while (1)
	{
		pos = SeqListFind(psl, data);
		if (pos == -1)
		{
			break;
		}
		SeqListErase(psl, pos);
	}*/
	while ((pos = SeqListFind(psl, data)) != -1)
	{
		SeqListErase(psl, pos);
	}
	//DataType *newArray = (DataType*)malloc(sizeof(DataType)*psl->size);
	//int i, j, k;
	//for (i = 0, j = 0; i < psl->size; i++)
	//{
	//	if (psl->array[i] != data)
	//	{
	//		newArray[j] = psl->array[i];
	//		j++;
	//	}
	//	
	//}
	//psl->size = j;

	////把新数组替换回旧数组
	//for (k = 0; k < psl->size; k++)
	//{
	//	psl->array[k] = newArray[k];
	//}
	//free(newArray);

	//????
	/*int i, j;
	for (i = 0; j = 0, i < psl->size; i++)
	{
		if (psl->array[i] != data)
		{
			psl->array[j] = psl->array[i];
			j++;
		}
	}
	psl->size = j;*/

}


//根据下标更新

void SeqListUpdate(SeqList *psl, int pos, DataType data)
{
	assert(psl);
	assert(pos >= 0 && pos <= psl->size);
	psl->array[pos] = data;
}
//查找函数,返回遇到的第一个下标,如果没有遇到,返回-1
int  SeqListFind(SeqList *psl, DataType data)
{
	assert(psl);
	int i;
	for (i = 0; i < psl->size; i++)
	{
		if (psl->array[i] == data)
		{
			return i;
		}
	}
	return -1;
}



void swap(DataType *a, DataType *b)
{
	DataType t = *a;
	*a = *b;
	*b = t;
}
//从小到大排序
void SelectOP(SeqList *psl)
{
	//minspace 用来放找的最小的数的下标,maxspace用来放找的最大的数的下标
	//minIndex整个数列中找到的最小的数的下标,MaxIndex整个数列中找到的最大的数的下标
	int minSpace = 0;
	int maxSpace = psl->size - 1;
	int i;
	int minIndex, maxIndex;
	while (minSpace < maxSpace)
	{
		minIndex = minSpace;
		maxIndex = maxSpace;

		for (i = minSpace; i <= maxSpace; i++)//从minSpace 到maxSpace遍历,找到最小数的下标和最大数的下标
		{
			if (psl->array[i] < psl->array[minIndex])
			{
				minIndex = i;
			}
			if (psl->array[i]>psl->array[maxIndex])
			{
				maxIndex = i;
			}
		}
		//到这里表示minIndex就是找到最小数的下标
		//maxIndex就是找到最大数的下标
		swap(psl->array + minIndex, psl->array + minSpace);

		//特殊情况,如9,5,4,8,2
		if (minSpace == maxIndex)
		{
			maxIndex = minIndex;
		}
		swap(psl->array + maxIndex, psl->array + maxSpace);

		minSpace++, maxSpace--;
	}
}


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

主函数main.c

#include "seqlist.h"



int main()
{
	SeqList sl;
	SeqListInit(&sl);
	SeqListPushBack(&sl, 9);
	SeqListPushBack(&sl, 8);
	SeqListPushBack(&sl, 4);
	SeqListPushBack(&sl, 6);
	SeqListPushBack(&sl, 3);
	SeqListPushBack(&sl, 5);
	

	SeqListInit(&sl, 5);

	SeqListPrint(&sl);
	SeqListPushBack(&sl, 8);
	SeqListPrint(&sl);
	SeqListPopBack(&sl);
	SeqListPrint(&sl);
	SeqListPopFront(&sl);
	SeqListPrint(&sl);
	SeqListErase(&sl, 2);
	SeqListPrint(&sl);
	SeqListRmove(&sl, 3);
	SeqListPrint(&sl);
	SeqListRemoveAll(&sl, 3);
	SeqListPrint(&sl);
	SelectOP(&sl);
	SeqListPrint(&sl);
	system("pause");
	return 0;
}






















猜你喜欢

转载自blog.csdn.net/Freedom_222/article/details/80682557