顺序表相关操作

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kevin980123 https://blog.csdn.net/kevin980123/article/details/83617611

顺序表相关操作


程序代码如下:


SeqList.h


#ifndef __SEQLIST_H__
#define __SEQLIST_H__

#include <stdio.h>
#include <assert.h>
#define MAX 10
typedef int DataType;
typedef struct SeqList
{
	DataType data[MAX];
	int sz;
}SeqList,*pSeqList;
//初始化 
void InitSeqList(pSeqList pSeq);
//尾部插入 
void PushBack(pSeqList pSeq, DataType data);
//尾部删除 
void PopBack(pSeqList pSeq);
//头部插入 
void PushFront(pSeqList pSeq, DataType data);
//头部删除 
void PopFront(pSeqList pSeq);
//查找指定元素 
int Find(pSeqList pSeq, DataType data);
//指定位置插入 
void Insert(pSeqList pSeq, int pos, DataType data);
//删除指定位置元素 
void Erase(pSeqList pSeq, int pos);
//删除指定元素 
void Remove(pSeqList pSeq, DataType data);
//删除所有的指定元素 
void RemoveAll(pSeqList pSeq, DataType data);
//返回顺序表的大小 
int Size(pSeqList pSeq);
//判断顺序表是否为空 
int Empty(pSeqList pSeq);
//冒泡排序 
void BubbleSort(pSeqList pSeq);
//选择排序 
void SelectSort(pSeqList pSeq);
//选择排序的优化 
void SelectSortOP(pSeqList pSeq);
//二分查找 
int BinarySearch(pSeqList pSeq, DataType data);
//二分查找递归写法 
int BinarySearch_R(pSeqList pSeq, int left, int right, DataType d);

//打印 
void PrintSeqList(pSeqList pSeq);


#endif //__SEQLIST_H__

SeqList.c

