【C語言】靜態順序表


要實現的接口如下:

#define _CRT_SECURE_NO_DEPRECATE
#ifndef _SeqList__H__
#define _SeqList__H__

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

#define MAX 100

typedef int DataType;
typedef struct SeqList
{
	DataType arr[MAX];
	int count;
}SeqList, *pSeqList;

void InitSeqList(pSeqList p);//初始化
void PushBack(pSeqList p, DataType d);//後插
void PopBack(pSeqList p);//後刪
void PushFront(pSeqList p, DataType d);//前插
void PopFront(pSeqList p);//前刪
int  Find(pSeqList p, DataType d);//查找
void Remove(pSeqList p, DataType d);//移除一個結點
void RemoveAll(pSeqList p, DataType d);//移除多個相同的結點
void Show(pSeqList p);//打印
void Sqrt(pSeqList p);//排序
int  BinarySearch(pSeqList p, DataType d);//二分法查找

#endif


具體實現:

#define _CRT_SECURE_NO_DEPRECATE
#include "SeqList.h"

void InitSeqList(pSeqList p)
{
	p->count = 0;
	memset(p->arr, 0, MAX*sizeof(DataType));
}

void PushBack(pSeqList p, DataType d)
{
	assert(p != NULL);

	if (p->count == MAX)
	{
		printf("表已滿");
		return;
	}
	p->arr[p->count] = d;
	p->count++;
	printf("插入成功\n");
}

void PopBack(pSeqList p)
{
	assert(p);

	int i = 0;

	if (0 == p->count)
	{
		printf("表爲空\n");
		return;
	}
	p->count--;
	printf("刪除成功\n");
}

void PushFront(pSeqList p, DataType d)
{
	assert(p);

	int i = 0;

	if (p->count == MAX)
	{
		printf("表已滿");
		return;
	}

	for (i = p->count; i > 0; i--)
	{
		p->arr[i] = p->arr[i - 1];
	}
	p->arr[0] = d;
	p->count++;
	printf("插入成功\n");
}

void PopFront(pSeqList p)
{
	assert(p);
	int i = 0;

	if (0 == p->count)
	{
		printf("順序表爲空\n");
		return;
	}

	for (i = 0; i < p->count - 1; i++)
	{
		p->arr[i] = p->arr[i + 1];
	}
	p->count--;
	printf("刪除成功\n");
}

int Find(pSeqList p, DataType d)
{
	assert(p);
	int i = 0;

	for (i = 0; i < p->count; i++)
	{
		if (d == p->arr[i])
		{
			return i+1;
		}
	}
	return -1;
}

void Remove(pSeqList p, DataType d)
{
	assert(p);
	DataType ret = Find(p, d);
	int i = 0;

	if (ret == -1)
	{
		printf("該列表中不存在該數\n");
	}
	else
	{
		for (i = ret; i < p->count - 1; i++)
		{
			p->arr[i] = p->arr[i + 1];
		}
		p->count--;
		printf("刪除成功\n");
	}
}

void RemoveAll(pSeqList p, DataType d)
{
	assert(p);
	int i = 0;
	int j = 0;
	int count = 0;

	if (0 == p->count)
	{
		printf("順序表爲空\n");
		return;
	}

	while (i<p->count)
	{
		if (d == p->arr[i])
		{
			j = i;
			while (j<p->count)
			{
				p->arr[j] = p->arr[j + 1];
				j++;
			}
			p->count--;
		}
		i++;
	}
	printf("刪除成功\n");
}

void Show(pSeqList p)
{
	assert(p);

	int i = 0;

	if (0 == p->count)
	{
		printf("順序表爲空\n");
		return;
	}
	for (i = 0; i < p->count; i++)
	{
		printf("%d ", p->arr[i]);
	}
	printf("\n");
}

void Sqrt(pSeqList p)
{
	assert(p);

	int i = 0;
	int j = 0;

	for (i = 0; i < p->count; i++)
	{
		for (j = i + 1; j < p->count; j++)
		{
			if (p->arr[i]>p->arr[j])
			{
				DataType tmp = p->arr[i];
				p->arr[i] = p->arr[j];
				p->arr[j] = tmp;
			}
		}
	}
	printf("排序成功\n");
}

int BinarySearch(pSeqList p, DataType d)
{
	assert(p);

	int left = 0;
	int right = p->count - 1;

	while (left <= right)
	{
		int mid = left - ((left - right) >> 1);
		if (d < p->arr[mid])
		{
			right = mid - 1;
		}
		else if (d>p->arr[mid])
		{
			left = mid + 1;
		}
		else
		{
			return mid + 1;
		}
	}
	return -1;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章