首页 > 代码库 > C指针编程之道 ---第五次笔记
C指针编程之道 ---第五次笔记
//内存中的堆栈和数据结构中的堆栈室友区别的
//在数据结构中,常常把堆栈放在一起表示的一种数据结构,
//但是在内存中堆是存储动态的内存,栈是存放静态的以及调用的函数。
//在数据结构中涉及了堆栈,队列,链表等在这里主要实现的是队列。
//循环队列的指针应用
#include <iostream>
#include <cstdio>
#define QueueSize_UarLen 8
using namespace std;
typedef struct
{
int front;
int rear;
int counter;
int uart_data[QueueSize_UartLen];
}CIRQUEUE_UART;
//队列的初始化
void InitQueue(CIRQUEUE_UART *queue)
{
queue->front = 0;
queue->rear = 0;
queue->counter = 0;
}
//入队
int InQueue(CIRQUEUE_UART *queue, int data)
{
if(QueueFull(queue))
{
//输出队列满了
return 0;
}
else
{
queue->uart_data[queue->rear] = data;
queue->counter++;
queue->rear = (queue->rear + 1) % QueueSize_UartLen;
//这里的上一句相当于
//if(queue-<rear + 1 == QueueSize_UartLen)
//{
// queue_rear = 0;
//}
//else
//{
// queue->rear ++;
//}
return 1;
}
}
//出队
int OutQueue(CIRQUEUE_UART *queue, int *p_data)
{
if(QueueEmpty(queue))
{
//输出队空提示
//return 0;
}else
{
*p_data = http://www.mamicode.com/queue->data(font);
queue->counter--;
queue->front = (queue->front + 1) % QueueSize_UartLen;
return 1;
}
}
//判断队列是否为空
int QueueEmpty(QueueSize_UartLen *queue)
{
return queue->counter == 0;
}
//判断是否是满的
int QueueFull(QueueSize_UartLen *queue)
{
return queue->counter == QueueSize_UartLen;
}
int main()
{
/**
*
* /
}
//C世界的树
//这里写的是二叉树的三种遍历方式
#include <stdio.h>
#include <malloc.h>
typedef struct tree_node
{
char data;
struct tree_node *lchild, *rchild;
}BT_Node;
#define Tree_NodeLen sizeof(BT_Node)
BT_Node *tree;
BT_Node *Creat_BTree(BT_Node *t);
void visit_Node(BT_Node *tree);
void Pre_Order(BT_Node *tree);
void Mid_Order(BT_Node *tree);
void After_Order(BT_Node *tree);
int main()
{
printf("\n请输入输的节点\n");
tree = Creat_BTree(Tree);
if(tree)
{
printf("\n前序遍历\n");
Pre_Order(tree);
printf("\n");
printf("\n中序遍历\n");
Mid_Oder(tree);
printf("\n");
printf("\n中序遍历\n");
After_Order(tree);
printf("\n");
}
printf("\n");
return 0;
}
BT_Node *Create_BTree(BT_Node *tree)
{
char ch;
ch = getchar();
if(ch == ‘*‘)
{
tree = null;
}
else
{
tree = (BT_Node *)malloc(Tree_NodeLen);
tree->data = http://www.mamicode.com/ch;
tree->lchild = Create_BTree(tree->lchild);
tree->rchild = Create_BTree(tree->rchild);
}
return(tree);
}
void Visited_Node(BT_Node *tree)
{
printf(" ");
putchar(tree->data);
printf("\t");
}
void Pre_Order(BT_Node *tree)
{
if(! tree)
{
return;
}
else
{
Visit_Node(tree);
Pre_Order(tree->lchild);
Pre_Order(tree->rchild);
}
}
void Mid_Order(BT_Node *tree)
{
if(!tree)
{
return;
}
else
{
Mid_Order(tree->lchild);
Visit_Node(tree);
Mid_Order(tree->rchild);
}
}
void After_Order(BT_Node *tree)
{
if(! tree)
{
return;
}
else
{
After_Order(tree->lchild);
After_Order(tree->rchild);
Visited_Node(tree);
}
}
C指针编程之道 ---第五次笔记