数据结构层序遍历

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dpdpd/article/details/79996790

以前写的数据结构的东西,先放上来

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct treenode *treepoint;
typedef struct treenode 
{
    char ch;
    treepoint l;
    treepoint r;
};
treepoint creat()
{
    treepoint bt;
    bt = (treepoint)malloc(sizeof(treenode));
    char x;
    scanf("%c", &x);
    if (x == '#')
    {
        bt = NULL;
    }
    else
    {
        bt->ch = x;
        bt->l = creat();
        bt->r = creat();
    }
    return bt;
}
treepoint que[1000];
int front;
int rear;
void creatq()
{
    memset(que, 0, sizeof(treepoint));
    front = -1;
    rear = -1;
}
void qpush(treepoint temp)
{
    if (rear >= 1000)
        printf("isfull\n");
    que[++rear] = temp;
}
treepoint qpop()
{
    return que[++front];
}
int main()
{
    creatq();
    treepoint ptr;
    ptr = (treepoint)malloc(sizeof(treenode));
    ptr=creat();
    if (ptr)
        qpush(ptr);
    for (;;)
    {
        if (ptr&&(front!=rear))
        {
            ptr = qpop();
            printf("%c", ptr->ch);
            if (ptr->l)
                qpush(ptr->l);
            if (ptr->r)
                qpush(ptr->r);
        }
        else
            break;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/dpdpd/article/details/79996790
今日推荐