0830hw

1.2.链式队列

head.h

#include 
typedef int datatype;
typedef struct seq
{
	union
	{
		datatype data;
		int len;
	};struct seq *next;
}seq,*S;
typedef struct PP
{
	S front;
	S rear;
}P;
P *create();
void input_tail(P *p,datatype n);
void pop_head(P *p);
void output(P *p);
void myfree(P *p);

fun.c

//创建
P *create()
{
	S H=(S)malloc(sizeof(seq));
	if(H==NULL)
	{
		printf("申请失败\n");
		return NULL;
	}
	H->len=0;
	H->next=NULL;
	P *p=(P *)malloc(sizeof(P));
	if(NULL==p)
	{
		printf("申请失败\n");
		return NULL;
	}
	p->front=H;
	p->rear=H;
	return p;
}	
//入队尾插
void input_tail(P *p,datatype n)
{
	if(p->front==NULL)
	{
		printf("队列不存在\n");
		return ;
	}

	S s=(S)malloc(sizeof(seq));
	s->data=n;
	s->next=NULL;//创建新结点
	p->rear->next=s;//将新节点赋值给rear的后一位
	p->rear=s;//rear重新指向最后一位
	p->front->len++;
}
//出队头删
void pop_head(P *p)
{
	//判空
	if(p->front==p->rear)
	{
		printf("表空\n");
		return;
	}
	S s=p->front->next;
	p->front->next=s->next;
	free(s);
	s=NULL;
	p->front->len--;
}
//遍历
void output(P *p)
{
	if(p->front==NULL)
	{
		printf("队列不存在\n");
		return ;
	}

	//判空
	if(p->front->next==NULL)
	{
		printf("表空\n");
		return;
	}

	S s=p->front->next;
	while(s!=NULL)
	{
		printf("%d\t",s->data);
		s=s->next;
	}
	printf("\n");
}
//释放
void myfree(P *p)
{
	if(p->front==NULL)
	{
		printf("队列不存在\n");
		return ;
	}
	//判空
	if(p->front==p->rear)
	{
		printf("表空\n");
		free(p->front);
		p->front=NULL;//释放头结点
		free(p);//释放头尾指针
		p=NULL;
	}
	S s=p->front->next;
	while(s!=NULL)
	{
		S s2=s;
		s=s->next;
		free(s2);
		s2=NULL;
	}
	free(p->front);
	p->front=NULL;//释放头结点		
	free(p);//释放头尾指针
	p=NULL;
}

3.递归

#include 
int fun(int n)
{
	int m=n;
	int count=0;
	if(n<10)
	{
		printf("%d\t",n);
		return 0;
	}
	while(n>9)
	{
		n=n/10;
		count++;
	}
	printf("%d\t",n);
	n=m-n*pow(10,count);
	fun(n);
}
int main(int argc, const char *argv[])
{
	fun(12345);
	
	return 0;
}

你可能感兴趣的:(算法,数据结构)