数据结构--顺序表

seqlist.h

#ifndef __SEQLIST_H
#define __SEQLIST_H

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

#define MAX 100
typedef int DataType;

//定义一个顺序表
typedef struct SeqList
{
	DataType data[MAX];
	int sz;//顺序表中有效元素的个数
}SeqList,*pSeqList;


void InitSeqList(pSeqList pSeq);//初始化
void PushBack(pSeqList pSeq, DataType data);//尾部插入
void PrintSeqList(pSeqList pSeq);
void PopBack(pSeqList pSeq);//尾部删除 
void PushFront(pSeqList pSeq, DataType d);//头部插入 
void PopFront(pSeqList pSeq);//头部删除 
int Find(pSeqList pSeq, DataType d);//查找指定元素 
void Insertp(pSeqList pSeq, int pos, DataType d);//指定位置插入 
void Erase(pSeqList pSeq, int pos);//删除指定位置元素 
void Remove(pSeqList pSeq, DataType d);//删除指定元素 
void RemoveAll(pSeqList pSeq, DataType d);//删除所有的指定元素 
int Size(pSeqList pSeq);//返回顺序表的大小 
int Empty(pSeqList pSeq);//判断顺序表是否为空 
void BubbleSort(pSeqList pSeq);// 冒泡排序
 
void SelectSort(pSeqList pSeq);//选择排序

void SelectSortOP(PSeqList pSeq);
void SelectSortOP(PSeqList pSeq);//选择排序的优化 

int BinarySearch(PSeqList pSeq, DataType d);//二分查找 

int BinarySearch_R(pSeqList pSeq, int left, int right, DataType d);//二分查找递归写法 
#endif

seqlist.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"seqlist.h"
void InitSeqList(pSeqList  pSeq)
{
	assert(pSeq != NULL);
	pSeq->sz = 0;
	memset(pSeq->data, 0, sizeof(pSeq->data));
}
void PushBack(pSeqList pSeq, DataType d)
{
	assert(pSeq != NULL);
	if (pSeq->sz == MAX )
	{
		printf("顺序表已满,无法插入\n");
		return;
	}
	pSeq->data[pSeq->sz] = d;
	pSeq->sz++;
}
void PopBack(pSeqList pSeq)
{
	assert(pSeq != NULL);
	if (pSeq->sz == NULL)
	{
		printf("顺序表为空,无法删除\n");
		return;
	}
	pSeq->sz--;
}
void PrintSeqList(pSeqList pSeq)
{
	assert(pSeq != NULL);
	int i = 0;
	for (i = 0; i < pSeq->sz; i++)
	{
		printf("%d", pSeq->data[i]);
		printf("\n");
	}
}
void PushFront(pSeqList pSeq, DataType d)
{
	assert(pSeq != NULL);
	if	(pSeq == MAX)
	{

		printf("顺序表已满,无法插入\n");
		return;
}
	int i = 0;
	for (i = pSeq->sz-1; i >= 0; i--)
	{
		pSeq->data[i + 1] = pSeq->data[i];
	}
	pSeq->data[pSeq->sz] = d;
	pSeq->sz++;
}
void PopFront(pSeqList pSeq)
{
	assert(pSeq != NULL);
	if (pSeq == NULL)
	{
		printf("顺序表为空,无法删除\n");
		return;
	}
	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 d)
{
	int i= 0; 
	assert(pSeq != NULL);
	for (i = 0; i < pSeq->sz - 1; i++)
	{
		if (pSeq->data[i] == d)
		{
			printf("[%d]的下标是%d\n", d, i);
		}
	}
	printf("找不到\n");
}
void Insert(pSeqList pSeq, int pos, DataType d)
{
	assert(pSeq != NULL);
	assert(pos >= 0 && pos <= sz);
	if (pSeq->sz == MAX)
	{
		printf("无法从指定位置插入\n");
		return;
	}
	int i = 0;
	for (i = pSeq->sz - 1; i >= pos; i--)
	{
		pSeq->data[i + 1] = pSeq->data[i];
		pSea->data[i] = d;
	}
	pSeq->sz++;
}
void Erase(pSeqList pSeq, int pos)
{
	assert(pSeq != NULL);
	assert(pos >= 0 && pos <= sz);
	if (pSeq->sz == 0)
	{
		printf("顺序表为空,无法删除\n");
		return;
	}
	int i = 0;
	for (i = pos; i <= pSeq->sz - 1; i++)
	{
		pSeq->data[i] = pSeq->data[i + 1];

	}
	pSeq->sz - ;
}
void Remove(PSeqList pSeq, DataType )
{
	int i = 0;
	assert(pSeq != NULL);
	if (pSeq->sz == 0)
	{
		printf("顺序表为空,无法删除\n");
		return;
	}
	for (i = 0; i < pSeq->sz; i++)
	{
		if (pSeq->data[i] == d)
		{
			break;
		}
	}
	if (i == pSeq->sz)
	{
		printf("删除元素不存在\n");
		return;
	}
	for (; i < pSeq->sz - 1; i++)//删除
	{
		pSeq->data[i] = pSeq->data[i + 1];
	}
	pSeq->sz--;
}
void RemoveAll(pSeqList pSeq, DataType d)
{
	assert(pSeq != NULL);
	if (pSeq->sz == 0)
	{
		printf("顺序表为空,无法删除\n");
		return;
	}
	int i = 0;
	for (i = 0; i < pSeq->sz; i++)
	{
		if (pSeq->data[i] == d)
		{
			int j = 0;
			for (j = i; j < pSeq->sz - 1; j++)
			{
				pSeq->data[j] = pSeq->data[j + 1];
			}
		}
	}
	pSeq->sz--;
	--i;
}
int Size(pSeqList pSeq)
{
	assert(pSeq != NULL);
	return pSeq->sz;
}
int Empty(pSeqList pSeq)
{
		//空指针判断
		if (pSeq == NULL)
		{
			printf("Seqlist error!\n");
			return;
		}
		if (pSeq->sz == 0)
			return 1;
		else
			return 0;
	
}
void swap(DataType *p1, DataType p2)
{
	DataType tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}
