今日任务
链式队列:
head.h
#ifndef __HEAD_H__
#define __HEAD_H__#include <stdio.h>
#include <stdlib.h>typedef int datatype;
typedef struct link_list{datatype data;struct link_list* next;
}link,*linkp;
typedef struct circulate_line_top{linkp front;linkp rear;
}link_top,*link_topp;link_topp create_top();
int push(link_topp p,datatype data);
int pull(link_topp p);
int output(link_topp p);
int del(link_topp p);#endif
fun.c
#include "head.h"
/** function: 创建top节点* @param [ in] * @param [out] * @return */
link_topp create_top(){link_topp top=(link_topp)malloc(sizeof(link_top));if(NULL==top){puts("top point malloc failed");return NULL;}top->front=NULL;top->rear=NULL;puts("top point create success");return top;
}
/** function: 创新链表节点* @param [ in] * @param [out] * @return */
linkp create(datatype data){linkp p=(linkp)malloc(sizeof(link));if(NULL==p){puts("link list point malloc failed");return NULL;}p->data=data;p->next=NULL;puts("link list point create success");return p;
}
/** function: 空指针判定* @param [ in] * @param [out] * @return */
int void_point(link_topp p){if(NULL==p){puts("null point pass");return -1;}return 0;
}
/** function: 判空* @param [ in] * @param [out] * @return */
int is_empty(link_topp p){if(NULL==p){puts("null point pass");return -1;}if(p->front==NULL){puts("stack is NULL");return 1;}return 0;
}
/** function: 入队* @param [ in] * @param [out] * @return */
int push(link_topp p,datatype data){if(void_point(p))return -1;linkp new=create(data);//入队if(p->front==NULL)p->front=new;if(p->rear!=NULL)p->rear->next=new;p->rear=new;// printf("t->front%d\n",p->front->data);
// printf("t->rear%d\n",p->rear->data);
// printf("t->front%p\n",p->front);puts("push success");return 0;
}
/** function: 出队* @param [ in] * @param [out] * @return */
int pull(link_topp p){if(void_point(p))return -1;//判空if(p->front==NULL){puts("link stack is null ,it's no deed to pull");return 0;}//出队linkp del=p->front;//记录出队指针,freedatatype data=del->data;p->front=p->front->next;free(del);del=NULL;puts("pull success");return 0;
}
/** function: 循环输出* @param [ in] * @param [out] * @return */
int output(link_topp p){linkp i=p->front;//不移动原指针if(void_point(p))return -1;if(is_empty(p))return 0;while(i!=NULL){printf("<-%d",i->data);i=i->next;}puts("output success");return 0;
}
/** function: 销毁* @param [ in] * @param [out] * @return */
int del(link_topp p){if(void_point(p))return -1;linkp del;while(p->front!=NULL){del=p->front;p->front=p->front->next;free(del);}free(p);puts("del success");return 0;
}
main.c
#include "head.h"
//链表实现循环队列---尾插头删
int main(int argc, const char *argv[])
{link_topp t=create_top();push(t,11);push(t,12);push(t,13);push(t,14);printf("t->front%p\n",t->front);output(t);printf("t->front%p\n",t->front);pull(t);output(t);pull(t);output(t);pull(t);output(t);pull(t);output(t);pull(t);output(t);del(t);t=NULL;return 0;
}
运行效果:
递归
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int fun(int a){printf("%d\n",a%10);a/=10;if(a>0)fun(a);return 0;
}
int main(int argc, const char *argv[])
{//使用递归实现程序,输入一个数,输出该数的每个数int a=123456;fun(a);return 0;
}
今日思维导图