uva 11988 - Broken Keyboard (a.k.a. Beiju Text)----链表快速插入

本文介绍了一种使用链表解决UVA11988题目(Broken Keyboard)的方法,通过实现链表节点的快速插入操作来模拟键盘输入行为,并最终输出文本内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  main.cpp
//  uva 11988 - Broken Keyboard (a.k.a. Beiju Text)----链表快速插入
//
//  Created by XD on 15/9/5.
//  Copyright (c) 2015年 XD. All rights reserved.
//
/*
 此题就是一道简单的链表题。注意一下当前的位置,起始位置和结束位置。
 */

#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<vector>
#include <string.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cstdio>
using namespace std ;
struct node{
    int key ;
    node* pre;
    node* nxt ;
};
node* getNode(int key)
{
    node* ret = (node*)malloc(sizeof(node)) ;
    ret->key = key ;
    return ret ;
}
node *header,*stop,*current ;
void insert(node* n1,node*n2,node* n3)
{
    n1->nxt = n3 ;
    n3->nxt = n2 ;
    n2->pre = n3 ;
    n3->pre = n1 ;
    current = n3 ;
}
void init()
{
    header = getNode(-1) ;
    stop = getNode(-1) ;
    header->pre = NULL ;
    header->nxt = stop ;
    stop->nxt = NULL ;
    stop->pre = header ;
    current = header ;
}
int main(int argc, const char * argv[]) {
    char s[100010] ;
    while (scanf("%s" , s) == 1) {
        init() ;
        int len = (int)strlen(s) ;
        for (int i = 0; i < len; i++) {
            if (s[i] == '[') {
                current = header ;
            }
            else if(s[i] == ']')
            {
                current=stop->pre ;
            }
            else{
                insert(current, current->nxt,getNode(s[i])) ;
            }
        }
        header = header->nxt ;
        while (header->key!=-1) {
            printf("%c",header->key) ;
            header = header->nxt ;
        }
        printf("\n") ;
    }
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值