数据结构之单向链表 UVa11988

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdlib>
 4 
 5 using namespace std;
 6 
 7 struct Node
 8 {
 9     char c;
10     Node* next;
11     Node(){next=NULL;}
12 };
13 
14 char arr[100000];
15 
16 int main()
17 {
18     while(cin>>arr)
19     {
20         int len=strlen(arr);
21         Node* head=new Node();
22         Node* cur=head;
23         Node* last=head;
24         for(int i=0;i<len;i++)
25         {
26             if(arr[i]=='[')
27             {
28                 cur=head;
29             }
30             else if(arr[i]==']')
31             {
32                 cur=last;
33             }
34             else
35             {
36                 Node* a=new Node();
37                 a->c=arr[i];
38                 a->next=cur->next;
39                 cur->next=a;
40                 if(last==cur)
41                 {
42                     last=a;
43                     last->next=NULL;
44                 }
45                 cur=a;
46             }
47         }
48         Node* t=head->next;
49         while(t!=NULL)
50         {
51             cout<<(*t).c;
52             t=t->next;
53      //       system("pause");
54         }
55         cout<<endl;
56     }
57     return 0;
58 }
View Code

 

posted @ 2015-08-07 14:07  相儒以沫  阅读(143)  评论(0编辑  收藏  举报