Stacks, queues and binary tree

Stack: last out.

Base conversion Schematic:

 

 

The following code is the stack

stack.h 

#ifndef STACK_H 
#define STACK_H 

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 

typedef struct StackNode { 
    elemType Data; 
    struct StackNode * Next; 
} Stack; 

// create a stack node / stack - creating a single list node
struct StackNode * create_node (DATA0 elemType;
// push
bool Push_stack (stack * scalp, elemType Data);
// stack - single linked list head puncturing method
bool pop_stack (stack * top, elemType Data *);
#endif

 

stack.c 

#include "stack.h"

// Create stack node / Stack - Creating a single list node
struct StackNode * create_node (elemType Data)
{
  struct Node * = StackNode the malloc (the sizeof (struct StackNode));
  IF (Node = NULL =) {
    perror ( "Fail mallloc:");
    return NULL;
  }
  node-> Data = Data;
  node-> Next = NULL;
  return Node;
}

// push - single linked list head interpolation
bopl push_stack (Stack Top *, elemType Data)
{
  IF (Top == NULL) {
    perror ( "stack does not exist");
    return to false;
  // Create a node
  stack * = create_node node (Data);
  node-> Next TOP- => Next ;
  TOP-> Next = Node;
  Retrun to true;
}

// stack - single linked list head puncturing method
delete_stack BOOL (Top Stack *, * Data elemType)
{
  IF (Top == NULL)
  {
    perror ( "stack does not exist");
    return to false;
  }
  Stack * = TOP- Node> Next;

  IF (Node == NULL)
  {
    perror ( "empty stack");
    return to false;
  }
  // remove the node from the stack
  top-> next = node-> next;
  data // save for the first node and then delete nodes
  * data = node- > Data;
  Free (Node);
  Node = NULL;
  return to true;
}

 

 1 1栈.c
 2 
 3 #include <stdio.h>
 4 #include "stack.h"
 5 
 6 int main
 7 {
 8     //创建栈
 9     Stack  *top = create_node(0);
10     
11     for(int i = 0;i<10;i++)
12     {
13         push_stack(top,i);
14     }
15     
16     //出栈
17     int data = 0;
18 is      the while (pop_stack (Top, & Data))
 . 19          the printf ( " % D " , Data);
 20 is          the printf ( " \ n- " );
 21 is  
22 is      // stack implementation hexadecimal converter 
23 is      int obj = 0 ;
 24      Scanf ( " D% " , & obj);
 25      
26 is      the while (obj)
 27      {
 28          push_stack (Top, obj% 2 );
 29          obj obj = / 2 ;
 30      }
31     while(pop_stack(top,&data));
32     printf("%d",data);
33     printf("\n");
34 
35     return 0;
36 }

 

Queue: FIFO

Here is the code queue:

 

Queue 1 .c 

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>

typedef int elemType;
struct {the Node
  elemType Data;
  struct the Node * Next;
};

typedef _Queue struct
{
  int length;
  struct * Start the Node;
  struct * End the Node;
} queue;

// create a queue
queue create_queue * (0
{
  queue Q * = the malloc (the sizeof (queue));
  // create a queue
  q-> length 0 =;
  Q-> = Q- Start> End = NULL;
  return Q;
}

// queues - interpolation end of the list
BOOL enter_queue (queue Q *, elemType Data)
{
  IF (Q == NULL)
  {
    perror ( "queue error");  
    to false return;
  }
  IF (Q-> length == 0) // queue is empty
  {
    struct the Node Node * = the malloc (the sizeof (struct the Node));
    node-> Data = Data;
    node-> Next = NULL;
    Q- > = Q- Start> End = NULL;
    Q->. 1 = length;
  } the else {
    struct the Node Node * = the malloc (the sizeof (struct the Node));
    node-> Data = Data;
    node-> Next = NULL;
    // node on the back end
    Q-> END-> = Next node;  
    // to point to the linked list tail end
    Q-> = end node;
    Q-> length ++;
  }
  return to true;
}

// the queue - method deleted list head
bool out_queue (queue Q *, elemType Data)
{
  IF (Q == NULL)
  {
    perror ( "error queue");
    to false return;
  }
  IF (Q-> length == 0)
  {
    return to false;
  }

  // start deleting data from Start
  IF (Q-> == Q- Start> End)
  {
    * Data = Q-> Start-> Data ;
    Free (Q-> Start);
    Q-> = Q- Start> End = NULL;
    Q-> length = 0;
  } the else {
    * Data = Q-> Start-> Data;
    // temporary Start
    struct the Node * Q- = tmp> Start;
    // Start backward movement
    Q-> Start = Q-> Start-> Next;
    Q-> length--;
  }
  return to true;
}

int get_queue_length (Queue Q *)
{
  IF (Q = NULL =)
  {
    perror ( 'error queue ");
    return -1;
  }
  return q->length;
}

int main
{
  Queue *q = create_queue();
  for(int i= 0;i<10;i++)
  {
    enter_queue(q,i);
  }

  int data = 0;
  while(out_queue(q,&data)
    printf("%d",data);
  return 0;
}

 Binary Tree

The basic concept of the tree: the tree is only one root node and multiple leaf nodes. Tree layers, depth, the leaves of the tree tree.

Binary Tree: a root node, each node at most two immediate successors.

Binary tree features:

 

 Complete binary tree:

 

 Full binary tree:

 

 Design Binary Tree

struct BTree
{
    int data;
    struct BTree *lchild;
    struct BTree *rchild;
}

Binary tree traversal:

Preorder traversal: first root node in the left leaves, and finally the right leaf. (Preorder traversal first is the root node)

Preorder: left node first, and then the root node, the last right node. (Preorder the left is the root of the tree on the left, the right is the right of the tree)

Postorder: left first node, and then right node, and finally the root node. (Postorder last root node)

 

 

Preorder traversal: ABDEFGCH

Preorder: DBFEGAHC

Postorder: DFGEBHCA

 

Preorder traversal: ABCEDFGH

Preorder: BECAGFHD

After the write-order traversal

 

 实现二叉树

原理图

 

#include <<stdio.h>
#include <stdlib.h>

typedef struct BTree
{
  char data;
  struct BTree *lchild;
  struct BTree *rchild;
}BiTree;

//前序创建树
BiTree *create_tree()
{
  char ch ='\0';
  scanf("%c",&ch);
  printf("***************%c\n",ch);
  
  if(ch == '#')
  {
    return NULL;
  }else{
    BiTree *root = malloc(sizeof(BiTree));
    root->data = ch;
    root->lchild = create_tree();
    root->rchild = create_tree();
    return root;
  }
}

//三种遍历都是不停地递归,直到这边遍历完了,再递归遍历另一边
//前序遍历
void show_tree(BiTree *root)
{
  if(root == NULL)return;
  printf("c\n",root->data);
  show_tree(root->lchild);
  show_tree(root->rchild);
}

//中序遍历
void_mid_tree(BiTree *root)
{
  if(root == NULL)return;
  mid_tree(root->lchild);
  printf("%c",root->data);
  mid_tree(root->rchild);
}

//后序遍历
void tail_tree(BiTree *root)
{
  if(root == NULL)return;
  tail_tree(root->lchild);
  tail_tree(root->lchild);
  printf("%c",root->data);
}

//销毁二叉树
void destroy_tree(BiTree *root)
{
  //后序遍历销毁
  if(root == NULL)return;
  destroy_tree(root->lchild);
  destroy_tree(root->rchild);
  free(root);
  root = NULL;
}

int main()
{
  BiTree *root = create_tree();
  show_tree(root);
  printf("\n");
  mid_tree(root);
  printf("\n");
  tail_tree(root);
  printf("\n");
  return 0;
}

 

PS:有哪里写的不对的,请指正,互相学习。

 

Guess you like

Origin www.cnblogs.com/smallqizhang/p/12423803.html