void BubbleSort(pSeqList pSeq)
{
	int i = 0;
	int j = 0;
	int flag = 0;
	assert(pSeq1 = NULL);
	for (i = 0; i < pSeq - sz - 1; i++)
	{
		for (j = 0; j < pSeq->sz - 1 - i; j++)
		{
			swap(pSeq->data[j], pSeq->data[j + 1]);
			flag = 1;
		}
	}
	if (flag == 0)
	{
		return;
	}
}
void SelectSort(pSeqList pSeq)
{
	int i = 0;
	int j = 0;
	assert(pSeq != NULL);
	for (i = 0; i < pSeq->sz - 1; i++)
	{
		int max = 0;
		for (j = 1; j < pSeq->sz - 1 - i; j++)
		{
			if (pSeq->data[max] < pSeq->data[j])
			{
				max = j;
			}
		}
		if (max != pSeq->sz - 1 - i)
		{
			swap(pSeq->data[max], pSeq->data[sz - 1 - i]);
		}
	}
}
void SelectSortOP(PSeqList pSeq)
{
	int start = 0;
	int end = pSeq->sz - 1;
	assert(pSeq != NULL);
	while (start < end)
	{
		int max = start;
		int min = start;
		for (int i = start; i <= end; i++)
		{
			if (pSeq->data[i]>pSeq->data[max])
			{
				max = i;
			}
			if (pSeq->data[i] < pSeq->data[min])
			{
				min = i;
			}
			if (min != start)
			{
				swap(pSeq->data[start], pSeq->data[min]);
			}
			if (max = min)
			{
				max = min;
			}
			if (max != end)
			{
				swap(pSeq->data[max], pSeq->data[end]);
			}
			start++;
			end--;
		}
		
	}
}
int BinarySearch(PSeqList pSeq, DataType d)
{
	int left = 0;
	int right = pSeq->sz-1;
	while (left <= right)
	{
		int mid = left + (left - right) / 2;
		if (pSeq->data[mid] > d)
		{
			right = mid - 1;
		}
		else if (pSeq->data[mid) < d)
		{
			left = mid + 1;

		}
		else
		{
			return mid;
		}
	}
	printf("找不到\n");
}
int BinarySearch_R(pSeqList pSeq, int left, int right, DataType d)
{
	int mid = 0;
	if (left > right)
		printf("找不到\n");
	mid = left + (right + left) / 2;
	if (pSeq->data[mid] > d)
	{
		return BinarySearch_R(pSeq, left, mid - 1, d);
	}
	else if (pSeq->data[mid] < d)
	{
		return BinarySearch_R(pSeq, mid + 1, right, d);

	}
	else
	{
		return mid;
	}
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"seqlist.h"

void TestPushFront()//头插
{
	InitSeqList(&seq);
	
	PushFront(&seq, 5);
	PushFront(&seq, 4);
	PushFront(&seq, 3);
	PushFront(&seq, 2);
	PushFront(&seq, 1);
	PrintSeqList(&seq);
}

void TestPopFront()//头删
{
	
	InitSeqList(&seq);


	//先插入五个

	PushFront(&seq, 5);
	PushFront(&seq, 4);
	PushFront(&seq, 3);
	PushFront(&seq, 2);
	PushFront(&seq, 1);
	PrintSeqList(&seq);

	PopFront(&seq);
	PopFront(&seq);
	PrintSeqList(&seq);
}
void TestPushBack(Seqlist* seqlist)//尾部删除
{

	InitSeqList(&seq);

	//先插入五个
	PushFront(&seq, 5);
	PushFront(&seq, 4);
	PushFront(&seq, 3);
	PushFront(&seq, 2);
	PushFront(&seq, 1);
	PrintSeqList(&seq);

	//尾插
	PushBack(&seq, 8);
	PushBack(&seq, 7);
	PushBack(&seq, 5);
	PushBack(&seq, 1);
	PushBack(&seq, 6);
	PrintSeqList(&seq);
}
//尾删测试
void TestPopBack()//尾删测试
{
	PopBack(&seq);
	PopBack(&seq);
	PopBack(&seq);
	PopBack(&seq);
	PopBack(&seq);
	PrintSeqList(&seq);
}




void TestInsert()//Insert测试
{

	int pos = 0;
	Typedata d = 0;

	printf("input pos:>");
	scanf("%d", &pos);
	printf("input d:>");
	scanf("%c", &d);
	Insert(seq, pos, d);
	PrintSeqList(&seq);
}
/ 查找指定元素下标测试
void TestFind(Seqlist* seqlist)// 查找指定元素下标测试
{
	
	Typedata d;
	printf("input d:>");
	scanf("%c", &d);
	int ret = Find(&seq, d);
	printf("pos is %d\n", ret);
}
//Erase测试
void TestErase()//Erase测试
{
	
	int pos = 0;
	printf("input pos:>");
	scanf("%d", &pos);
	Erase(&seq, pos);
	PrintSeqList(&seq);
}
void TestRemove()//删除表中指定元素测试
{
	
	Typedata d;
	printf("input d:>");
	scanf("%c", &d);
	SeqlistRemove(&seq, d);
	PrintSeqList(&seq);
}
void TestRemoveAll(Seqlist* seqlist)//删除表中所有指定元素测试
{
	
	Typedata d;
	printf("input d:>");
	scanf("%c", &d);
	SeqlistRemoveAll(&seq, d);
	PrintSeqList(&seq);
}


void TestSize(Seqlist* seqlist)//获取顺序表个数测试
{
	
	int ret = Size(&seq);
	printf(" size is %d\n", ret);
}

void TestEmpty(Seqlist* seqlist)//empty测试
{
	
	int ret = Empty(&seq);
	if (ret == 1)
		printf("Seqlist is empty!\n");
	if (ret == 0)
		printf("Seqlist is not umpty!\n");
}

void TestBubbleSort(Seqlist* seqlist)//冒泡排序测试
{
	
	BubbleSort(&seq);
	PrintSeqList(&seq);
}

void TestSelect(Seqlist* seqlist)//选择排序测试
{
	
	SelectSort(&seq);
	PrintSeqList(&seq);
}

int main()
{
	SeqList seq;
	TestPushFront()
	
		//  TestPushfront();
		//  TestPopback();
		//  TestPushback();
		//  TestErase();
		//  TestInsert();
		//  TestFind);
		//  TestRemove();
		//  TestRemoveAll();
		//  TestDestroy();
		//  TestEmpty();
		//  TestSize();
		//  TestGet();
		//  TestSet();
		//  TestBubbleSort();
		//  TestBubbleSortEx();
		TestSelect();
	return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值