通过以下程序实现创建一个长度为10的链表,其中链表的元素是0-9.并且打印出来。
//#include"stdafx.h"
#include<iostream>
#include<cstring>
#include<vector>
#include<assert.h>
using namespace std;
struct ListNode
{
int m_key;
ListNode* next;
};
void createList1(ListNode* pHead)
{
ListNode* p=pHead;//指向头节点的指针
for(int i=1;i<10;i++)
{
ListNode*pNewNode=new ListNode;
pNewNode->m_key=i;
pNewNode->next=NULL;
p->next=pNewNode;
p=pNewNode;
}
}
int main()
{
ListNode*head=NULL;
head=new ListNode;
head->m_key=0;
head->next=NULL;
createList1(head);
while(head!=NULL)
{
cout<<head->m_key<<endl;
head=head->next;
}
}