反转链表

 1 #include<iostream>
 2 #include<vector>
 3 
 4 using namespace std;
 5 
 6 struct Node
 7 {
 8     int key;
 9     Node* L_Next;
10 };
11 Node* ReverseList(Node *List)
12 {
13     Node* Reverse_L = NULL;
14     Node* PNode = List->L_Next;
15     Node* PPrev = NULL;
16     while (PNode != NULL)
17     {
18         Node* Pnext = PNode->L_Next;
19         if (Pnext == NULL)
20         {
21             Reverse_L = PNode;
22         }
23         PNode->L_Next = PPrev;
24         PPrev = PNode;
25         PNode = Pnext;
26     }
27     return Reverse_L;
28 }
29 
30 int main(void)
31 {
32 
33     Node* L = new Node;
34     L->L_Next = NULL;
35     Node* p;
36     p = L;
37     vector<int>vec;
38     int a;
39     char temp;
40     while (temp = cin.get() != '\n')
41     {
42         cin.unget();
43         cin >> a;
44         vec.push_back(a);
45     }
46     for (int i = 0; i < vec.size(); i++)
47     {
48         Node* q = new Node;
49         q->key = vec[i];
50         q->L_Next = NULL;
51         //q->L_Next = L->L_Next;
52         //L->L_Next = q;
53         while (p->L_Next != NULL)
54         {
55             p = p->L_Next;
56         }
57         p->L_Next = q;
58     }
59 
60     p = L;
61     while (p->L_Next != NULL)
62     {
63         p = p->L_Next;
64         cout << p->key<<' ';
65     }
66     L=ReverseList(L);
67     cout << endl;
68     p = L;
69     while (p != NULL)
70     {
71         cout << p->key << ' ';
72         p = p->L_Next;
73     }
74     return 0;
75 
76 }

 

posted @ 2015-10-11 20:57  hhboboy  阅读(153)  评论(0编辑  收藏  举报