C++学习 1单向链表建立(内容来自chixujohnny)

  1. #include <iostream>  
  2. using namespace std;  
  3. /* 创建一个单链表 */  
  4. struct ListNode{  
  5.     int m_key;  
  6.     ListNode* next;  
  7. };  
  8. void createList(ListNode* pHead){  
  9.     ListNode* p = pHead;  
  10.     for (int i = 1; i < 10; ++i) {  
  11.         ListNode* pNewNode = new ListNode;  
  12.         pNewNode->m_key = i; // 将新节点的值赋值为i  
  13.         pNewNode->next = NULL;  
  14.         p->next = pNewNode; // 上一个节点指向这个新建立的节点  
  15.         p = pNewNode; // p节点指向这个新的节点  
  16.     }  
  17. }  
  18. int main(){  
  19.     ListNode* head = NULL;  
  20.     head = new ListNode;  
  21.     head->m_key = 0;  
  22.     head->next = NULL;  
  23.     createList(head);  
  24.     system("pause");
  25.     return 0;  
  26. }  

猜你喜欢

转载自blog.csdn.net/sannianyihoushuma/article/details/79749316
今日推荐