#include <string.h>
#include "SeqList.h"
//初始化
void InitSeqList(pSeqList pSeq)
{
	assert(pSeq);
	pSeq->sz = 0;
	memset(pSeq->data, 0, sizeof(pSeq->data));
}
//尾部插入
void PushBack(pSeqList pSeq,DataType data)
{
	assert(pSeq);
	if (pSeq->sz == MAX)
	{
		printf("顺序表已满,无法插入\n");
		return;
	}
	pSeq->data[pSeq->sz] = data;
	pSeq->sz++;
}
//尾部删除
void PopBack(pSeqList pSeq)
{
	assert(pSeq);
	if (pSeq->sz == 0)
	{
		return;
	}
	pSeq->sz--;

}
//头部插入 
void PushFront(pSeqList pSeq, DataType data)
{
	assert(pSeq);
	int i = 0;
	if (pSeq->sz == MAX)
	{
		printf("顺序表已满,无法插入\n");
		return;
	}
	for (i = pSeq->sz; i > 0; i--)
	{
		pSeq->data[i] = pSeq->data[i - 1];
	}
	pSeq->data[0] = data;
	pSeq->sz++;
}
//头部删除 
void PopFront(pSeqList pSeq)
{
	assert(pSeq);
	if (pSeq->sz)
	{
		int i = 0;
		for (i = 0; i < pSeq->sz - 1; i++)
		{
			pSeq->data[i] = pSeq->data[i + 1];
		}
		pSeq->sz--;
	}
}
//查找指定元素 
int Find(pSeqList pSeq, DataType data)
{
	assert(pSeq);
	int i = 0;
	for (i = 0; i < pSeq->sz; i++)
	{
		if (data == pSeq->data[i])
			return i;
	}
	return -1;
}
//指定位置插入 
void Insert(pSeqList pSeq, int pos, DataType data)
{
	assert(pSeq);
	int i = 0;
	if (pSeq->sz == MAX)
		return;
	for (i = pSeq->sz; i > pos; i--)
	{
		pSeq->data[i] = pSeq->data[i - 1];
	}
	pSeq->data[pos] = data;
	pSeq->sz++;
}
//删除指定位置元素 
void Erase(pSeqList pSeq, int pos)
{
	assert(pSeq);
	int i = 0;
	if (pSeq->sz == 0)
		return;
	for (i = pos; i < pSeq->sz - 1; i++)
	{
		pSeq->data[i] = pSeq->data[i + 1];
	}
	pSeq->sz--;
}
//删除指定元素 
void Remove(pSeqList pSeq, DataType data)
{
	assert(pSeq);
	int i = 0;
	int pos = 0;
	if (pSeq->sz == 0)
		return;
	while ((pos = Find(pSeq, data)) != -1)
	{
		for (i = pos; i < pSeq->sz - 1; i++)
		{
			pSeq->data[i] = pSeq->data[i + 1];
		}
		pSeq->sz--;
	}
}
//删除所有的指定元素 
void RemoveAll(pSeqList pSeq, DataType data)
{
	assert(pSeq);
	int i = 0;
	int j = 0;
	if (pSeq->sz == 0)
		return;
	while (i < pSeq->sz)
	{
		if (pSeq->data[i] != data)
		{
			if (i != j)
				pSeq->data[j] = pSeq->data[i];
			i++;
			j++;
		}
		else
		{
			i++;
		}
	}
	pSeq->sz = j;

}
//返回顺序表的大小 
int Size(pSeqList pSeq)
{
	assert(pSeq);
	return pSeq->sz;
}
//判断顺序表是否为空 
int Empty(pSeqList pSeq)
{
	assert(pSeq);
	return 0 == pSeq->sz;
}
//冒泡排序 
void BubbleSort(pSeqList pSeq)
{
	assert(pSeq);
	int i = 0;
	int j = 0;
	DataType tmp = 0;
	if (pSeq->sz == 0)
		return;
	for (i = 0; i < pSeq->sz - 1; i++)
	{
		for (j = 0; j < pSeq->sz - 1 - i; j++)
		{
			if (pSeq->data[j] > pSeq->data[j + 1])
			{
				tmp= pSeq->data[j];
				pSeq->data[j] = pSeq->data[j + 1];
				pSeq->data[j + 1] = tmp;
			}
		}
	}
}
//选择排序 
void SelectSort(pSeqList pSeq)
{
	assert(pSeq);
	int i = 0;
	int j = 0;
	DataType tmp = 0;
	if (pSeq->sz == 0)
		return;
	for (i = 0; i < pSeq->sz - 1; i++)
	{
		for (j = i + 1; j < pSeq->sz - 1; j++)
		{
			if (pSeq->data[i] > pSeq->data[j])
			{
				tmp = pSeq->data[i];
				pSeq->data[i] = pSeq->data[j];
				pSeq->data[j] = tmp;
			}
		}
	}
}
//选择排序的优化 
void SelectSortOP(pSeqList pSeq)
{
	assert(pSeq);
	int i = 0;
	int j = 0;
	int index = 0;
	DataType num = 0;
	DataType tmp = 0;
	if (pSeq->sz == 0)
		return;
	for (i = 0; i < pSeq->sz - 1; i++)
	{
		index = i;
		num = pSeq->data[i];
		for (j = i + 1; j < pSeq->sz - 1; j++)
		{
			if (num > pSeq->data[j])
			{
				index = j;
				num = pSeq->data[j];
			}
		}
		if (index != i)
		{
			tmp = pSeq->data[index];
			pSeq->data[index] = pSeq->data[i];
			pSeq->data[i] = tmp;
		}
	}
}
//二分查找 
int BinarySearch(pSeqList pSeq, DataType data)
{
	assert(pSeq);
	int left = 0;
	int right = pSeq->sz - 1;
	while (left <= right)
	{
		int mid = left + ((right - left) >> 1);
		if (data < pSeq->data[mid])
			right = mid - 1;
		else if (data == pSeq->data[mid])
			return mid;
		else
			left = mid + 1;
	}
	return -1;
}
//二分查找递归写法 
int BinarySearch_R(pSeqList pSeq, int left, int right, DataType d)
{
	assert(pSeq);
	
	if (left <= right)
	{
		int mid = left + ((right - left) >> 1);
		if (d == pSeq->data[mid])
			return mid;
		else if (d < pSeq->data[mid])
			return  BinarySearch_R(pSeq, left, mid - 1, d);
		else
			return BinarySearch_R(pSeq, mid + 1, right, d);
	}
	return -1;
}

