猴子吃桃问题(递归)
#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);
}