您的位置:首页 > 其它

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

2015-09-05 23:27 309 查看
//
//  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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: