数据结构难题

瑞格课内实验实验二——8568(用队列打印杨辉三角)

在这里插入图片描述

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

#define MAXQSIZE 200
typedef int QElemType;
typedef struct {
    QElemType  *base;
    int front;
    int rear;
}SqQueue;
void InitQueue(SqQueue *Q){
    //构造一个空队列Q
    Q->base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType));
    if(!Q->base)exit(1);//exit(1)表示直接退出程序
    Q->front=Q->rear=0;
}
int QueueLength(SqQueue *Q){
    //返回Q的元素个数,即队列的长度
    int e;
    e=(Q->rear-Q->front+MAXQSIZE)%MAXQSIZE;
    return e;
}
void EnQueue(SqQueue *Q,QElemType e){
    //插入的元素e为Q的新的队尾元素
    if((Q->rear+1)%MAXQSIZE ==Q->front) exit(1);
    Q->base[Q->rear]=e;
    Q->rear=(Q->rear+1)%MAXQSIZE;
}
void DeQueue(SqQueue *Q){
    //若队列不空,则删除Q的队头元素,用e返回其值
    if(Q->front==Q->rear)
        exit(1);
    //e=Q.base[Q.front];
    Q->front=(Q->front+1)%MAXQSIZE;
}
QElemType GetHead(SqQueue *Q){
    //返回队头元素
    return Q->base[Q->front];
}
int main(){
    int N,n,c;
    QElemType t,x;
    SqQueue f,*Q;
    Q=&f;
    InitQueue(Q);//建造一个空队列,采用的是顺序结构
    printf("请输入杨辉三角规模:\n");
    scanf("%d",&N);
    EnQueue(Q,1);
    for(n=2;n<=N;n++){
        EnQueue(Q,1);
        for(c=1;c<=n-2;c++){
            t=GetHead(Q);
            DeQueue(Q);
            printf("%4d",t);
            x=GetHead(Q);
            t=t+x;
            EnQueue(Q,t);   
        }
        EnQueue(Q,1);
        printf("%4d",GetHead(Q));
        DeQueue(Q);
        printf("\n");
    }
    while(Q->front!=Q->rear){
            printf("%4d",GetHead(Q));
            DeQueue(Q);
        }
    return 0;
    }

输出
在这里插入图片描述

瑞格课内实验实验二——8569(利用栈实现表达式求值)

在这里插入图片描述
在这里插入图片描述

瑞格课内实验实验三——7078(中序线索化)

在这里插入图片描述

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

typedef char ElemType;
typedef struct BiNode
{
    ElemType data;
    struct BiNode *lchild,*rchild;
    int rtag,ltag;
}BiNode;
BiNode *pre;

BiNode * CreateBiTree()
{
    BiNode *T;
    ElemType ch;
    scanf("%c",&ch);
    if(ch=='@')
    {
        T=NULL;
    }
    else
    {
        T=(BiNode *)malloc(sizeof(BiNode));
        T->data=ch;
        T->lchild=T->rchild=NULL;
        T->ltag=T->rtag=0;
        T->lchild=CreateBiTree();
        T->rchild=CreateBiTree();
    }
    return T;
}

void InThreading(BiNode *T)//对树中以任意节点T为根的子树进行中序线索化
{
    if(T!=NULL)
    {
        InThreading(T->lchild);
        if(T->lchild==NULL)
        {
            T->ltag=1;
            T->lchild=pre;
        }
        if(pre->rchild==NULL)
        {
            pre->rtag=1;
            pre->rchild=T;
        }
        pre=T;
        InThreading(T->rchild);
    }
}

BiNode * InOrderThreading(BiNode *T)
{
    BiNode *Thrt;
    Thrt=(BiNode *)malloc(sizeof(BiNode));//建立一个头节点,其地址为Thrt;
    Thrt->ltag=0;//头节点标记分别初始化;
    Thrt->rtag=1;
    Thrt->rchild=Thrt;//初始化时,右指针指向自己
    if(!T)//如果树为空,左指针也指向自己
        Thrt->lchild=Thrt;
    else
    {
        Thrt->lchild=T;
        pre=Thrt;//头节点左孩子指向根,pre初值指向头节点
        InThreading(T);//调用算法,对以T为根的二叉树进行中序线索化
        pre->rchild=Thrt;//算法结束后,pre为最右节点,其右线索指向头节点
        pre->rtag=1;//将pre的右标记变为1,因为头节点相当于pre节点的后继
        Thrt->rchild=pre;//头结点的右线索指向pre
    }
    return Thrt;//将带头结点的中序线索二叉树地址返回
}

void InOrderTraverseThr(BiNode *Thrt)
{
    BiNode *T;//T指向根节点,根节点是头结点的左孩子;
    T=Thrt->lchild;//空树或遍历结束时
    while(T!=Thrt)
    {
        while(T->ltag==0)
        {
            T=T->lchild;//沿着左孩子一直向下找
        }
        printf("%c",T->data);//访问左子树为空的节点(最左点)
        while(T->rtag==1&&T->rchild!=Thrt)//如果有右线索,沿着右线索一直访问后继节点
        {
            T=T->rchild;
            printf("%c",T->data);
        }
        T=T->rchild;//没有右线索就找右孩子
    }
}

int main()
{
    BiNode *T,*Thrt;//Thrt是加上头节点后的树的头节点的地址
    T=CreateBiTree();//通过中序建立二叉树,返回建立的二叉树的根节点的地址。
    Thrt=InOrderThreading(T);//将建立的二叉树T加上根节点Thrt,并且对加上头结点的二叉树进行中序线索化。返回头结点的地址。
    InOrderTraverseThr(Thrt);//对带头结点的中序线索二叉树进行中序遍历。
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值