首页 > 学院 > 开发设计 > 正文

有关顺序表操作的函数实现

2019-11-06 06:23:15
字体:
来源:转载
供稿:网友
#include "SeqList.h"void InitSeqlist(pSeqList p)				// 顺序表初始化{	p->count = 0;	memset(p->arr,0,sizeof(DataType)*MAX);}void PushBack(pSeqList p,DataType d)			//后插{	assert(p);	if(MAX == p->count)	{		PRintf("满了/n");		return;					//顺序表满了返回	}	p->arr[p->count] = d;	p->count++;}void Show(pSeqList p)					//打印顺序表{	int i = 0;	assert(p);	for(i = 0; i < p->count; i++)			//遍历一遍	{		printf("%d ",p->arr[i]);	}	printf("/n");}void PopBack(pSeqList p)				//后弹{	assert(p);	if(p->count == 0)	{		return;	}	p->count--;}void PushFront(pSeqList p,DataType d)			//头插{	int i = p->count;	assert(p);	while(i)					//所有值右移	{		p->arr[i] = p->arr[i-1];		//腾出第一位置		i--;	}	p->arr[0] = d;					//插入	p->count++;}void PopFront(pSeqList p)				//头弹{	int i = 1;	assert(p);	while(i < p->count)				//所有值左移	{		p->arr[i-1] = p->arr[i];		i++;	}	p->count--;}int Find(pSeqList p,DataType d)				//查找节点{	int i = 0;	assert(p);	while(i < p->count)	{		if(p->arr[i] == d)			return i;		else			i++;	}	return -1;}void Remove(pSeqList p,DataType d)			//删除节点{	int i = 0;	for(i=0; i < p->count; i++)	{		if(p->arr[i] == d)		{			for(; i < p->count; i++)			{				p->arr[i] = p->arr[i+1];			}		}	}	p->count--;}void Sort(pSeqList p)					//冒泡排序{	int i=1;	int j=0;	DataType temp=0;	for(i=1;i<p->count;i++)		for(j=0;j<p->count-i;j++)			if(p->arr[j]>p->arr[j+1])			{				temp = p->arr[j];				p->arr[j] = p->arr[j+1];				p->arr[j+1] = temp;			}}int BinarySearch(pSeqList p,DataType d)			//二分查找{	int left = 0;	int right = p->count;	while(left <= right)	{		int mid = left - ((left - right)>>1);		if(p->arr[mid] == d)			return mid;		else if(p->arr[mid] < d)		{			left = mid+1;		}		else		{			right = mid-1;		}	}	return -1;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表