数据结构——火车车厢重排

话不多说,直接上代码。

入轨和出轨使用的都是栈,缓冲轨用的是队列。

#include <stdio.h>
#include <stdlib.h>
#define size 6
//要求:实现有三个缓冲轨得火车重排算法,使得无序得火车车厢,重排为小号车厢在前端,大号车厢在后端得有序序列
//定义五个队列,一个入,一个出,三个缓冲
//q1表示入轨,q2表示出轨,q3~5表示缓冲轨 
typedef struct list{
    int data;
    struct list*next;
}list;

typedef struct queue{
    int data[size];
    int front,rear;
}queue;

typedef struct stack{
    list *top;
    list *bottom;
}stack;

//初始化队列函数
queue *initq(queue*q){
    q=(queue*)malloc(sizeof(queue));
    if(!q){
        printf("初始化失败");
    }
    q->front=q->rear=0;
    return q;

//初始化栈函数 
stack *inits(stack*s){
    s=(stack*)malloc(sizeof(stack));
    s->bottom=(list*)malloc(sizeof(list));
    if(s->bottom==NULL){
        printf("初始化失败");
    }
    s->top=s->bottom;
    s->bottom->next=NULL;
    return s;
}

//入栈
void ens(stack*s,int e){
    list*p=(list*)malloc(sizeof(list));
    p->data=e;
    p->next=s->top;
    s->top=p;

//入轨的输入函数
void in(stack*s){
    int i=0;
    int e=0;
    for(i=0;i<5;i++){
        scanf("%d",&e);
        ens(s,e);
    }
}

//入队列函数
void enq(queue*q,int e){
    q->data[q->front]=e;
    q->front+=1;

//出队列函数
int deq(queue*q){
    int e=0;
    e=q->data[q->rear];
    q->rear=q->rear+1;
    return e;

//换轨 
void tobu(queue*q1,stack*q3){//q1去q3
    int e=0;
    e=deq(q1);
    ens(q3,e);

//出栈
int des(stack*s){
    list*p;
    p=s->top;
    int e=0;
    e=p->data;
    s->top=s->top->next;
    free(p);
    return e;

//打印队列 
void printq(queue*q){
    int e=0;
    while(q->front!=q->rear){
        e=deq(q);
        printf("%d",e);
    }
    printf("\n");
}

//打印栈
void prints(stack*s){
    int e=0;
    while(s->top!=s->bottom){
        e=des(s);
        printf("%d ",e);
    }

//判断队列是否为空
int empty(queue*q){
    if(q->front==q->rear){
        return 1;
    }
    else return 0;

//判断队列是否为空
int emptys(stack*s){
    if(s->top==s->bottom){
        return 1;
    }
    else{
        return 0;
    }

//主函数
int main(int argc,char*argv[]){
    queue *q3,*q4,*q5;
    stack *s1,*s2;
    s1=inits(s1);
    s2=inits(s2);
    q3=initq(q3);
    q4=initq(q4);
    q5=initq(q5);
    in(s1);//入轨
    int e=0;//接受q1的出轨
    int k=0;
    while(s1->top!=s1->bottom){
        e=des(s1);
        if(e==k+1){
            ens(s2,e);
            k++;
            continue;
        }
        //三条缓冲轨都为空 
        if(empty(q3)&&empty(q4)&&empty(q5)){
            enq(q3,e);
            continue;
        }
        else if(e>q3->data[q3->front-1]){
            enq(q3,e);
            continue;
        }
        else if(empty(q4)&&empty(q5)){
            enq(q4,e);
            continue;
        }
        else if(e>q4->data[q4->front-1]){
            enq(q4,e);
            continue;
        }
        else if(empty(q5)){
            enq(q5,e);
            continue;
        }
        else if(e>q5->data[q5->front-1]){
            enq(q5,e);
            continue;
        }
        else if(!empty(q3)&&!empty(q4)&&!empty(q5)&&!emptys(s1)){
            printf("重排失败");
            return 0;
        }
    } 
    //此处缓冲轨已满
    while(k!=5){
        if(q3->data[q3->rear]==k+1){
            tobu(q3,s2);
            k++;
            //printq(q3);
            //printf("%d ",k);
            continue;
        }
        else if(q4->data[q4->rear]==k+1){
            tobu(q4,s2);
            k++;
            //printq(q4);
            //printf("%d ",k);
            continue;
        }
        else if(q5->data[q5->rear]==k+1){
            tobu(q5,s2);
            k++;
            //printq(q5);
            //printf("%d ",k);
            continue;
        }
    }
    prints(s2);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值