双向链表 (szl测试txt)

要写子程序的delete  和insert    要不会很乱。

1.void  delete(int p)

{

    v[v[p].last].next=v[p].next;

    v[v[p].next].last=v[p].last;

}

 

2.void  insert(int q,int p)   q后插入p

{

int o=v[q].next;

v[p].last=q;

v[q].next=p;

v[p].next=o;

v[o].last=p;//顺序都无所谓

}

 1 #include<iostream>
 2 #include<cstring> 
 3 using namespace std;
 4 char txt[500000];
 5 struct node{
 6     int next;int last;char le;
 7 }link[500000];
 8 void shanchu(int p)
 9 {
10     link[link[p].last].next=link[p].next;
11     link[link[p].next].last=link[p].last;
12 }
13 void insert(int q,int p/*插入*/)  //xx  (link[q].next)
14 {
15     int o=link[q].next;
16     link[p].last=q;
17     link[q].next=p;
18     link[p].next=o;
19     link[o].last=p;
20 } 
21 int main()
22 {
23     int len,i,pos;
24     scanf("%s",txt+1);
25     len=strlen(txt+1);
26     int head=500005,tail=500006;//我就当我懂了吧。。
27     pos=head;
28     for(i=1;i<=len;i++)
29     {
30         if(txt[i]>='a'&&txt[i]<='z')
31         {
32             link[i].le=txt[i];
33             insert(pos,i); 
34             pos=link[pos].next;
35         }
36         if(txt[i]=='L')
37         {
38             if(pos!=head)  pos=link[pos].last;
39         }
40         if(txt[i]=='R')
41         {
42             if(link[pos].next!=tail)  pos=link[pos].next;
43         }
44         if(txt[i]=='B')
45         {
46             if(pos!=head)
47             
48             {
49                shanchu(pos);
50                pos=link[pos].last;
51             }
52         }
53     }
54     for(i=link[head].next;i!=tail;i=link[i].next)
55     printf("%c",link[i].le);
56     return 0;
57 }

 

posted on 2016-03-21 00:30  babyyang  阅读(173)  评论(0编辑  收藏  举报