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 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等