利用类实现链表的创建
1 #include<iostream> 2 3 using namespace std; 4 5 class Link { 6 public: 7 Link() {} 8 Link *_pNext; 9 void Create(); 10 void Output(); 11 int GetElement(); 12 private: 13 int _id; 14 int _cnt; 15 }; 16 17 void Link::Create() { 18 cin >> this->_id; 19 } 20 21 void Link::Output() { 22 cout << this->_id; 23 } 24 25 int Link::GetElement() { 26 return this->_id; 27 } 28 29 int main() { 30 Link *pLink = new Link; 31 Link *pHead = pLink; 32 bool isfull = false; 33 34 while (!isfull) { 35 Link *data = new Link; 36 data->Create(); 37 if (data->GetElement() != 0) { 38 data->_pNext = NULL; 39 pHead->_pNext = data; 40 pHead = pHead->_pNext; 41 } 42 else { 43 isfull = true; 44 } 45 } 46 47 pLink = pLink->_pNext; 48 49 while (pLink != 0) { 50 pLink->Output(); 51 pLink = pLink->_pNext; 52 } 53 54 return 0; 55 }