2023.8.30

头文件

#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int datatype;
//创建链表
typedef struct list{
	datatype data;
	struct list *next;
}queue,*plist;
//创建队头队尾
typedef struct Point{
	plist rear;//tail
	plist front;//head
}need,*Pre;
#endif

功能函数

#include "list.h"
//创建
Pre build_queue()
{
	//创建头结点
	plist H=(plist)malloc(sizeof(queue));
	if(NULL==H){
		puts("The application failed");
		return NULL;
	}
	H->next=NULL;
	//创建头尾指针节点
	Pre P=(Pre)malloc(sizeof(need));
	if(NULL==P){
		puts("The application failed");
		return NULL;
	}
	//置零
	P->rear=H;
	P->front=H;
	return P;
}
//判空
int empty_queue(Pre P)
{
	//合法性判断
	if(NULL==P){
		puts("The input is invalid");
		return -1;
	}
	return P->rear==P->front?1:0;
}
//入队
int push_queue(Pre P,datatype data)
{
	//新建普通节点
	plist new=(plist)malloc(sizeof(queue));
	if(NULL==new){
		puts("The application failed");
		return -1;
	}
	new->data=data;
	new->next=P->rear->next;
	P->rear->next=new;
	P->rear=new;
	return 0;
}
//出队
int pop_queue(Pre P)
{
	//判断队列元素是否为空
	if(empty_queue(P)){
		puts("The queue is empty");
		return -1;
	}
	//记录需要出队的元素
	plist pop=P->front->next;
	printf("Elements out of the team :%d\n",P->front->next->data);
	//移动头节点指向输出元素的下一个元素
	P->front->next=pop->next;
	free(pop);//释放出队元素
	pop=NULL;
	return 0;
}
//遍历
int out_queue(Pre P)
{
	//判断队列元素是否为空
	if(empty_queue(P)){
		puts("The queue is empty");
		return -1;
	}
	//记录头结点
	plist H=P->front;
	while(P->front!=P->rear){
		printf("%d->",P->front->next->data);
		P->front=P->front->next;
	}
	putchar(10);
	//遍历结束,front重新指向头结点
	P->front=H;
	return 0;
}
//销毁
int des_queue(Pre P)
{
	//判断队列元素是否为空
	if(empty_queue(P)){
		puts("The queue is empty");
		return -1;
	}
	//循环释放所有队员
	do{
		plist del=P->front;
		P->front=P->front->next;
		free(del);
		del=NULL;
	}while(P->front!=P->rear);
	//释放P
	free(P);
	P=NULL;
	return 0;
}

主函数

#include "list.c"
int main(int argc, const char *argv[])
{
	Pre P=build_queue();
	push_queue(P,1);
	push_queue(P,2);
	push_queue(P,3);
	push_queue(P,4);
	push_queue(P,5);
	push_queue(P,6);
	push_queue(P,7);
	out_queue(P);
	pop_queue(P);
	pop_queue(P);
	pop_queue(P);
	out_queue(P);
	des_queue(P);
	P=NULL;
	out_queue(P);
	return 0;
}

结果展示

1->2->3->4->5->6->7->
Elements out of the team :1
Elements out of the team :2
Elements out of the team :3
4->5->6->7->
段错误 (核心已转储)

 递归

#include<stdio.h>
#include<stdlib.h>
 
//递归方式实现打印一个整数的每一位
 
void Print(int n)
{
	if (n > 9)
	{
		Print(n / 10);
	}
	printf("%d\n", n % 10);
}
int main()
{
	int n;
	printf("请输入一个数n:");
	scanf("%d",&n);
	Print(n);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值