sgu 397

来源:互联网 发布:淘宝店铺图片描述尺寸 编辑:程序博客网 时间:2024/06/01 07:26

手写链表

#include<cstdio>#include<cstring>using namespace std;char s;int tem,head,nex,last;struct word{    char c;    int next;    int last;}seq[1000100];int main(){    int i,f=1;    //freopen("a.txt","r",stdin);    //freopen("b.txt","w",stdout);    while(scanf("%c",&s)!=EOF){        if(s=='\n'){            if(tem==0)continue;            tem=head;            while(tem!=-1){                printf("%c",seq[tem].c);                tem=seq[tem].next;            }            printf("\n");        }        if(s=='\n'||f){            tem=0;last=-1;head=0;nex=-1;            f=0;            memset(seq,0,sizeof(seq));        }        if(s=='\n')continue;        if(s!='L' && s!='R'){            seq[tem].c=s;            seq[tem].last=last;            seq[tem].next=nex;            if(last!=-1)                seq[last].next=tem;            else if(last==-1)                head=tem;            if(nex!=-1)                seq[nex].last=tem;            tem++;            seq[tem].last=tem-1;            last=tem-1;        }        else if(s=='L'){            if(last==-1)continue;            nex=last;            last=seq[last].last;        }        else if(s=='R'){            if(nex==-1)continue;            last=nex;            nex=seq[nex].next;        }    }}

用stl的list

#include <cstdio>#include <cstring>#include <list>using namespace std;char org[1000001];int main(void) {    list<char> word;    scanf ("%s", org);    int i;    int len = strlen(org);    list<char>::iterator it = word.begin();    for (i = 0; i < len; ++i) {        if (org[i] == 'L') {            if (it != word.begin()) {                --it;            }        } else if (org[i] == 'R') {            if (it != word.end()) {                ++it;            }        } else {            word.insert(it, org[i]);        }    }    for (it = word.begin(); it != word.end(); ++it) {        printf ("%c", *it);    }    printf ("\n");    return 0;}



原创粉丝点击