数据结构 day3

1.

 2.

void maopao(Seqptr p)
{
	for(int i=1;i<p->len;i++)
	{
		for(int j=0;j<p->len-i;j++)
		{
			if(p->arr[j]>p->arr[j+1])
			{
				int temp;
				temp=p->arr[j];
				p->arr[j]=p->arr[j+1];
				p->arr[j+1]=temp;
			}
		}
	}
}
void xuanze(Seqptr p)
{
	for(int i=0;i<p->len-1;i++)
	{
		int max=i;
		for(int j=i+1;j<p->len;j++)
		{
			if(p->arr[max]<p->arr[j])
				max=j;
		}
			int temp;
			temp=p->arr[i];
			p->arr[i]=p->arr[max];
			p->arr[max]=temp;
	}
}

3.

Seqptr seqlist_create()         //创建顺序表
{
	Seqptr P= (Seqptr)malloc(sizeof(Seq));
	if(P==NULL)
		return NULL;
	memset(P->arr,0,sizeof(P->arr));
	P->len=0;
	return P;
}

int full_list(Seqptr p)         //判断是否满 
{
	if(p==NULL)
	{
		printf("无参\n");
		return -1;
	}
	return p->len==MAX?1:0;
}
int empty_list(Seqptr p)      //判断是否空
{
	if(p==NULL)
	{
		printf("无参\n");
		return -1;
	}
	return p->len==0?1:0;
}
int insert_tail(Seqptr p,int data)     //尾插函数
{
	if(p==NULL)
	{
		printf("无参\n");
		return -1;
	}
	if(p->len==MAX)
	{
		printf("已满,不可插入\n");
		return -2;
	}
	p->arr[p->len]=data;           //最后一位赋值
	p->len++;
	return 0;
}
int insert_head(Seqptr p,int data)     //头插函数
{
	if(p==NULL)
	{
		printf("无参\n");
		return -1;
	}
	if(p->len==MAX)
	{
		printf("已满,不可插入\n");
		return -2;
	}
	for(int i=p->len-1;i>=0;i--)    // 向后移一位
		p->arr[i+1]=p->arr[i];
	p->arr[0]=data;            //首位赋值
	p->len++;
	return 0;
}
void output(Seqptr p)       //输出函数
{
	if(p==NULL)
	{
		printf("无参\n");
	}
	for(int i=0;i<p->len;i++)
	{
		printf("%d\n",p->arr[i]);
	}
}
int del_tail(Seqptr p)       // 尾删函数
{
	if(p==NULL)
	{
		printf("无参\n");
		return -1;
	}
	if(p->len==0)
	{
		printf("空,不可删除\n");
		return -2;
	}
	p->arr[p->len-1]=0;          //最后一位赋0
	p->len--;
	return 0;
}
int del_head(Seqptr p)     //头删函数
{
	if(p==NULL)
	{
		printf("无参\n");
		return -1;
	}
	if(p->len==0)
	{
		printf("空,不可删除\n");
		return -2;
	}
	for(int i=1;i<=p->len;i++)     //向前移一位
		p->arr[i-1]=p->arr[i];    //最后一位为0 赋给前一位
	p->len--;
	return 0; 
}
int insert(Seqptr p,int pos,int data)   //选定位置插入函数
{
	if(full_list(p)||pos<0||pos>p->len)
	{
		printf("插入失败\n");
		return -1;
	}
	for(int i=p->len;i>=pos;i--)
		p->arr[i]=p->arr[i-1];
	p->arr[pos-1]=data;
	p->len++;
	return 0;
}
int search_value(Seqptr p,int value,int new)  //按值替换函数
{
	if(p==NULL)
	{
		printf("无参\n");
		return -1;
	}
	if(p->len==0)
	{
		printf("空,不可修改\n");
		return -2;
	}
	for(int i=0;i<p->len;i++)
	{
		if(p->arr[i]==value)
			p->arr[i]=new;
	}
	return 0;
}
int search_pos(Seqptr p,int pos,int new)  //按位置替换函数
{
	if(p==NULL)
	{
		printf("无参\n");
		return -1;
	}
	if(p->len==0)
	{
		printf("空,不可修改\n");
		return -2;
	}
	if(pos>=p->len||pos<0)
	{
		printf("位置错误,不可修改\n");
		return -3;
	}
	p->arr[pos-1]=new;
	return 0;
}
int del(Seqptr p,int pos)   //按位置删除函数
{
	if(p==NULL)
	{
		printf("无参\n");
		return -1;
	}
	if(empty_list(p)||pos<0||pos>=p->len)
	{
		printf("不可删除\n");
		return -2;
	}
	for(int i=pos;i<p->len;i++)
		p->arr[i]=p->arr[i+1];
	p->len--;
	return 0;
}
int find_value(Seqptr p,int value)  //按值查找函数
{
	if(p==NULL)
	{
		printf("无参\n");
		return -1;
	}
	if(empty_list(p))
	{
		printf("空,不可查找\n");
		return -2;
	}
	for(int i=0;i<p->len;i++)
	{
		if(p->arr[i]==value)
		{
			printf("位置为%d",i);
			return 0;
		}
	}
	printf("没有该数,无法查找\n");
	return -3;
}
int find_pos(Seqptr p,int pos)  //按位置查找函数
{
	if(p==NULL)
	{
		printf("无参\n");
		return -1;
	}
	if(empty_list(p)||pos<0||pos>=p->len)
	{
		printf("空,不可查找\n");
		return -2;
	}
	printf("该位置的值为%d",p->arr[pos]);
	return 0;
}
int nosame_list(Seqptr p)    //去重函数
{
	if(p==NULL)
	{
		printf("无参\n");
		return -1;
	}
	if(empty_list(p))
	{
		printf("空,无法去重;");
		return-2;
	}
	for(int i=0;i<p->len;i++)     //两次循环,第一次表示次数,第二次遍历元素
	{
		for(int j=i+1;j<p->len;j++)
		{
			if(p->arr[i]==p->arr[j])
			{
				del(p,j);
				j--;      //删除后 j返回原来的值
			}
		}
	}
	return 0;
}
void free_list(Seqptr p)
{
	if(p!=NULL)
	{
		free(p);
		p=NULL;
		printf("成功销毁\n");
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值