动态顺序表

#include"test.h"
#include<assert.h>


void InitSeqList(SeqList* pSeq)   //初始化
{
	assert(pSeq);
	pSeq->size = 0;
	pSeq->capacity = 2;
	pSeq->Seq = (Datatype*)malloc(sizeof(Datatype)*(pSeq->capacity));
	if(NULL == pSeq->Seq)
	{
		printf("申请空间失败!\n");
		exit(1);
	}
 }

 void PushBack(SeqList* pSeq,Datatype data)   //尾插
 {
	 assert(pSeq);
	 if(pSeq->size == pSeq->capacity)
	 {
		 Datatype*temp = pSeq->Seq;
		 pSeq->Seq = (Datatype*)realloc(pSeq->Seq ,sizeof(Datatype)*(pSeq->capacity)*2);   //realloc\malloc ???
		 if(NULL == pSeq->Seq)
		 {
			 printf("空间不足!\n");
			 pSeq->Seq = temp;
			 exit(1);
		 }
		 else
		 {
			 pSeq->capacity = (pSeq->capacity)*2;   //如果申请成功,则将容量扩大
		 }
	 }
	 pSeq->Seq[pSeq->size++] = data;
 }

 void PopBack(SeqList* pSeq)   //尾删
 {
	 assert(pSeq);
	 if(0 == pSeq->size)
	 {
		 return ;
	 }
	 else
	 {
		 pSeq->size--;
	 }
 }

 void PushFront(SeqList* pSeq,Datatype data)  //头插
 {
	 int i = 0;
	 assert(pSeq);
	 if(pSeq->size == pSeq->capacity)
	 {
		 Datatype*temp = pSeq->Seq;
		 pSeq->Seq = (Datatype*)realloc(pSeq->Seq,sizeof(Datatype)*(pSeq->capacity)*2);
		 if(NULL == pSeq->Seq)
		 {
			 printf("空间不足!\n");
			 pSeq->Seq = temp;
			 exit(1);
		 }
		 else
		 {
			 pSeq->capacity = (pSeq->capacity)*2;   //如果申请成功,则将容量扩大
		 }
	 }
	 for(i = pSeq->size-1;i >= 0;i--)
	 {
		 pSeq->Seq[i+1] = pSeq->Seq[i];
	 }
	 pSeq->Seq[0] = data;
	 pSeq->size++;
 }
void PopFront(SeqList* pSeq)   //头删
{
	int i = 0;
	assert(pSeq);
	if(0 == pSeq->size)
	{
		return ;
	}
	else
	{
		for(i = 1;i < pSeq->size;i++)
		{
			pSeq->Seq[i-1] = pSeq->Seq[i];
		}
		pSeq->size--;
	}
}

void Insert(SeqList* pSeq,int pos,Datatype data)   //在第pos位置插入一个值,尾插无法实现??
{
	int i = 0;
	if(NULL == pSeq || pos > pSeq->size)
	{
		return ;
	}
	if(pSeq->size == pSeq->capacity)
	{
		Datatype*temp = pSeq->Seq;
		pSeq->Seq = (Datatype*)realloc(pSeq->Seq,sizeof(Datatype)*(pSeq->capacity)*2);
		if(NULL == pSeq->Seq)
		{
			printf("空间不足!\n");
			pSeq->Seq = temp;
			exit(1);
		}
		else
		 {
			 pSeq->capacity = (pSeq->capacity)*2;   //如果申请成功,则将容量扩大
		 }
	}
	for(i = pSeq->size - 1;i >= pos-1;i--)
	{
		pSeq->Seq[i+1] = pSeq->Seq[i];
	}
	pSeq->Seq[pos-1] = data;
	pSeq->size++;
}

