Application of pointers in data structure
In-memory stacks and data structures in the stack roommate area other
In the data structure, the stack is often put together to represent a data structure,
But in-memory heaps are storage of dynamic memory, and stacks are stored as static and called functions.
In the data structure involved in the stack, queues, lists, etc. are mainly implemented here is the queue.
Pointer application for circular queues
#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;
Initialization of the queue
void Initqueue (Cirqueue_uart *queue)
{
Queue->front = 0;
queue->rear = 0;
Queue->counter = 0;
}
Team
int InQueue (cirqueue_uart *queue, int data)
{
if (Queuefull (queue))
{
The output queue is full.
return 0;
}
Else
{
Queue->uart_data[queue->rear] = data;
queue->counter++;
Queue->rear = (queue->rear + 1)% Queuesize_uartlen;
The last sentence here is equivalent to
if (queue-<rear + 1 = = Queuesize_uartlen)
//{
// queue_rear = 0;
//}
Else
//{
// Queue->rear + +;
//}
return 1;
}
}
Out Team
int Outqueue (cirqueue_uart *queue, int *p_data)
{
if (Queueempty (queue))
{
Output team NULL prompt
return 0;
}else
{
*p_data = Queue->data (font);
queue->counter--;
Queue->front = (queue->front + 1)% Queuesize_uartlen;
return 1;
}
}
Determine if the queue is empty
int Queueempty (Queuesize_uartlen *queue)
{
return queue->counter = = 0;
}
Judging if it's full.
int Queuefull (Queuesize_uartlen *queue)
{
return queue->counter = = Queuesize_uartlen;
}
int main ()
{
/**
*
* /
}
The tree of the C world
Here are three ways to traverse a two-fork tree
#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 please input the node \ n");
Tree = Creat_btree (tree);
if (tree)
{
printf ("\ n pre-order traversal \ n");
Pre_order (tree);
printf ("\ n");
printf ("\ n sequence traversal \ n");
Mid_oder (tree);
printf ("\ n");
printf ("\ n sequence traversal \ 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 = 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 Pointer Programming path---fifth notes