struct ListNode
{
int m_nKey;
ListNode* m_pNext;
};
链表的创建和打印,不说了,放到这里:
ListNode *Head = new ListNode;
ListNode *pNode=NULL;
pNode =Head;
//建立链表并赋初值;
for (int i = 0; i < length; i++)
{
cin >> pNode->m_nKey;//给每个结点赋值;
<span style="color:#ff0000;">//重新创建下一个链表结点;
pNode->m_pNext = new ListNode;
pNode=pNode->m_pNext;</span>
}
pNode->m_pNext = NULL;<span style="color:#ff0000;">//最后为指针指向NULL;千万别忘记!</span>
//打印链表;
pNode = Head;
while (pNode->m_pNext != NULL)
{
printf("%d ", pNode->m_nKey);
pNode = pNode->m_pNext;
}