链式队列代码
#ifndef __LIST_H__
#define __LIST_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node,*node_p;
typedef struct T
{
node_p front;
node_p rear;
}zt,*zt_p;
zt_p initia();
node_p create_node(int data);
int empty_que(zt_p T);
void push_que(zt_p T,int data);
int pop_que(zt_p T);
void show_que(zt_p T);
void des_que(zt_p *T);
#endif
#include "list.h"
int main(int argc, const char *argv[])
{
zt_p T=initia();
push_que(T,99);
push_que(T,100);
push_que(T,101);
push_que(T,102);
show_que(T);
printf("出队元素:%d\n",pop_que(T));
show_que(T);
des_que(&T);
return 0;
}
#include"list.h"
zt_p initia()
{
zt_p T=(zt_p)malloc(sizeof(zt));
if(T==NULL)
{
printf("空间申请失败\n");
return NULL;
}
T->front=NULL;
T->rear=NULL;
return T;
}
node_p create_node(int data)
{
node_p new=(node_p)malloc(sizeof(node));
if(new==NULL)
{
printf("空间申请失败\n");
return NULL;
}
new->data=data;
return new;
}
int empty_que(zt_p T)
{
if(T==NULL)
{
printf("入参为空\n");
return -1;
}
return T->front==NULL;
}
void push_que(zt_p T,int data)
{
if(T==NULL)
{
printf("入参为空\n");
return;
}
node_p new=(node_p)malloc(sizeof(node));
if(new==NULL)
{
printf("空间申请失败\n");
return;
}
new->data=data;
new->next=NULL;
if(empty_que(T))
{
T->front=T->rear=new;
}
else
{
T->rear->next=new;
T->rear=new;
}
}
int pop_que(zt_p T)
{
if(T==NULL)
{
printf("入参为空\n");
return -1;
}
if(empty_que(T))
{
printf("链式列为空\n");
return -2;
}
node_p p=T->front;
int data=p->data;
T->front=T->front->next;
if(T->front==NULL)
{
T->rear=NULL;
}
free(p);
return data;
}
void show_que(zt_p T)
{
if(T==NULL)
{
printf("入参为空\n");
return;
}
if(empty_que(T))
{
printf("链式列为空\n");
return;
}
node_p p=T->front;
for(;p!=NULL;p=p->next)
{
printf("%-4d",p->data);
}
putchar(10);
}
void des_que(zt_p *T)
{
if(empty_que(*T))
{
printf("入参为空\n");
}
if(empty_que(*T))
{
pop_que(*T);
}
free(*T);
*T=NULL;
}
二叉树遍历代码
#ifndef _BTREE_H__
#define _BTREE_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
}node,*node_p;
node_p creat_node(char data);
node_p creat_btree();
void pri_show(node_p T);
void cen_show(node_p T);
void lad_show(node_p T);
#endif
#include "btree.h"
int main(int argc, const char *argv[])
{
node_p T=creat_btree();
pri_show(T);
putchar(10);
cen_show(T);
putchar(10);
lad_show(T);
putchar(10);
return 0;
}
#include "btree.h"
node_p creat_btree()
{
char data ='\0';
scanf("%c",&data);
if(data=='#')
{
return NULL;
}
node_p new=creat_node(data);
new->lchild=creat_btree();
new->rchild=creat_btree();
return new;
}
node_p creat_node(char data)
{
node_p new=(node_p)malloc(sizeof(node));
if(new==NULL)
{
printf("空间申请失败\n");
return NULL;
}
new->data = data;
return new;
}
void pri_show(node_p T)
{
if(T==NULL)
{
return;
}
printf("%c",T->data);
pri_show(T->lchild);
pri_show(T->rchild);
}
void cen_show(node_p T)
{
if(T==NULL)
{
return;
}
cen_show(T->lchild);
printf("%c",T->data);
cen_show(T->rchild);
}
void lad_show(node_p T)
{
if(T==NULL)
{
return;
}
lad_show(T->lchild);
lad_show(T->rchild);
printf("%c",T->data);
}
画二叉树:先序:FCADBEHGM
中序:ACBDFHEMG