#include <iostream> using namespace std; //链表结构体 struct ListNode { int m_Value; ListNode *next; }; //创建一个单链表 ListNode *CreateList(int *a,int n) { ListNode *pHead = NULL; ListNode *pTemp = NULL; int i = 0; for(i = 0; i < n; i++) { //ListNode *pNew = (ListNode *)malloc(sizeof(struct ListNode)); ListNode *pNew = new ListNode(); //创建一个新的节点 pNew->m_Value = a[i]; pNew->next = NULL; if(NULL == pHead) { pHead = pNew; pTemp = pNew; } else { pTemp->next = pNew; pTemp = pTemp->next; } } return pHead; } //链表尾部插入一个节点 void AddTailNode(ListNode **pHead,int key) { // ListNode *pNew = (ListNode *)malloc(sizeof(struct ListNode)); ListNode *pNew = new ListNode(); pNew->m_Value = key; pNew->next = NULL; /*if(NULL == pHead) { return; }*/ if(NULL == *pHead) { *pHead = pNew; return; } ListNode *p = *pHead; ListNode *q = NULL; while(NULL != p) { q = p; p = p->next; } q->next = pNew; } int main(void) { int a[5] = {3,6,8,2,1}; ListNode *p = CreateList(a,5); //ListNode *p = NULL; AddTailNode(&p,4); while(NULL != p) { cout<<p->m_Value; p = p->next; } return 0; }