ACM篇:Uva 11988-- Broken Keyboard

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int MAX = 100000;

char text[MAX+2];

struct Point
{
    int val;
    Point *next;
};
Point *head = (Point *)(malloc(sizeof(Point)));

void list_set()
{
    head->next = NULL;
    Point *cur = head;
    Point *last = head;
    for (char *ptr = text; *ptr; ptr++)
    {
        if (*ptr == '[')
            cur = head;
        else if (*ptr == ']')
            cur = last;
        else
        {
            Point *temp = (Point *)(malloc(sizeof(Point)));
            temp->val = *ptr;
            temp->next = cur->next;
            cur->next = temp;
            cur = cur->next;
            if (!cur->next)
                last = cur;
        }
    }   
}

void list_print()
{
    Point *cur = head->next;
    Point *temp = NULL;
    while (cur)
    {
        putchar(cur->val);
        temp = cur;
        cur = cur->next;
        free(temp);
    }   
    putchar('\n');
}
int main()
{   
    while (scanf("%s", text) != EOF)
    {
        list_set();
        list_print();   
    }
    return 0;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章