简单的双向链表题目(可是T哭了!!)(这个题目给自己提个醒吧)

答案:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct node
{
    char data;
    struct node *last;
    struct node *next;
};
char str[1000005];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int i;
        struct node *head,*p,*tail,*q;
        head = (struct node *)malloc(sizeof(struct node));
        head->last = NULL;
        head->next = NULL;
        scanf("%s",str);
        tail = head;
        for(i=0; str[i]; i++)
        {
            if(str[i]=='<')
            {
                if(tail->last)
                    tail = tail->last;
            }
            else if(str[i]=='>')
            {
                if(tail->next)
                    tail = tail->next;
            }
            else if(str[i]=='-')
            {
                if(tail->last)
                {
                    q = tail;
                    tail = tail->last;
                    tail->next = q->next;
                    if(tail->next)
                        q->next->last = tail;///当时少了这句话,很难受,其实链表有个特点:(很简单)但是(错一点都不行+错任何地方都很难找BUG!)
                    q->last = NULL;
                    q->next = NULL;
                    free(q);
                }
            }
            else
            {
                p = (struct node *)malloc(sizeof(struct node));
                p->data = str[i];
                p->next = tail->next;
                if(tail->next!=NULL)
                {
                    tail->next->last = p;
                }
                tail->next = p;
                p->last = tail;
                tail = p;
            }
        }
        p = head->next;
        while(p)
        {
            printf("%c",p->data);
            p = p->next;
        }
        printf("\n");
    }
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/ACMerdsb/article/details/82191248