void Delete(SeqList* pSeq,int pos)   //删除第pos个元素,[1-pos]
{
	int i = pos;
	if(NULL == pSeq || pos > pSeq->size)
	{
		return ;
	}
	if(0 == pSeq->size)
	{
		printf("表为空!\n");
		return;
	}
    for(;i < pSeq->size;i++)
	{
		pSeq->Seq[i-1] = pSeq->Seq[i];
	}
	pSeq->size--;
}

 int Search(SeqList* pSeq,Datatype data)  //查找
 {
	 int i = 0;
	 assert(pSeq);
	 for(;i < pSeq->size;i++)
	 {
		 if(pSeq->Seq[i] == data)
		 {
			 return i;
		 }
	 }
	 return -1;
 }

 void Remove(SeqList* pSeq,Datatype key)  //删除等于k的值
 {
	 int temp = Search(pSeq,key);
	 assert(pSeq);
	 if(-1 == temp)
	 {
		 return;
	 }
     Delete(pSeq,temp+1);
 }
 void RemoveAll(SeqList* pSeq,Datatype key)   //删除所有等于k的值
 {
	 int i = 0;
	 int count = 0;
	 assert(pSeq);
	 for(;i < pSeq->size;i++)
	 {
		 if(pSeq->Seq[i] == key)
		 {
			 count++;
		 }
		 else
		 {
			 pSeq->Seq[i-count] = pSeq->Seq[i];
		 }
	 }
	 pSeq->size = pSeq->size - count;
 }

  void SelectSort(SeqList* pSeq)   //选择排序
  {
	  int begin = 0;   //一次让最大的和最小的在各自的位置上
	int end = pSeq->size - 1;
	while(begin < end)
	{
		int minpos = begin;
		int maxpos = end;
		int i = begin;     //原来是begin+1;这样的错误是找最大的时候没能和第一个比较
		for(;i <= end;i++)
		{
			if(pSeq->Seq[minpos] > pSeq->Seq[i])
				minpos = i;
			if(pSeq->Seq[maxpos] < pSeq->Seq[i])
				maxpos = i;
		}
		if(minpos != begin)
			swap(&pSeq->Seq[minpos],&pSeq->Seq[begin]);
		if((maxpos != end)&&(minpos != end))    //当改为int i = begin时,这边交换时又出现了问题,就是如果5 4 3 2的话,会被交换两次,因为minpos不等于begin,maxpos不等于end。
			swap(&pSeq->Seq[maxpos],&pSeq->Seq[end]);
		begin++;
		end--;
		
	}
  }
 void swap(Datatype *a,Datatype *b)
 {
	 
	*a = (*a)^(*b);
	*b = (*a)^(*b);
	*a = (*a)^(*b);
 }
void BubbleSort(SeqList* pSeq)    //冒泡排序
{
	int i = 0;
	int j = 0;
	assert(pSeq);
	for(i = 0;i < pSeq->size - 1;i++)
	{
		int flag = 0;
		for(j = 0;j < pSeq->size - i - 1;j++)
		{
			if(pSeq->Seq[j] > pSeq->Seq[j+1])
			{
				swap(&pSeq->Seq[j],&pSeq->Seq[j+1]);
				flag = 1;
			}
		}
		if(0 == flag)
			break;
	}
}

void PrintSeqList(SeqList* pSeq)    //打印
{
	int i = 0;
	assert(pSeq);
	for(;i < pSeq->size;i++)
	{
		printf("%d ",pSeq->Seq[i]);
	}
}

void DGPrintSeqList(SeqList* pSeq,int temp)  //递归打印
{
	assert(pSeq);
	temp--;
	if(0 != temp)
	{
		DGPrintSeqList(pSeq,temp);
	}
	printf("%d ",pSeq->Seq[temp]);
}

void Destory(SeqList* pSeq)      //销毁顺序表
{
	assert(pSeq);
	pSeq->capacity = 0;
	pSeq->size = 0;
	free (pSeq->Seq);
}

int  BinarySort(SeqList* pSeq,Datatype key)   //二分查找
{
	int left = 0;
	int right = pSeq->size - 1;
	int mid = (left + right)/2;
	assert(pSeq);
	while(left <= right)
	{
		mid = (left + right)/2;
		if(key == pSeq->Seq[mid])
		{
			printf("%d\n",mid);
			return mid;
		}
		else if(key < pSeq->Seq[mid])
			right = mid - 1;
		else
			left = mid + 1;

	}
	return -1;
}

int DGBinarySort(SeqList* pSeq,int left,int right,Datatype key)  //递归二分查找
{
	int mid = 0;  //有更好的方法
	assert(pSeq);
	if(left <= right)
	{
		mid = (left + right)/2;
		if(key == pSeq->Seq[mid])
		{
			return mid;
		}
		else if(key < pSeq->Seq[mid])
			DGBinarySort(pSeq,left,mid-1,key);
		else
			DGBinarySort(pSeq,mid+1,right,key);
	}
	else
		return -1;
}

int main()
{
	SeqList List;
	InitSeqList(&List);
	PushBack(&List,3);
	PushBack(&List,4);
	//PopBack(&List);
	PushFront(&List,2);
    PushFront(&List,1);
	//PopFront(&List);
	Insert(&List,4,5);
    PushBack(&List,2);
	//Delete(&List,5);
	//Remove(&List,5);
	//RemoveAll(&List,7);
	SelectSort(&List);
	//BubbleSort(&List);
	//Search(&List,5);
    Destory(&List);
	//DGPrintSeqList(&List,List.size);
    //BinarySort(&List,5);
    //printf("%d\n",DGBinarySort(&List,0,List.size-1,5));
	PrintSeqList(&List);
	return 0;
}







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值