//打印 
void PrintSeqList(pSeqList pSeq)
{
	assert(pSeq);
	int i = 0;
	if (pSeq->sz == 0)
	{
		printf("顺序表为空\n");
		return;
	}
	for (i = 0; i < pSeq->sz; i++)
	{
		printf("%d ", pSeq->data[i]);
	}
	printf("\n");
}

test.c

#include <stdio.h>
#include "SeqList.h"
#define SORT 0
#define BS 2

void test1()
{
	SeqList ps;
	//初始化 
	InitSeqList(&ps);
	//尾部插入 
	PushBack(&ps, 1);
	PushBack(&ps, 2);
	PushBack(&ps, 3);
	PushBack(&ps, 4);
	PushBack(&ps, 5);
	//打印 
	PrintSeqList(&ps);
	//尾部删除 
	PopBack(&ps);
	PopBack(&ps);
	//打印 
	PrintSeqList(&ps);
}

void test2()
{
	SeqList ps;
	//初始化 
	InitSeqList(&ps);
	//头部插入 
	PushFront(&ps, 6);
	PushFront(&ps, 5);
	PushFront(&ps, 5);
	PushFront(&ps, 4);
	PushFront(&ps, 3);
	PushFront(&ps, 1);
	PushFront(&ps, 0);
	//打印 
	PrintSeqList(&ps);
	//头部删除 
	PopFront(&ps);
	//打印 
	PrintSeqList(&ps);
	//查找指定元素 
	int num = Find(&ps, 3);
	if (num !=-1)
		//指定位置插入 
		Insert(&ps, num, 2);
	//打印 
	PrintSeqList(&ps);
	//查找指定元素 
	num = Find(&ps, 5);
	//删除指定位置元素 
	Erase(&ps, num);
	//打印 
	PrintSeqList(&ps);
	//删除指定元素 
	Remove(&ps, 5);
	//打印 
	PrintSeqList(&ps);
}

void test3()
{
	SeqList ps;
	//初始化 
	InitSeqList(&ps);
	//头部插入 
	PushFront(&ps, 9);
	PushFront(&ps, 7);
	PushFront(&ps, 11);
	PushFront(&ps, 6);
	PushFront(&ps, 2);
	PushFront(&ps, 8);
	PushFront(&ps, 11);
	//打印 
	PrintSeqList(&ps);
	//删除所有的指定元素 
	RemoveAll(&ps, 11);
	//打印 
	PrintSeqList(&ps);
	//返回顺序表的大小 
	int sz = Size(&ps);
	printf("顺序表大小:%d\n", sz);
	//判断顺序表是否为空 
	int ret = Empty(&ps);
	if (0 == ret)
		printf("顺序表不为空\n");
	else
		printf("顺序表为空\n");
	switch (SORT)
	{
	case 1:
		//选择排序 
		SelectSort(&ps);
		break;
	case 2:
		//选择排序的优化 
		SelectSortOP(&ps);
		break;
	default:
		//冒泡排序 
		BubbleSort(&ps);
		break;

	}
	//打印 
	PrintSeqList(&ps);
	if (1 == BS)
	{
		//二分查找 
		ret = BinarySearch(&ps, 7);
		if (ret == -1)
			printf("找不到该元素\n");
		else
			printf("该元素位于顺序表第%d位\n",ret);
	}
	else
	{
		//二分查找递归写法 
		ret = BinarySearch_R(&ps, 0, ps.sz - 1, 12);
		if (ret == -1)
			printf("找不到该元素\n");
		else
			printf("该元素位于顺序表第%d位\n", ret);
	}
}

int main()
{
	//test1();
	//test2();
	test3();
	system("pause");
	return 0;
}

程序运行结果如下:


test1();
在这里插入图片描述

test2();
在这里插入图片描述

test3();
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/kevin980123/article/details/83617611