1. 循环队列
Seq_Queue.h文件
#ifndef __SEQ_QUEUE_H__
#define __SEQ_QUEUE_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 10
typedef int ElemType;
typedef struct {
ElemType data[MAXSIZE];
int front,rear;
}SeqQueue,*SeqQueue_p;
//创建循环队列
SeqQueue_p creat_queue();
//循环队列判空
int isEmpty(SeqQueue_p Q);
//循环队列判空
int isFull(SeqQueue_p Q);
//入队
void EnQueue(SeqQueue_p Q,ElemType data);
//打印队列中的元素
void print_queue(SeqQueue_p Q);
//出队
void DeQueue(SeqQueue_p Q,ElemType *e);
//销毁队列
void Destroy_Queue(SeqQueue_p *Q);
#endif
Seq_Queue.c文件
#include "Seq_Queue.h"
//创建循环队列
SeqQueue_p creat_queue()
{
SeqQueue_p Q=(SeqQueue_p)malloc(sizeof(SeqQueue));
if(NULL==Q)
{
printf("空间申请失败\n");
return NULL;
}
bzero(Q->data,sizeof(Q->data));//数组清不清零都可以
Q->front=Q->rear=0;
return Q;
}
//循环队列判空
int isEmpty(SeqQueue_p Q)
{
if(NULL==Q)
{
printf("队列不存在\n");
return -1;
}
return Q->rear==Q->front?1:0;
}
//循环队列判满
int isFull(SeqQueue_p Q)
{
if(NULL==Q)
{
printf("队列不存在\n");
return -1;
}
return (Q->rear+1)%MAXSIZE==Q->front?1:0;
}
//入队
void EnQueue(SeqQueue_p Q,ElemType data)
{
if(NULL==Q)
{
printf("队列不存在\n");
return;
}
if(isFull(Q))
{
printf("队列满\n");
return;
}
Q->data[Q->rear]=data;
Q->rear=(Q->rear+1)%MAXSIZE;
return;
}
//打印队列中的元素
void print_queue(SeqQueue_p Q)
{
if(NULL==Q)
{
printf("队列不存在\n");
return;
}
if(isEmpty(Q))
{
printf("队列为空\n");
return;
}
int i;
for(i=Q->front;i!=Q->rear;i=(i+1)%MAXSIZE)
{
printf("%-3d",Q->data[i]);
}
putchar(10);
}
//出队
void DeQueue(SeqQueue_p Q,ElemType *e)
{
if(NULL==Q)
{
printf("队列不存在\n");
return;
}
if(isEmpty(Q))
{
printf("队列为空\n");
return;
}
*e=Q->data[Q->front];
Q->front=(Q->front+1)%MAXSIZE;
return;
}
//销毁队列
void Destroy_Queue(SeqQueue_p *Q)
{
if(Q==NULL || NULL==*Q)
{
printf("队列不存在\n");
return;
}
free(*Q);
*Q=NULL;
return;
}
main.c文件
#include "Seq_Queue.h"
int main()
{
SeqQueue_p Q=creat_queue();//创建循环队列
printf("入队5个元素:\n");
EnQueue(Q,1);//入队
EnQueue(Q,2);
EnQueue(Q,3);
EnQueue(Q,4);
EnQueue(Q,5);
print_queue(Q);
printf("出队2个元素:\n");
ElemType e;
DeQueue(Q,&e);
printf("第1次出队元素为:%d\n",e);
DeQueue(Q,&e);
printf("第2次出队元素为:%d\n",e);
printf("出队两个元素后的结果为:\n");
print_queue(Q);
Destroy_Queue(&Q);//销毁队列
print_queue(Q);
return 0;
}
2. 链队
Link_Queue.h文件
#ifndef __LINK_QUEUE_H__
#define __LINK_QUEUE_H__
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct LinkNode {
ElemType data;
struct LinkNode *next;
}LinkNode;
typedef struct {
LinkNode *front,*rear;
}LinkQueue;
//创建链队列
LinkQueue* creat_link_queue();
//创建待插元素
LinkNode* creat_link_node(ElemType data);
//判空
int isEmpty(LinkQueue *Q);
//入队
void EnQueue(LinkQueue *Q,ElemType data);
//输出队列
void print_queue(LinkQueue *Q);
//出队,用e保存一下出队元素
void DeQueue(LinkQueue *Q,ElemType *e);
//释放队列
void DestoryQueue(LinkQueue **Q);
#endif
Link_Queue.c文件
#include "Link_Queue.h"
//创建链队列
LinkQueue* creat_link_queue()
{
LinkQueue* Q=(LinkQueue*)malloc(sizeof(LinkQueue));
if(NULL==Q)
{
printf("申请空间失败");
return NULL;
}
Q->front=Q->rear=NULL;//不带头结点
return Q;
}
//创建待插元素
LinkNode* creat_link_node(ElemType data)
{
LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode));
if(NULL==s)
{
printf("空间申请失败\n");
return NULL;
}
s->data=data;
s->next=NULL;
return s;
}
//判空
int isEmpty(LinkQueue *Q)
{
if(NULL==Q)
{
printf("队列不存在\n");
return -1;
}
return Q->front==NULL?1:0;
}
//入队
void EnQueue(LinkQueue *Q,ElemType data)
{
if(NULL==Q)
{
printf("队列不存在\n");
return;
}
LinkNode *s=creat_link_node(data);
if(isEmpty(Q))
{
Q->front=s;
Q->rear=s;
}
Q->rear->next=s;//因为在申请结点时s的next已指向NULL,所以不需再重新赋值为NULL
Q->rear=s;
}
//出队,用e保存一下出队元素
void DeQueue(LinkQueue *Q,ElemType *e)
{
if(NULL==Q)
{
printf("队列不存在\n");
return;
}
if(isEmpty(Q))
{
printf("队列为空\n");
return;
}
LinkNode *p=Q->front;
*e=p->data;
Q->front=Q->front->next;
if(p==Q->rear)//此次是最后一个元素出队
{
Q->front=NULL;
Q->rear=NULL;
}
free(p);
}
//输出队列
void print_queue(LinkQueue *Q)
{
if(NULL==Q)
{
printf("队列不存在\n");
return;
}
if(isEmpty(Q))
{
printf("队列为空\n");
return;
}
LinkNode *p=Q->front;
while(p!=NULL)
{
printf("%-3d",p->data);
p=p->next;
}
putchar(10);
}
//释放队列
void DestoryQueue(LinkQueue **Q)
{
if(NULL==Q || NULL==*Q)
{
printf("队列不存在\n");
return;
}
if(isEmpty(*Q))
{
printf("队列为空\n");
free(*Q);
*Q=NULL;
}
while((*Q)->front!=(*Q)->rear)
{
LinkNode *p;
p=(*Q)->front;
(*Q)->front=(*Q)->front->next;
free(p);
}
LinkNode *p;
p=(*Q)->front;
free(p);
free(*Q);
*Q=NULL;
}
main.c文件
#include "Link_Queue.h"
int main()
{
LinkQueue *Q=creat_link_queue();//创建队列
printf("入队5个元素:\n");
EnQueue(Q,1);//入队
EnQueue(Q,2);
EnQueue(Q,3);
EnQueue(Q,4);
EnQueue(Q,5);
print_queue(Q);
printf("出队两个元素:\n");
ElemType e;//保存出队元素
DeQueue(Q,&e);//出队
printf("出队的第1个元素为:%-3d\n",e);
DeQueue(Q,&e);
printf("出队的第2个元素为:%-3d\n",e);
printf("出队两个元素后的结果为:\n");
print_queue(Q);
DestoryQueue(&Q);
printf("%p\n",Q);
return 0;
}
3. 二叉树,先序遍历、中序遍历、后序遍历
BiTree.h文件
#ifndef __BITREE_H__
#define __BITREE_H__
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct BiTNode {
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
//创建二叉树
BiTree creat_BiTree();
//创建结点
BiTNode* creat_BiTNode(ElemType data);
//访问结点
void visit(BiTree T);
//先序遍历
void PreOrder(BiTree T);
//中序遍历
void InOrder(BiTree T);
//后序遍历
void PostOrder(BiTree T);
#endif
BiTree.c文件
#include "BiTree.h"
//创建二叉树
BiTree creat_BiTree()
{
char data='\0';
scanf(" %c",&data);
if(data=='#')
{
return NULL;
}
BiTree T=creat_BiTNode(data);
T->lchild=creat_BiTree();
T->rchild=creat_BiTree();
return T;
}
//创建结点
BiTNode* creat_BiTNode(ElemType data)
{
BiTNode *s=(BiTNode*)malloc(sizeof(BiTNode));
if(NULL==s)
{
printf("空间申请失败\n");
return NULL;
}
s->data=data;
return s;
}
//访问结点
void visit(BiTree T)
{
printf("%c",T->data);
}
//先序遍历
void PreOrder(BiTree T)
{
if(NULL==T)
{
return;
}
visit(T);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
//中序遍历
void InOrder(BiTree T)
{
if(NULL==T)
{
return;
}
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
//后序遍历
void PostOrder(BiTree T)
{
if(NULL==T)
{
return;
}
PostOrder(T->lchild);
PostOrder(T->rchild);
visit(T);
}
main.c文件
#include "BiTree.h"
int main()
{
printf("请输入树中元素:\n");
BiTree T=creat_BiTree();
printf("先序遍历:\n");
PreOrder(T);
putchar(10);
printf("中序遍历:\n");
InOrder(T);
putchar(10);
printf("后序遍历:\n");
PostOrder(T);
putchar(10);
return 0;
}
4. 画二叉树
先序:FCADBEHGM
中序:ACBDFHEMG