64,682
社区成员
发帖
与我相关
我的任务
分享
1. class CListNode
2. {
3. public:
4. CListNode();
5. CListNode(int i);
6. int m_Data;
7. CListNode *m_Next;
8. };
9.
10. CListNode::CListNode():m_Data(0), m_Next(0)
11. {
12.
13. }
14.
15. CListNode::CListNode(int i):m_Data(i), m_Next(0)
16. {
17.
18. }
19.
20.
21. class CList
22. {
23. public:
24. CList();
25. CList(int i);
26. CList(CList *pNew);
27. ~CList();
28.
29. int NextNode();
30. void _InsertAfter(int iNew);
31. void _DeleteAfter();
32. void _DeleteFront();
33. void _PrintData();
34.
35. CListNode m_Head;
36.
37. private:
38. CListNode *c_m_Head;
39. int m_iCount;
40. protected:
41.
42. };
43.
44. CList::CList()
45. {
46. m_Head.m_Next = NULL;
47. c_m_Head = &m_Head;
48. cout<<"1:"<<&m_Head<<endl;
49. }
50. CList::CList(int i)
51. {
52. _InsertAfter(i);
53.
54. }
55.
56. CList::~CList()
57. {
58. m_Head = *c_m_Head;
59. CListNode *p;
60.
61. while( m_Head.m_Next!=NULL )
62. {
63. p = m_Head.m_Next;
64. m_Head.m_Next = p->m_Next;
65. delete p;
66. }
67.
68. }
69.
70. void CList::_InsertAfter(int iNew)
71. {
72. CListNode *p= new CListNode(iNew);
73.
74. p->m_Next = m_Head.m_Next;
75. m_Head.m_Next = p;
76.
77. }
78.
79. void CList::_DeleteFront()
80. {
81.
82. }
83.
84. void CList::_DeleteAfter()
85. {
86. CListNode *p;
87. p = m_Head.m_Next;
88. m_Head.m_Next = p->m_Next;
89. delete p;
90. }
91.
92. void CList::_PrintData()
93. {
94. m_Head = *c_m_Head;
95. while( m_Head.m_Next!=NULL )
96. {
97. m_Head = *(m_Head.m_Next);
98. cout<<m_Head.m_Data<<endl;
99.
100. }
101. }
102.
103.
104.
105. int main()
106. {
107. CList *clist = new CList;
108. int i;
109. for(i=0; i<10; i++)
110. {
111. clist->_InsertAfter(i);
112. }
113.
114. clist->_PrintData();
115. delete clist;
116. return 0;
117. }