【无标题】

链式队列代码

#ifndef __LIST_H__
#define __LIST_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
    int data;
    struct node *next;
}node,*node_p;
typedef struct T
{
    node_p front;
    node_p rear;
}zt,*zt_p;
zt_p initia();
node_p create_node(int data);
int empty_que(zt_p T);
void push_que(zt_p T,int data);
int pop_que(zt_p T);
void show_que(zt_p T);
void des_que(zt_p *T);
#endif

#include "list.h"
int main(int argc, const char *argv[])
{
    zt_p T=initia();
    push_que(T,99);
    push_que(T,100);
    push_que(T,101);
    push_que(T,102);
    show_que(T);
    printf("出队元素:%d\n",pop_que(T));
    show_que(T);    
    des_que(&T);
    return 0;
}

#include"list.h"
zt_p initia()
{
    zt_p T=(zt_p)malloc(sizeof(zt));
    if(T==NULL)
    {
        printf("空间申请失败\n");
        return NULL;
    }
    T->front=NULL;
    T->rear=NULL;
    return T;
}
node_p create_node(int data)
{
    node_p new=(node_p)malloc(sizeof(node));
    if(new==NULL)
    {
        printf("空间申请失败\n");
        return NULL;
    }
    new->data=data;
    return new;
}
int empty_que(zt_p T)
{
    if(T==NULL)
    {
        printf("入参为空\n");
        return -1;
    }
    return T->front==NULL;
}

void push_que(zt_p T,int data)
{
    if(T==NULL)
    {
        printf("入参为空\n");
        return;
    }
    node_p new=(node_p)malloc(sizeof(node));
    if(new==NULL)
    {
        printf("空间申请失败\n");
        return;
    }
    new->data=data;
    new->next=NULL;
    if(empty_que(T))
    {
        T->front=T->rear=new;
    }
    else
    {
        T->rear->next=new;
        T->rear=new;
    }
}
int pop_que(zt_p T)
{
    if(T==NULL)
    {
        printf("入参为空\n");
        return -1;
    }
    if(empty_que(T))
    {
        printf("链式列为空\n");
        return -2;
    }
    node_p p=T->front;
    int data=p->data;
    T->front=T->front->next;
    if(T->front==NULL)
    {
        T->rear=NULL;
    }
    free(p);
    return data;
}
void show_que(zt_p T)
{
    if(T==NULL)
    {
        printf("入参为空\n");
        return;
    }
    if(empty_que(T))
    {
        printf("链式列为空\n");
        return;
    }
    node_p p=T->front;
    for(;p!=NULL;p=p->next)
    {
        printf("%-4d",p->data);
    }
    putchar(10);
}
void des_que(zt_p *T)
{
    if(empty_que(*T))
    {
        printf("入参为空\n");
    }
    if(empty_que(*T))
    {
        pop_que(*T);
    }
    free(*T);
    *T=NULL;
}

二叉树遍历代码

#ifndef _BTREE_H__        
#define _BTREE_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
    char data;
    struct node *lchild;
    struct node *rchild;
}node,*node_p;
node_p creat_node(char data);
node_p creat_btree();
void pri_show(node_p T);
void cen_show(node_p T);
void lad_show(node_p T);
#endif

#include "btree.h"


int main(int argc, const char *argv[])
{
    node_p T=creat_btree();
    pri_show(T);
    putchar(10);
    cen_show(T);
    putchar(10);
    lad_show(T);
    putchar(10);
    return 0;
}

#include "btree.h"
node_p creat_btree()
{
    char data ='\0';
    scanf("%c",&data);
    if(data=='#')
    {
        return NULL;
    }
    node_p new=creat_node(data);
    new->lchild=creat_btree();
    new->rchild=creat_btree();
    return new;
}
node_p creat_node(char data)
{
    node_p new=(node_p)malloc(sizeof(node));
    if(new==NULL)
    {
        printf("空间申请失败\n");
        return NULL;
    }
    new->data = data;
    return new;
}
void pri_show(node_p T)
{
    if(T==NULL)
    {
        return;
    }
    printf("%c",T->data);
    pri_show(T->lchild);
    pri_show(T->rchild);
}
void cen_show(node_p T)
{
    if(T==NULL)
    {
        return;
    }
    cen_show(T->lchild);
    printf("%c",T->data);
    cen_show(T->rchild);
}
void lad_show(node_p T)
{
    if(T==NULL)
    {
        return;
    }
    lad_show(T->lchild);
    lad_show(T->rchild);
    printf("%c",T->data);
}

画二叉树:先序:FCADBEHGM
  中序:ACBDFHEMG

  • 24
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值