设为首页 加入收藏

TOP

面试题11:在O(1)时间删除链表结点 (二)
2015-11-21 01:27:48 来源: 作者: 【 】 浏览:12
Tags:试题 时间 删除 结点
lete pToBeDeleted; pToBeDeleted = NULL; } } else { cout << "链表为空!" << endl; } } //创建一个链表,输入从头到尾结点的值,输入-1表示结束 void CreateList(ListNode *& pHead) { ListNode *pListNode = NULL; ListNode *pCurLastNode = NULL; bool isHead = true; while (1) { if (isHead) { pHead = new ListNode(); cin >> pHead->m_nValue; pHead->m_pNext = NULL; isHead = false; pCurLastNode = pHead; } else { pListNode = new ListNode(); cin >> pListNode->m_nValue; if (pListNode->m_nValue == -1) { break; } pListNode->m_pNext = NULL; pCurLastNode->m_pNext = pListNode; pCurLastNode = pListNode; } } } //从头到尾打印链表 void PrintList(ListNode *pHead) { if (pHead != NULL) { ListNode *pCur = pHead; while (pCur != NULL) { cout << pCur->m_nValue << " "; pCur = pCur->m_pNext; } cout << endl; } else { cout << "链表为空!" << endl; } } int _tmain(int argc, _TCHAR* argv[]) { ListNode *pHead = NULL; //创建链表 CreateList(pHead); PrintList(pHead); //删除头结点 DeleteNode(&pHead, pHead); PrintList(pHead); //删除第二个结点 if (pHead != NULL) { DeleteNode(&pHead, pHead->m_pNext); PrintList(pHead); } //删除尾结点 ListNode *pCur = pHead; if (pCur != NULL) { while (pCur->m_pNext != NULL) { pCur = pCur->m_pNext; } DeleteNode(&pHead, pCur); PrintList(pHead); } system("pause"); return 0; }

?

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇面试题8:旋转数组中的最小数字 下一篇面试题12:调整数组顺序使奇数位..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: