用c语言实现顺序表

用c语言实现顺序表

Seqlist.h

#pragma once
typedef int DataType;
#define N 100
typedef struct Seqlist
{
	DataType array[N];
	size_t size;
}Seqlist;

void InitSeqlist(Seqlist* s); //初始化
void PrintSeqlist(Seqlist* s); //打印
void PushBack(Seqlist* s, DataType x); //尾插
void PopBack(Seqlist* s); //尾删
void PushFront(Seqlist* s, DataType x); //头插
void PopFront(Seqlist* s); //头删
void Insert(Seqlist* s, size_t pos, DataType x); //将x插入任意位置
int Find(Seqlist* s, DataType x); //查找元素
void Erase(Seqlist* s, size_t pos); //删除任意位置元素
void Modify(Seqlist* s, size_t pos, DataType x); //替换,将pos位置值修改为x
int Remove(Seqlist* s, DataType x); //移除某值的第一项
int RemoveAll(Seqlist* s, DataType x); //移除某值得全部项
void Swap(DataType* left, DataType* right); //交换两元素
void BubbleSort(Seqlist* s); //冒泡排序
void SelectSort(Seqlist* s); //选择排序
void SelectSort_OP(Seqlist* s); //选择排序的优化
int BinarySearch(Seqlist* s, DataType x); //二分查找

void InitSeqlist(Seqlist* s) //初始化
{
	memset(s->array, 0, sizeof(DataType)*N);
	s->size = 0;
}

void PrintSeqlist(Seqlist* s) //打印
{
	for (size_t i = 0; i < s->size; i++)
	{
		printf("%d ", s->array[i]);
	}
	printf("\n");
}

void PushBack(Seqlist* s, DataType x) //尾插
{
	assert(s);
	if (s->size == N)
	{
		printf("Seqlist is full \n");
		return;
	}
	s->array[s->size++] = x;
}

void PopBack(Seqlist* s) //尾删
{
	assert(s);
	if (s->size == 0)
	{
		printf("Seqlist is empty \n");
		return;
	}
	s->array[s->size - 1] = 0;
	s->size--;
}

void PushFront(Seqlist* s, DataType x) //头插
{
	assert(s);
	int end = s->size - 1;
	if(s->size == N)
	{
		printf("Seqlist is full \n");
			return;
	}
	for (; end >= 0; --end)
	{
		s->array[end + 1] = s->array[end];
	}
	s->array[0] = x;
	s->size++;
}

void PopFront(Seqlist* s) //头删
{
	assert(s);
	if (s->size == 0)
	{
		printf("Seqlist is empty \n");
		return;
	}
	for (size_t i = 1; i < s->size; ++i)
	{
		s->array[i-1] = s->array[i];
	}
	s->size--;
}

void Insert(Seqlist* s, size_t pos, DataType x) //将x插入任意位置
{
	assert(s);
	assert(pos <= s->size);
	if (s->size == N)
	{
		printf("Seqlist is full \n");
		return ;
	}
	size_t end = s->size - 1;
	for (; end >= pos; --end)
	{
		s->array[end + 1] = s->array[end];
	}
	s->array[pos] = x;
	s->size++;
}

int Find(Seqlist* s, DataType x) //查找元素
{
	size_t i = 0;
	assert(s);
	for (; i <= s->size; ++i)
	{
		if (s->array[i] == x)
		{
			return i;
		}
	}
	return -1;
}

void Erase(Seqlist* s, size_t pos) //删除任意位置元素
{
	assert(s);
	assert(pos < s->size);
	size_t i = 0;
	for (i = pos; i < s->size -1; ++i)
	{
		s->array[i] = s->array[i + 1];
	}
	s->size--;
}

void Modify(Seqlist* s, size_t pos, DataType x) //替换,将pos位置值修改为x
{
	assert(s);
	assert(pos < s->size);
	s->array[pos] = x;
}

int Remove(Seqlist* s, DataType x) //移除某值的第一项
{
	assert(&s);
	size_t pos;
	pos = Find(s, x);
	if (pos != -1)
	{
		Erase(s, pos);
		return 0;
	}
	else
	{
		return -1;
	}
}

int RemoveAll(Seqlist* s, DataType x) //移除某值得全部项
{
	size_t i=0, j = 0;
	assert(s);
	while (i < s->size)
	{
		if (s->array[i] != x)
		{
			s->array[j] = s->array[i];
			++j;
		}
		++i;
	}
	s->size = j;
	return i == j ? -1 : 0;
}

void Swap(DataType* left, DataType* right) //交换两元素
{
	DataType tmp = *left;
	*left = *right;
	*right = tmp;
}

void BubbleSort(Seqlist* s) //冒泡排序  处理有相同元素的数组时出错
{
	assert(s);
	size_t start = 0;
	size_t end;
	int exchange = 0;
	for (end = s->size; end > 0; --end)
	{
		while (start + 1 < s->size)
		{
			if (s->array[start] > s->array[start + 1])
			{
				Swap(s->array + start, s->array + start + 1);
				exchange = 1;
			}
			++start;
		}
	}
}

void SelectSort(Seqlist* s) //选择排序
{
	size_t start = 0;
	assert(s);
	for (; start < s->size; ++start)
	{
		size_t i = start;
		size_t min = start;
		for (; i < s->size; ++i)
		{
			if (s->array[min]>s->array[i])
			{
				min = i;
			}
		}
		Swap(s->array + start, s->array + min);
	}
}

void SelectSort_OP(Seqlist* s) //选择排序的优化
{
	size_t start = 0, finish = s->size - 1;
	assert(s);
	while (start < finish)
	{
		size_t i = start;
		size_t min = start, max = start;
		while (i <= finish)
		{
			if (s->array[i] < s->array[min])
			{
				min = i;
			}
			if (s->array[i]>s->array[max])
			{
				max = i;
			}
			++i;
		}
		Swap(s->array + start, s->array + min);
		if (start == max)
		{
			max = min;
		}
		Swap(s->array + finish, s->array + max);
		start++;
		finish--;
	}
}

int BinarySearch(Seqlist* s, DataType x) //二分查找
{
	int left = 0, right = s->size - 1;
	while (left <= right)
	{
		int mid = left + (right - left) / 2;
		if (s->array[mid] < x)
		{
			left = mid + 1;
		}
		else if (s->array[mid]>x)
		{
			right = mid - 1;
		}
		else
		{
			return mid;
		}
	}
	return -1;
}

TestSeqlist1()
{
	Seqlist s;
	InitSeqlist(&s);
	PushBack(&s, 0);
	PushBack(&s, 2);
	PushBack(&s, 4);
	PushBack(&s, 6);
	PushFront(&s, 10);
	PushFront(&s, 11);
	PushFront(&s, 12);
	PushFront(&s, 13);
	PrintSeqlist(&s);
	//PopBack(&s);
	PopFront(&s);
	PopFront(&s);
	PrintSeqlist(&s);
	Insert(&s, 3, 1);
	PrintSeqlist(&s);
}

TestSeqlist2()
{
	Seqlist s;
	InitSeqlist(&s);
	PushBack(&s, 0);
	PushBack(&s, 2);
	PushBack(&s, 4);
	PushBack(&s, 6);
	PushFront(&s, 10);
	PushFront(&s, 11);
	PushFront(&s, 12);
	PushFront(&s, 13);
	PrintSeqlist(&s);
	printf("%d\n", Find(&s, 0));
	Erase(&s, Find(&s, 0));
	PrintSeqlist(&s);
	Modify(&s, 3, 0);
	PrintSeqlist(&s);
}

TestSeqlist3()
{
	Seqlist s;
	InitSeqlist(&s);
	PushBack(&s, 1);
	PushBack(&s, 2);
	PushBack(&s, 4);
	PushBack(&s, 6);
	PushFront(&s, 0);
	PushFront(&s, 1);
	PushFront(&s, 2);
	PushFront(&s, 3);
	PrintSeqlist(&s);
	//Remove(&s, 2);
	//PrintSeqlist(&s);
	RemoveAll(&s, 2);
	RemoveAll(&s, 1);
    PrintSeqlist(&s);
	BubbleSort(&s);
	PrintSeqlist(&s);
}

TestSeqlist4()
{
	Seqlist s;
	InitSeqlist(&s);
	PushBack(&s, 0);
	PushBack(&s, 2);
	PushBack(&s, 4);
	PushBack(&s, 6);
	PushFront(&s, 10);
	PushFront(&s, 11);
	PushFront(&s, 12);
	PushFront(&s, 13);
	PrintSeqlist(&s);
	/*SelectSort(&s);
	PrintSeqlist(&s);*/
	SelectSort_OP(&s);
	PrintSeqlist(&s);
	printf("6的下标是:%d\n",BinarySearch(&s, 6));
}



test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include 
#include 
#include 
#include 
#include "Seqlist.h"

int main()
{
	TestSeqlist4();
	system("pause");
	return 0;
}

你可能感兴趣的:(C语言)