m0_62746615 2022-10-30 17:14 采纳率: 85%
浏览 148
已结题

C语言,双向循环链表

img


输入格式:
输入一个字符串,长度不超过5×104,包含大小写字母、空格、下划线、{、}、<、>、#,表示贝蒂和哈利的按键序列。
输出格式:
输出为屏幕上最终显示的字符串。
输入样例1:
jlu_cc{i_love_}st
输出样例1:
i_love_jlu_ccst

  • 写回答

2条回答 默认 最新

  • 三块不一样的石头 2022-10-30 19:08
    关注
    #include<stdio.h>
    #include<stdlib.h>
    struct Queue{
        char data;
        struct Queue *last,*next;
    }head,tail,*now;
    
    void input(char ch);
    void deleteNode();
    int main()
    {
        head.next = &tail;
        tail.last = &head;
        tail.data = '\0';
        now = &head;
    
        char s[100001];
        gets(s);
        for(int z=0;s[z]!='\0';z++){
            if(s[z]=='{') now = &head;
            if(s[z]=='}') now = tail.last;
            if(s[z]=='<' && now!=&head) now = now->last;
            if(s[z]=='>' && now!=tail.last) now = now->next;
            if(s[z]=='#') deleteNode();
            input(s[z]);
        }
    
        head = *head.next;
        while (head.data!='\0'){
            putchar(head.data);
            head = *head.next;
        }
    
        return 0;
    }
    void input(char ch){
        if(ch=='{' || ch=='}' || ch=='<' || ch=='>' || ch=='#') return;
        struct Queue *node;
        node = (Queue *)malloc(sizeof(struct Queue));
        node->data = ch;
        node->next = now->next;
        now->next->last = node;
        node->last = now;
        now->next = node;
        now = now->next;
    }
    void deleteNode(){
        if(now==&head) return;
        struct Queue *flag = now;
        now = now->last;
        now->next = flag->next;
        flag->next->last = now;
        free(flag);
    }
    

    img

    #include<stdio.h>
    #include<stdlib.h>
    struct Queue{
        char data;
        struct Queue *last,*next;
    }head,tail,*now;
    
    void input(char ch);
    void deleteNode();
    int main()
    {
        head.next = &tail;
        tail.last = &head;
        tail.data = '\0';
        now = &head;
    
        char s;
    
    
        while (scanf("%c",&s) && s!='\n'){
            if(s=='{') now = &head;
            if(s=='}') now = tail.last;
            if(s=='<' && now!=&head) now = now->last;
            if(s=='>' && now!=tail.last) now = now->next;
            if(s=='#') deleteNode();
            input(s);
        }
    
        head = *head.next;
        while (head.data!='\0'){
            putchar(head.data);
            head = *head.next;
        }
    
        return 0;
    }
    void input(char ch){
        if(ch=='{' || ch=='}' || ch=='<' || ch=='>' || ch=='#') return;
        struct Queue *node;
        node = (struct Queue *)malloc(sizeof(struct Queue));
        node->data = ch;
        node->next = now->next;
        now->next->last = node;
        node->last = now;
        now->next = node;
        now = now->next;
    }
    void deleteNode(){
        if(now==&head) return;
        struct Queue *flag = now;
        now = now->last;
        now->next = flag->next;
        flag->next->last = now;
        free(flag);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月1日
  • 已采纳回答 10月30日
  • 创建了问题 10月30日

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示