动态表的基本实现

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


typedef int Datatype;
typedef struct SeqList
{
	Datatype* arry;
	size_t size;
	size_t capacity;

}SeqList;
void InitSeqList(SeqList* seq);
void DestorySeqList(SeqList* seq);
void CheckCapacity(SeqList *seq);



void PushBack(SeqList* seq,Datatype x);
void PopBack(SeqList* seq);//尾插尾删

void PushFront(SeqList* seq, Datatype x);
void PopFront(SeqList* seq, Datatype x);//头插头删

void Insert(SeqList* seq,size_t pos, Datatype x);//指定位置插入
void Delete(SeqList* seq,size_t pos);//指定位置删除
void Erase(SeqList* seq, Datatype x);//删除数据
void RemoveAll(SeqList* seq, Datatype x);//删除全部指定元素
int Search(SeqList* seq, Datatype x);//查找元素
void BubbleSort(SeqList* seq);//冒泡排序
void SelectSort(SeqList* seq);//选择排序
void InsertSort(SeqList* seq);//插入排序
int BinarySeach(SeqList* seq, Datatype x);//二分查找

#include"seq.h"

void InitSeqList(SeqList* seq)
{
	assert(seq);
	seq->arry = (Datatype*)malloc(3 * sizeof(Datatype));
	assert(seq->arry);
	seq->size = 0;
	seq->capacity = 3;

}
void Destory(SeqList* seq)
{
	assert(seq);
	if (seq->arry)
	{
		free(seq->arry);
	}
	seq->arry = NULL;
	seq->capacity = 0;
	seq->size = 0;
}
void CheckCapacity(SeqList *seq)
{
	Datatype* temp;
	if (seq->size >= seq->capacity)
	{

		//	Datatype* temp = (Datatype*)malloc(sizeof(sizeof(Datatype)*seq->capacity * 2));
		//assert(temp);
		开辟更大空间

		//memcpy(temp, seq->arry, sizeof(Datatype)*seq->size);
		拷贝数据

		//free(seq->arry);
		//seq->arry = temp;
		//seq->capacity *= 2;
		释放旧空间
		seq->arry = (Datatype*)realloc(seq->arry, sizeof(Datatype)*seq->capacity * 2);
		assert(seq->arry);
		seq->capacity *= 2;

	}
}
void PushBack(SeqList* seq, Datatype x)
{
	assert(seq);
	CheckCapacity(seq);
	seq->arry[seq->size++]=x;

}
void PopBack(SeqList* seq)
{
	assert(seq);
	CheckCapacity(seq);
	if (seq->size <= 0)
	{
		printf("The List is Empty\n");
	}

	seq->size--;

}


void PushFront(SeqList* seq, Datatype x)
{
	assert(seq);
	CheckCapacity(seq);
	int end = seq->size - 1;
	while (end >= 0)
	{
		seq->arry[end] = seq->arry[end - 1];
		end--;
	}
	seq->size++;
	seq->arry[0] = x;
}

void PopFront(SeqList* seq)
{
	assert(seq);
	CheckCapacity(seq);
	if (seq->size <= 0)
	{
		printf("The List is Empty\n");
	}
	for (size_t i = 0; i < seq->size; i++)
	{
		seq->arry[i] = seq->arry[i + 1];
	}
	seq->size--;
}

void Insert(SeqList* seq,size_t pos,Datatype x)
{
	assert(seq);
	assert(pos>0);
	assert(pos <= seq->size );
	CheckCapacity(seq);
	int end = seq->size;
	while (end >= pos)
	{
		seq->arry[end] = seq->arry[end - 1];
		end--;
	}
	seq->size++;
	seq->arry[pos - 1] = x;
	
}
void Delete(SeqList* seq, size_t pos)//指定位置删除
{
	assert(seq);
	assert(pos>0);
	assert(pos <= seq->size);
	CheckCapacity(seq);
	size_t i =0;
	if (seq->size <= 0)
	{
		printf("The List is Empty\n");

	}
	for (i = pos-1; i < seq->size-1;i++)
	{
		seq->arry[i] = seq->arry[i + 1];
		
	}
	seq->size--;
}
int Search(SeqList* seq, Datatype x)
{
	assert(seq);
	
	if (seq->size <= 0)
	{
		printf("The List is Empty\n");
	}
	else
	{
	for (int i = 0; i < seq->size; i++)
	{
			if (seq->arry[i] == x)
			{
			return i+1;
			}
	}
	return -1;	
	}
	
}
void Erase(SeqList* seq, Datatype x)
{
	assert(seq);
	
	if (seq->size <= 0)
	{
		printf("The List is Empty\n");
	}
	else
	{
		size_t i = Search(seq, x);
		if (i != -1)
		{
			Delete(seq, i);
		}
		else
		{
			printf("The x is no there\n");
		}
	}
	/*{

		for (int i = 0; i < seq->size; i++)
		{
			if (seq->arry[i] == x)
			{
				return i+1;
			}
		}
	     return -1;
	}*/
}
void RemoveAll(SeqList* seq, Datatype x)
{
	assert(seq);
	if (seq->size <= 0)
	{
		printf("The List is Empty\n");
	}
	int i = 0;
	int j = 0;
	int count = 0;
	for (i = 0; j < seq->size; i++)
	{
		if (seq->arry[i] == x)
		{
			count++;
		}
		else
		{
			seq->arry[j] = seq->arry[i];
			j++;
		}
	}
	seq->size -= count;
}
int Swap(int *a, int *b)
{
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}
void BubbleSort(SeqList* seq)
{
	assert(seq);
	if (seq->size <= 0)
	{
		printf("The List is Empty\n");
		return;
	}
	size_t i = 0;
	size_t j = 0;
	for (i = 0; i < seq->size; i++)
	{
		for (j = i + 1; j < seq->size; j++)
		{
			if (seq->arry[i]>seq->arry[j])
				Swap((&seq->arry[i]), &(seq->arry[j]));
		}

	}
}
void SelectSort(SeqList* seq)
{
	assert(seq);
	if (seq->size <= 0)
	{
		printf("The List is Empty\n");
	}
	int Start = 0;
	int End = seq->size - 1;
	while (Start <= End)
	{
		int max = End;
		int min = Start;
		for (size_t i = 0; i < seq->size; i++)
		{
			if (seq->arry[i] < seq->arry[min])
				min = i;
			if (seq->arry[i]>seq->arry[max])
				max = i;
		}
		Swap(&(seq->arry[Start]), &(seq->arry[min]));
		if (max == Start)
		{
			max = min;
		}
		Swap(&(seq->arry[End]), &(seq->arry[max]));
		Start++;
		End--;



	}


}
void InsertSort(SeqList* seq)
{
	assert(seq);
	if (seq->size <= 0)
	{
		printf("The List is Empty\n");
	}
	for (size_t i = 0; i < seq->size - 1; i++)
	{
		int end = i;
		int temp = seq->arry[end + 1];
		while (end >= 0 && seq->arry[end]>temp)
		{
			seq->arry[end + 1] = seq->arry[end];
			end--;
		}
		seq->arry[end + 1] = temp;
	}
}
int BinarySeach(SeqList* seq, Datatype x)
{
	assert(seq);
	if (seq->size <= 0)
	{
		printf("The List is Empty\n");
	}
	int start = 0;
	int end = seq->size - 1;
	while (start <= end)
	{
		int mid = start + (end - start) / 2;
		if (seq->arry[mid] < x)
		{
			start = mid + 1;
		}
		else if (seq->arry[mid]>x)
		{
			end = mid - 1;
		}
		else
			return mid + 1;
	}
	return -1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值