1
#include
<
iostream
>
2 #include < cstdlib >
3
4 using namespace std;
5
6 class Node
7 {
8 private:
9 int data;
10 Node* next;
11 public:
12 friend class List;
13 Node(int theValue)
14 {
15 data = theValue;
16 next = 0;
17 }
18
19} ;
20
21 class List
22 {
23 public:
24 List()
25 {
26 first = 0;
27 end = 0;
28 }
29 ~List()
30 {
31 Node* next;
32 while (first)
33 {
34 next = first->next;
35 delete first;
36 first = next;
37 }
38 }
39
40
41 //增加item,从屁股后面插入 ^_^
42 void Add(int value)
43 {
44 if (first == 0)
45 {
46 first = end = new Node(value);
47 }
48 else
49 {
50 end->next = new Node(value);
51 end = end->next;
52 }
53 }
54
55 //反转List,从前面插入(原有的item,不要想歪了 ^_^)
56 void Reverse()
57 {
58 if (first == 0)
59 return;
60
61 Node* newFirst = first;//临时变量,保存新的first.
62 end = first;//反转后,first 刚好和end 互换.
63 Node* nextOfFirst;//辅助变量,其值始终为first的下一个成员,即first->next.
64
65 first = first->next;
66 while (first)
67 {
68 nextOfFirst = first->next;
69
70 first->next = newFirst;
71 newFirst = first;
72
73 first = nextOfFirst;
74 }
75
76 first = newFirst;
77 end->next = 0;
78
79 }
80
81 //显示List的所有成员
82 void Display()
83 {
84 if (first == 0)
85 {
86 cout << "The List has no items." << endl;
87 return;
88 }
89 else
90 {
91 cout << "The List's items:" << endl;
92 }
93
94 Node* item = first;
95 while (item)
96 {
97 cout << item->data << " ";
98 item = item->next;
99 }
100
101 cout << endl;
102 }
103
104 private:
105 Node* first;
106 Node* end;
107
108} ;
109
110
111 int main()
112 {
113 List theList;
114 for (int i = 0; i < 10; i++)
115 theList.Add(i);
116
117 //显示其初始值
118 cout << "List 的原始值为:" <<endl;
119 theList.Display();
120
121
122 //反转,并显示其值
123 cout << endl <<endl
124 <<"List 反转之后的值为:" << endl;
125 theList.Reverse();
126 theList.Display();
127
128 system("pause");
129
130}
2 #include < cstdlib >
3
4 using namespace std;
5
6 class Node
7 {
8 private:
9 int data;
10 Node* next;
11 public:
12 friend class List;
13 Node(int theValue)
14 {
15 data = theValue;
16 next = 0;
17 }
18
19} ;
20
21 class List
22 {
23 public:
24 List()
25 {
26 first = 0;
27 end = 0;
28 }
29 ~List()
30 {
31 Node* next;
32 while (first)
33 {
34 next = first->next;
35 delete first;
36 first = next;
37 }
38 }
39
40
41 //增加item,从屁股后面插入 ^_^
42 void Add(int value)
43 {
44 if (first == 0)
45 {
46 first = end = new Node(value);
47 }
48 else
49 {
50 end->next = new Node(value);
51 end = end->next;
52 }
53 }
54
55 //反转List,从前面插入(原有的item,不要想歪了 ^_^)
56 void Reverse()
57 {
58 if (first == 0)
59 return;
60
61 Node* newFirst = first;//临时变量,保存新的first.
62 end = first;//反转后,first 刚好和end 互换.
63 Node* nextOfFirst;//辅助变量,其值始终为first的下一个成员,即first->next.
64
65 first = first->next;
66 while (first)
67 {
68 nextOfFirst = first->next;
69
70 first->next = newFirst;
71 newFirst = first;
72
73 first = nextOfFirst;
74 }
75
76 first = newFirst;
77 end->next = 0;
78
79 }
80
81 //显示List的所有成员
82 void Display()
83 {
84 if (first == 0)
85 {
86 cout << "The List has no items." << endl;
87 return;
88 }
89 else
90 {
91 cout << "The List's items:" << endl;
92 }
93
94 Node* item = first;
95 while (item)
96 {
97 cout << item->data << " ";
98 item = item->next;
99 }
100
101 cout << endl;
102 }
103
104 private:
105 Node* first;
106 Node* end;
107
108} ;
109
110
111 int main()
112 {
113 List theList;
114 for (int i = 0; i < 10; i++)
115 theList.Add(i);
116
117 //显示其初始值
118 cout << "List 的原始值为:" <<endl;
119 theList.Display();
120
121
122 //反转,并显示其值
123 cout << endl <<endl
124 <<"List 反转之后的值为:" << endl;
125 theList.Reverse();
126 theList.Display();
127
128 system("pause");
129
130}