数据结构Day6作业

链栈

使用头指针来指向第一个结点

//定义结构体
typedef struct node
{
//	union
//	{
		datatype data;
//		int len;
//	};
	struct node * next;
}*link_stack;

//创建普通结点
link_stack creat_node(){
	link_stack node=(link_stack)malloc(sizeof(struct node));
	if(NULL==node){
		printf("创建失败\n");
		return NULL;
	}
	node->next=NULL;
	return node;	
}
//链栈的入栈
void push_link_stack(link_stack *L,datatype data){
	if(NULL==L){
		printf("传参错误\n");
		return;
	}
	link_stack new=creat_node();
	if(NULL==new){
		printf("创建失败\n");
		return;
	}
	new->data=data;
	new->next=(*L);
//	L->next=new;
	(*L)=new;
//	L->len++;
	return;
}
//链栈的出栈
void pop_link_stack(link_stack *L){
	if(NULL==L || NULL ==*L){
		printf("传参错误, 栈空\n");
		return;
	}
	printf("出栈元素为:%d\n",(*L)->data);
	link_stack del=(*L);
	*L=del->next;
	free(del);
//	del=NULL;
//	L->len--;
	return;
}


//链栈的遍历
void show_link_stack(link_stack L){
	if(NULL==L){
		printf("传参错误, 栈空\n");
		return;
	}
	link_stack p=L;
	while(p!=NULL){
		printf("%d\t",p->data);
		p=p->next;
	}
	puts("");
}
//链栈的空间释放
void free_link_stack(link_stack *L){
	if(NULL==L||NULL==*L){
		printf("传参错误\n");
		return;
	}
	link_stack p=*L;
	link_stack del;
	while(p!=NULL){
		del=p;
		p=p->next;
		free(del);
	}
//	free(L);
	*L=NULL;
//	return L;
}

链式队列

//链式队列结点结构体
typedef struct node
{
	union{
		datatype data;
		int len;
	};
	struct node *next;
}linkqueue,*linkqueue_p;

//头指针和尾指针结构体
typedef struct Que
{
	//头指针
	linkqueue_p front;
	//尾指针
	linkqueue_p rear;
}*link_Que;
//1.创建链式队列
link_Que create_Que(){
	link_Que q=(link_Que)malloc(sizeof(struct Que));
	if(NULL==q){
		printf("创建失败\n");
		return NULL;
	}
	linkqueue_p H=(linkqueue_p)malloc(sizeof(linkqueue));
	if(NULL==H){
		printf("创建失败\n");
		return NULL;
	}
	//初始化
	H->len=0;
	H->next=NULL;
	q->front=H;
	q->rear=H;
	return q;
}

//2.结点创建
linkqueue_p create_node(){
	linkqueue_p node=(linkqueue_p)malloc(sizeof(linkqueue));
	if(NULL==node){
		printf("创建失败\n");
		return NULL;
	}
	node->next=NULL;
	return node;
}
//3.链式队列的入队(尾插)
void link_enqueue(link_Que q,datatype data){
	if(NULL==q){
		printf("传入参数错误\n");
		return;
	}
	linkqueue_p new=create_node();
	if(NULL==new){
		printf("创建失败\n");
		return;
	}
	new->data=data;
	q->rear->next=new;
	q->front->len++;
	q->rear=new;
	return;
}

//4.链式队列的出队(头删)
void link_dequeue(link_Que q){
	if(NULL==q){
		printf("传入参数错误\n");
		return;
	}
	if(q->front==q->rear){
		printf("队列为空\n");
		return;
	}
	//当队列只有一个结点时,防止队尾指针变成野指针
	if(q->front->next==q->rear){
		q->rear=q->front;
	}
	linkqueue_p del=q->front->next;
	printf("出队元素是:%d\n",del->data);
	q->front->next=del->next;
	free(del);
	del=NULL;
	q->front->len--;
	return;
}
//5.链式队列的遍历
void show_linkqueue(link_Que q){
	if(NULL==q){
		printf("传入参数错误\n");
		return;
	}
	if(q->front==q->rear){
		printf("队列为空\n");
		return;
	}
	linkqueue_p p=q->front;
	while(p->next!=NULL){
		p=p->next;
		printf("%d\t",p->data);
	}
	puts("");
	puts("***************************");
}

//6.链式队列的空间释放
link_Que free_linkqueue(link_Que q){
	if(NULL==q){
		printf("传入参数错误\n");
		return NULL;
	}
	while(q->front->next!=NULL){
		link_dequeue(q);
	}
	free(q->front);
	q->front=q->rear=NULL;
	free(q);
	q=NULL;
	return q;
}

猴子吃桃

  1. 猴子吃桃问题,猴子第一天摘了若干个桃,当即就吃了一半数量的桃,没吃过瘾,又多吃一个,第二天,在剩下的桃里有吃了一半数量的桃,没吃过瘾,又多吃了一个,依此类推,直到第10天,想吃桃的时候,发现只剩下一个桃了,问:猴子第一天摘了多少个桃。(递归完成)
//猴子吃桃
int fun(int n);
int main(int argc, const char *argv[])
{
	int a=fun(10);
	printf(" 第一天摘了%d个桃\n",a);
	return 0;
}
int fun(int n){
	if(1==n){
		return 1;
	}
	else{
		return 2*fun(n-1)+2;
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值