数据结构 作业6

本文详细介绍了如何用C语言编写猴子吃桃的递归函数,并通过实例展示了链栈和循环队列的数据结构,包括创建、操作和销毁过程。
摘要由CSDN通过智能技术生成

 猴子吃桃问题(递归)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int fun(int i)
{
	if(i==10)
	{
		return 1;
	}
	else
		
		return 2*(fun(i+1)+1) ;
}
int main(int argc, const char *argv[])
{
   int i=1;
   int sum=fun(i);
   printf("%d\n",sum);

	return 0;
}

链栈


//8、创建链栈
void create_link_stack(link_s *L)
{
   *L = NULL;
}

//9、创建新结点
link_s creat_node(datatype data)
{
	link_s new =(link_s)malloc(sizeof(node));
	if (new==NULL)
	{
		printf("申请空间失败 !\n");
		return NULL;
	}
	new->data=data;
	return new;
}

//10、判空
int empty_link_stack(link_s L)
{
	
    return L==NULL?1:0;
}
//11、入栈
void push_link_stack(link_s *L,datatype data)
{
	if(L==NULL)
	{
		printf("Error !\n");
          return;
	}
	
	link_s new= creat_node(data);
    new->next=*L;
	*L=new;
}
//12、出栈
void out_link_stack(link_s *L)
{	
	if(L==NULL)
	{
		printf("Error !\n");
          return;
	}

	link_s p=*L;
	*L=p->next;
	free(p);

}
//13、清空链栈
void clear_link_stack(link_s *L)
{	
	if(L==NULL)
	{
		printf("Error !\n");
          return;
	}

    while(*L!=NULL)
	{
		out_link_stack(L);
	}
}
//14、输出链栈
void show_link_stack(link_s L)
{	
	if(L==NULL)
	{
		printf("Error !\n");
          return;
	}
    link_s p = L;
	while(p!=NULL)
	{
		printf("%d\n",p->data);
		p=p->next;
	}
	putchar(10);

}

 

循环队列

#include"queue.h"

//1、创建队列
queue *create_q ()
{
	queue *Q = (queue *)malloc(sizeof(queue));

	if(Q==NULL)
	{
		printf("申请空间失败!\n");
		return NULL;
	}
    
	Q->rear=Q->front=0;

		return Q;

}
//2、判空
int empty_queue(queue *Q)
{
    return Q->rear==Q->front?1:0;
}

//3、判满
int full_queue(queue *Q)
{
	return (Q->rear+1)%MAX==Q->front?1:0;
}

//4、入队
void push_queue(queue *Q,datatype data)
{
	if(Q==NULL)
	{
		printf("Erorr!\n");
		return;
	}
    if(full_queue(Q))
	{
		printf("队列满,无法入队!\n");
		return ;
	}
	Q->data[Q->rear]=data;
	Q->rear=(Q->rear+1)%MAX;
}

//5、出队
void pop_queue(queue *Q)
{
	if(Q==NULL)
	{
		printf("Erorr!\n");
		return;
	}
    if(empty_queue(Q))
	{
		printf("队列空,无法出队!\n");
		return ;
	}
	Q->front=(Q->front+1)%MAX;
}

//6、销毁
void free_queue(queue *Q)
{
	if(Q==NULL)
	{
		printf("Erorr!\n");
		return;
	}
	while(Q->front!=Q->rear)
	{
	   Q->front=(Q->front+1)%MAX;
	}
	free(Q);

}

//7、输出队列
void show_queue(queue *Q)
{
	if(Q==NULL)
	{
		printf("Erorr!\n");
		return;
	}
    if(empty_queue(Q))
	{
		printf("队列空,无法输出!\n");
		return ;
	}
    while(Q->front!=Q->rear)
	{
		printf("%d\n",Q->data[Q->front]);
		Q->front=(Q->front+1)%MAX;
	}
	putchar(10);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值