C++建立及销毁链表

 struct ListNode {
	 int val;
	 ListNode *next;
	 ListNode(int x) : val(x), next(NULL) {}
 };

 void createList(ListNode* &pHead){
	 pHead = new ListNode(-1);
	 int length = 5;
	 ListNode *p = pHead;
	 for (size_t i = 0; i < length; i++)
	 {
		 ListNode *node = new ListNode(i);

		 p->next = node;
		 p = node;
	 }		
 }

 void destroyList(ListNode* pHead){
	 _ASSERT(pHead != NULL);

	 ListNode *pNext = pHead->next;
	 while (pNext!=NULL)
	 {
		 delete pHead;
		 pHead = pNext;
		 pNext = pHead->next;
	 };

	 delete pHead;
	 pHead = NULL;

	 


 }

posted @ 2016-07-13 10:39  牧羊少年10号  阅读(434)  评论(0编辑  收藏  举报