头文件
#pragma once
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <stdlib.h>
struct BinaryTreeNod;//前置申明
typedef struct BinaryTreeNode* QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
}Queue;
void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);
// 队尾入
void QueuePush(Queue* pq, QDataType x);
// 队头出
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
int QueueSize(Queue* pq);
bool QueueEmpty(Queue* pq);
队列功能
#include "Queue.h"
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
}
void QueueDestory(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
}
// 队尾入
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (pq->tail == NULL)
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}
// 队头出
void QueuePop(Queue* pq)
{
assert(pq);
assert(pq->head);
// 1、一个
// 2、多个
if (pq->head->next == NULL)
{
free(pq->head);
pq->head = pq->tail = NULL;
}
else
{
QNode* next = pq->head->next;
free(pq->head);
pq->head = next;
}
}
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->head);
return pq->head->data;
}
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->head);
return pq->tail->data;
}
int QueueSize(Queue* pq)
{
assert(pq);
int size = 0;
QNode* cur = pq->head;
while (cur)
{
++size;
cur = cur->next;
}
return size;
}
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}
二叉树的前中后序的实现,用递归和队列计算叶子节点和节点
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<stdlib.h>
#include "Queue.h"
typedef char Datetype;
struct Node//左孩子,右兄弟
{
struct Node* _firstchild;
struct Node* _pNextBrother;
Datetype _Date;
};
typedef struct BinaryTreeNode{
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
Datetype date;
}BTNode;
void PrevOrder(BTNode* root)
{
if (root == NULL){
printf("NULL ");
return;
}
printf("%c ", root->date);
PrevOrder(root->left);
PrevOrder(root->right);
}
void InOrder(BTNode* root)
{
if (root == NULL){
printf("NULL ");
return;
}
PrevOrder(root->left);
printf("%c ", root->date);
PrevOrder(root->right);
}
void PostOrder(BTNode* root)
{
if (root == NULL){
printf("NULL ");
return;
}
PrevOrder(root->left);
PrevOrder(root->right);
printf("%c ", root->date);
}
//int* TreeSize(BTNode* root,int* psize)//遍历
//{
//
// if (root == NULL){
// return psize;
// }
// else
// {
// (*psize)++;
// }
// TreeSize(root->left, psize);
// TreeSize(root->right, psize);
// return psize;
//}
int TreeSize(BTNode* root)
{
return root == NULL ? 0 : TreeSize(root->left) + TreeSize(root->right) + 1;
}
int TreeLeafSize(BTNode* root)//递归方法
{
if (root == NULL)
{
return 0;
}
if (root->left == NULL && root->right == NULL)
{
return 1;
}
return TreeLeafSize(root->left) + TreeLeafSize(root->right);
}
void Levelorder(BTNode* root)//队列方法
{
Queue q;//创建队列
QueueInit(&q);//初始化
if (root)//确保压入队列的二叉树不为空
QueuePush(&q, root);//压入二叉树的第一个节点的指针(头结点的指针就是二叉树的全部)
while (!QueueEmpty(&q))//确保循环时队列不为空
{
BTNode* front = QueueFront(&q);//二叉树的第一个节点的指针写为队列的第一个值
printf("%c", front->date);
QueuePop(&q);
if (front->left)
{
QueuePush(&q,front->left);
}
if (front->right)
{
QueuePush(&q, front->right);
}
}
printf("\n");
QueueDestory(&q);
}
int main()
{
BTNode* A = (BTNode*)malloc(sizeof(BTNode));
if (A == NULL)
printf("内存分配失败");
A->date = 'A';
A->left = NULL;
A->right = NULL;
BTNode* B = (BTNode*)malloc(sizeof(BTNode));
if (B == NULL)
printf("内存分配失败");
B->date = 'B';
B->left = NULL;
B->right = NULL;
BTNode* C = (BTNode*)malloc(sizeof(BTNode));
if (C == NULL)
printf("内存分配失败");
C->date = 'C';
C->left = NULL;
C->right = NULL;
BTNode* D = (BTNode*)malloc(sizeof(BTNode));
if (D == NULL)
printf("内存分配失败");
D->date = 'D';
D->left = NULL;
D->right = NULL;
BTNode* E = (BTNode*)malloc(sizeof(BTNode));
if (E == NULL)
printf("内存分配失败");
E->date = 'E';
E->left = NULL;
E->right = NULL;
A->left = B;
A->right = C;
B->left = D;
B->right = E;
PrevOrder(A);
printf("\n");
InOrder(A);
printf("\n");
PostOrder(A);
printf("\n");
/*int size = 0;
treesize(A, &size);
printf("%d ",size);
size = 0;
treesize(B, &size);
printf("%d ",size);*/
int size = 0;
/*size=TreeSize(A);
printf("%d ", size);
size=TreeSize(B);
printf("%d ", size);*/
size = TreeLeafSize(A);
printf("%d \n", size);
Levelorder(C);
}
排序
int main()
{
int n=0;
int arr[200];
int i = 0;
scanf("%d ", &n);
for (i = 0; i<n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i<n - 1; i++)
{
int j = 0;
for (j = 0; j<n - 1 - i; j++)
{
if (arr[j]>arr[j + 1])
{
int tmp;
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
for (i = 0; i<n; i++)
{
printf("%d ", arr[i]);
}
}
|