一、链栈
/*------------链栈的创建------------*/
void create_link_stack(node_p* Stack)
{
*Stack=NULL;
}
/*---------链栈新结点的创建---------*/
node_p create_new_node_link_stack(datatype newdata)
{
node_p New_node=(node_p)malloc(sizeof(node));
if(New_node==NULL)
{
printf("空间申请失败\n");
}
New_node->data=newdata;
New_node->next=NULL;
return New_node;
}
/*------------链栈的判空------------*/
int empty_link_stack(node_p Stack)
{
return Stack==NULL?1:0;
}
/*------------链栈的入栈------------*/
void push_link_stack(node_p* Stack,datatype newdata)
{
if(Stack==NULL)
{
printf("参数为空,请检查\n");
return;
}
node_p New_node=create_new_node_link_stack(newdata);
New_node->next=*Stack;
*Stack=New_node;
}
/*------------链栈的出栈------------*/
void pop_link_stack(node_p* Stack)
{
if(Stack==NULL)
{
printf("参数为空,请检查\n");
return;
}
if(empty_link_stack(*Stack))
{
printf("链栈已空,无法出栈\n");
return;
}
node_p Delete_node=*Stack;
*Stack=Delete_node->next;
printf("出栈的元素是%d\n",Delete_node->data);
free(Delete_node);
}
/*------------链栈的输出------------*/
void show_link_stack(node_p Stack)
{
if(Stack==NULL)
{
printf("参数为空,请检查\n");
return;
}
if(empty_link_stack(Stack))
{
printf("链栈已空,无法输出");
return;
}
printf("该栈的元素有\n");
while(Stack!=NULL)
{
printf("%d\n",Stack->data);
Stack=Stack->next;
}
}
二、循环队列
/*-------------循环队列的创建------------*/
queue* create_cricul_queue(void)
{
queue* Queue=(queue*)malloc(sizeof(queue));
if(Queue==NULL)
{
printf("空间申请失败\n");
return NULL;
}
Queue->front=0;
Queue->rear=0;
return Queue;
}
/*-------------循环队列的判空------------*/
int empty_cricul_queue(queue* Queue)
{
if(Queue==NULL)
{
printf("参数为空,请检查\n");
return -1;
}
return Queue->front==Queue->rear?1:0;
}
/*-------------循环队列的判满------------*/
int full_cricul_queue(queue* Queue)
{
if(Queue==NULL)
{
printf("参数为空,请检查\n");
return -1;
}
return (Queue->rear+1)%MAX==Queue->front?1:0;
}
/*-------------循环队列的入队------------*/
void enter_cricul_queue(queue* Queue,datatype newdata)
{
if(Queue==NULL)
{
printf("参数为空,请检查\n");
return;
}
if(full_cricul_queue(Queue))
{
printf("队列已满,无法入队\n");
return;
}
Queue->arr[Queue->rear]=newdata;
Queue->rear=(Queue->rear+1)%MAX;
}
/*-------------循环队列的出队------------*/
void exit_cricul_queue(queue* Queue)
{
if(Queue==NULL)
{
printf("参数为空,请检查\n");
return;
}
if(empty_cricul_queue(Queue))
{
printf("队列已空,无法出队\n");
return;
}
printf("出队的元素是%d\n",Queue->arr[Queue->front]);
Queue->front=(Queue->front+1)%MAX;
}
/*-------------循环队列的销毁------------*/
void free_cricul_queue(queue** Queue)
{
if(Queue==NULL||*Queue==NULL)
{
printf("参数为空,请检查\n");
return;
}
free(*Queue);
*Queue==NULL;
}
/*-------------循环队列的输出------------*/
void show_circul_queue(queue* Queue)
{
if(Queue==NULL)
{
printf("参数为空,请检查\n");
return;
}
if(empty_cricul_queue(Queue))
{
printf("循环队列已空,无法输出\n");
return;
}
int i;
printf("循环队列的元素有\n");
for(i=Queue->front;i!=Queue->rear;i=(i+1)%MAX)
{
printf("%d\n",Queue->arr[i]);
}
}
三、猴子摘桃
int total_peach(int n)
{
if(n==10)
{
return 1;
}
else
{
return (total_peach(n+1)+1)*2;
}
}