- 单链表的快速排序
- 线索化二叉树结构体:
typedef char ElemType;
typedef enum{LINK = 0,THREAD = 1}PointTag;
typedef struct BiThrNode
{
BiThrNode *leftchild;
BiThrNode *rightchild;
PointTag Ltag,Rtag;
ElemType data;
}BiThrNode * ThreadBinaryTree;
- 层次遍历引入队列解决:
void LevelOrder(BtNode *ptr)
{
if (ptr == NULL)
{
return;
}
Queue q;
init_queue(&q);
push_queue(&q,ptr);
while(!empty_queue(&q))
{
ptr = queue_front(&q);
pop_queue(&q);
printf("%c ",ptr->data);
if (ptr->leftchild != NULL)
{
push_queue(&q,ptr->leftchild);
}
if (ptr->rightchild != NULL)
{
push_queue(&q,ptr->rightchild);
}
}
destroy_queue(&q);
}
void printfK(BtNode *ptr,int k)
{
if (ptr == NULL || k == 0)
{
printf() ptr->data;
return;
}
if (ptr->leftchild != NULL)
{
printfK(ptr->leftchild,k-1);
}
if (ptr->rightchild != NULL)
{
printfK(ptr->rightchild,k-1);
}
}
void print_KLevelData(BtNode *ptr,int k)
{
if (ptr == NULL || k<0)
{
return;
}
printfK(ptr,k);
}
- 队列的结构体:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "queue.h"
#define MAXSIZE 10
typedef int ElemType;
typedef struct
{
ElemType data[MAXSIZE];
int front;
int tail
}Queue;
typedef struct
{
ElemType *data;
int front;
int tail;
int maxsize;
int cuisize;
}Queue;
void init_queue(Queue *p)
{
assert(p != 0);
p->front = 0;
p->tail = 0;
p->maxsize = 0;
p->cuisize = 0;
p->data = (ElemType *)malloc(sizeof(ElemType)*p->maxsize);
}
void destroy_queue(Queue *p)
{
assert(p != NULL);
free(p->data);
p->data = NULL;
p->front = 0;
p->tail = 0;
p->maxsize = 0;
p->cuisize = 0;
}
bool push_queue(Queue *p,ElemType value)
{
assert(p != NULL);
if (full_queue(p))
{
return false;
}
p->data[p->tail] = value;
p->tail++;
p->cursize++;
return true;
}
bool empty_queue(Queue *p)
{
assert(p != NULL);
return(size_queue(p) == 0);
}
bool full_queue(Queue *p)
{
assert(p != NULL);
return(size_queue(p) == p->maxsize);
}
ElemType queue_front(Queue *p)
{
assert(p != NULL);
return p->front;
}
ElemType queue_tail(Queue *p)
{
assert(p != NULL);
int cur = p
return p->tail;
}
int size_queue(Queue *p,ElemType value)
{
assert(p != NULL);
return p->cuisize;
}