静态顺序表的实现

Seqlish.h:

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

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

#define MAX 10

typedef int DataType;

typedef struct SeqList
{
	DataType data[MAX];
	int sz;
}SeqList, *pSeqList;

void InitSeqList(pSeqList ps);//初始化
void PushBack(pSeqList ps, DataType d);//后加
void PopBack(pSeqList ps);//后删除
void Display(const pSeqList ps);//打印
void PushFront(pSeqList ps, DataType d);//前加
void PopFront(pSeqList ps);//前删除
int Find(pSeqList ps, DataType d);//查找
void Insert(pSeqList ps, DataType d, int pos);//指定添加
void Remove(pSeqList ps, DataType d);//删除指定数
void RemoveAll(pSeqList ps, DataType d);//删除所有的指定数
void Reverse(pSeqList ps);//逆序
void Sort(pSeqList ps);//排序
int BinarySearch(pSeqList ps, DataType d);//二分查找
#endif //__SEQLIST_H__
SeqList.c:

#define _CRT_SECURE_NO_WARNINGS 1
#include"Seqlist.h"
void InitSeqList(pSeqList ps)
{
	assert(ps != NULL);
	ps->sz = 0;
	memset(ps->data, 0, sizeof(DataType)*MAX);
}
void PushBack(pSeqList ps, DataType d)//后加
{
	assert(ps != NULL);
	if (ps->sz == MAX)
	{
		return;
	}
	ps->data[ps->sz] = d;
	ps->sz++;
}
void PopBack(pSeqList ps)//后删除
{
	assert(ps != NULL);
	if (ps->sz == 0)
	{
		return;
	}
	ps->sz--;
}
void Display(const pSeqList ps)//打印
{
	int i = 0;
	assert(ps != NULL);
	if (ps->sz == 0)
	{
		return;
	}
	for (i = 0; i < ps->sz; i++)
	{
		printf("%d ", ps->data[i]);
	}
	printf("\n");
}
void PushFront(pSeqList ps, DataType d)//前加
{
	int i = 0;
	assert(ps != NULL);
	if (ps->sz == MAX)
	{
		return;
	}
	for (i = ps->sz-1; i >= 0; i--)
	{
		ps->data[i + 1] = ps->data[i];
	}
	ps->data[0] = d;
	ps->sz++;
}
void PopFront(pSeqList ps)//前删除
{
	int i = 0;
	assert(ps != NULL);
	if (ps->sz == 0)
	{
		return;
	}
	for (i = 1; i < ps->sz; i++)
	{
		ps->data[i - 1] = ps->data[i];
	}
	ps->sz--;
}
int Find(pSeqList ps, DataType d)//查找
{
	int i = 0;
	assert(ps != NULL);
	if (ps->sz == 0)
	{
		return -1;
	}
	for (i = 0; i < ps->sz; i++)
	{
		if (ps->data[i] == d)
		{
			return i;
		}
	}
	return -1;
}
void Insert(pSeqList ps, DataType d, int pos)//中间添加
{
	int i = 0;
	assert(ps != NULL);
	if (ps->sz == MAX)
	{
		return;
	}
	for (i = ps->sz; i > pos; i--)
	{
		ps->data[i] = ps->data[i - 1];
	}
	ps->data[pos] = d;
	ps->sz++;
}
void Remove(pSeqList ps, DataType d)//删除指定数
{
	int ret = 0;
	int i = 0;
	assert(ps != NULL);
	if (ps->sz == 0)
	{
		return;
	}
	ret = Find((pSeqList)ps->data, d);
	if (ret == -1)
	{
		printf("没找到指定的数\n");
	}
	else
	{
		for (i = ret; i < ps->sz; i++)
		{
			ps->data[i] = ps->data[i + 1];
		}
		ps->sz--;
	}
}
void RemoveAll(pSeqList ps, DataType d)//删除所有的指定数
{
	int i = 0;
	int j = 0;
	assert(ps != NULL);
	if (ps->sz == 0)
	{
		return;
	}
	for (j = 0; j < ps->sz; j++)
	{
		if (d == ps->data[j])
		{
			for (i = j; i < ps->sz; i++)
			{
				ps->data[i] = ps->data[i + 1];
			}
			ps->sz--;
		}
	}
}
void Reverse(pSeqList ps)//逆序
{
	int left = 0;
	int right = ps->sz - 1;
	assert(ps != NULL);
	if (ps->sz == 0)
	{
		return;
	}
	while (left < right)
	{
		int tmp = ps->data[left];
		ps->data[left] = ps->data[right];
		ps->data[right] = tmp;
		left++;
		right--;
	}
}
void Sort(pSeqList ps)//排序
{
	int i = 0;
	int j = 0;
	assert(ps != NULL);
	if (ps->sz == 0)
	{
		return;
	}
	for (i = 0; i < ps->sz - 1; i++)
	{
		for (j = 0; j < ps->sz - i - 1; j++)
		{
			if (ps->data[j]>ps->data[j + 1])
			{
				int tmp = ps->data[j];
				ps->data[j] = ps->data[j + 1];
				ps->data[j + 1] = tmp;
			}
		}
	}
}
int BinarySearch(pSeqList ps, DataType d)//二分查找
{
	int left = 0;
	int right = ps->sz - 1;
	int mid = 0;
	assert(ps != NULL);
	if (ps->sz == 0)
	{
		return -1;
	}
	while (left <= right)
	{
		mid = left + (right - left) / 2;
		if (ps->data[mid] > d)
		{
			right = mid - 1;
		}
		else if (ps->data[mid] < d)
		{
			left = mid + 1;
		}
		else
		{
			return mid;
		}
	}
	return -1;
}
test.c:
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void test1()
{
	struct SeqList plist;
	InitSeqList(&plist);
	PushBack(&plist, 1);
	PushBack(&plist, 2);
	PushBack(&plist, 3);
	PushBack(&plist, 4);
	PushBack(&plist, 5);
	Display(&plist);
	PopBack(&plist);
	Display(&plist);
	PopBack(&plist);
	Display(&plist); 
	PopBack(&plist);
	Display(&plist);
}
void test2()
{
	struct SeqList plist;
	InitSeqList(&plist);
	PushFront(&plist, 1);
	PushFront(&plist, 2);
	PushFront(&plist, 3);
	PushFront(&plist, 4);
	PushFront(&plist, 5);
	Display(&plist);
	PopFront(&plist);
	Display(&plist);
	PopFront(&plist);
	Display(&plist);
	PopFront(&plist);
	Display(&plist);
}
void test3()
{
	int pos = 0;
	struct SeqList plist;
	InitSeqList(&plist);
	PushBack(&plist, 1);
	PushBack(&plist, 2);
	PushBack(&plist, 3);
	PushBack(&plist, 4);
	PushBack(&plist, 5);
	Display(&plist);
	pos = Find(&plist, 3);
	printf("pos = %d\n", pos);
	Insert(&plist, 3, pos);
	Display(&plist);
}
void test4()
{
	struct SeqList plist;
	InitSeqList(&plist);
	PushBack(&plist, 1);
	PushBack(&plist, 2);
	PushBack(&plist, 3);
	PushBack(&plist, 4);
	PushBack(&plist, 5);
	Display(&plist);
	Remove(&plist, 3);
	Display(&plist);
	PushBack(&plist, 3);
	PushBack(&plist, 4);
	PushBack(&plist, 5);
	Display(&plist);
	RemoveAll(&plist, 4);
	Display(&plist);
}
void test5()
{
	struct SeqList plist;
	InitSeqList(&plist);
	PushBack(&plist, 1);
	PushBack(&plist, 2);
	PushBack(&plist, 3);
	PushBack(&plist, 4);
	PushBack(&plist, 5);
	Display(&plist);
	Reverse(&plist);
	Display(&plist);
	Sort(&plist);
	Display(&plist);
}
void test6()
{
	struct SeqList plist;
	InitSeqList(&plist);
	PushBack(&plist, 1);
	PushBack(&plist, 2);
	PushBack(&plist, 3);
	PushBack(&plist, 4);
	PushBack(&plist, 5);
	int ret = BinarySearch(&plist, 5);
	printf("ret = %d\n", ret);
}
int main()
{
	printf("测试1:\n");
	test1();
	printf("\n");
	printf("\n");
	printf("\n");
	printf("测试2:\n");
	test2();
	printf("\n");
	printf("\n");
	printf("\n");
	printf("测试3:\n");
	test3();
	printf("\n");
	printf("\n");
	printf("\n");
	printf("测试4:\n");
	test4();
	printf("\n");
	printf("\n");
	printf("\n");
	printf("测试5:\n");
	test5();
	printf("\n");
	printf("\n");
	printf("\n");
	printf("测试6:\n");
	test6();
	system("pause");
	return 0;
}

下面是运行结果:


以上就是静态顺序表的实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值