链表常用函数
2015-09-27 11:09
274 查看
链表的常用操作包括:
1. 插入(依序插入链尾)
2. 查找
3. 删除
4. 打印输出
5. 判空
源代码如下:
MyList.h
MyList.cpp
1. 插入(依序插入链尾)
2. 查找
3. 删除
4. 打印输出
5. 判空
源代码如下:
MyList.h
#include <iostream> struct ListNode { int nData; ListNode* pNext; }; class MyList { public: MyList() : m_pHead(NULL) {} void Insert(int data); // 直接在尾部插入 bool Find(int data); void Delete(int data); void Print(); // bool Empty(); private: ListNode* m_pHead; };
MyList.cpp
#include "MyList.h" #include <iostream> using namespace std; void MyList::Insert(int data) { ListNode* pNew = new ListNode; pNew->nData = data; pNew->pNext = NULL; if (NULL == m_pHead) { m_pHead = pNew; return; } ListNode* pCur = m_pHead; while (pCur->pNext != NULL) pCur = pCur->pNext; pCur->pNext = pNew; } bool MyList::Find(int data) { ListNode* pCur = m_pHead; while (pCur != NULL) { if (pCur->nData == data) return true; pCur = pCur->pNext; } return false; } void MyList::Delete(int data) { if (NULL == m_pHead) return; if (m_pHead->nData == data) { ListNode* pDelete = m_pHead; m_pHead = m_pHead->pNext; delete pDelete; return; } ListNode* pPrev = m_pHead; ListNode* pCur = m_pHead->pNext; while (pCur != NULL) { if (pCur->nData == data) { pPrev->pNext = pCur->pNext; delete pCur; return; } pCur = pCur->pNext; pPrev = pPrev->pNext; } } void MyList::Print() { ListNode* pCur = m_pHead; while (pCur != NULL) { std::cout << pCur->nData << " "; pCur = pCur->pNext; } std::cout << std::endl; } bool MyList::Empty() { return (m_pHead == NULL); }
相关文章推荐
- LAMP+DNS+nfs+Samba实现discuz论坛
- VIM+Ctags Linux源码阅读神器
- PPT制作教程:如何制作ppt
- HDU 5476 Explore Track of Point(平面几何)
- 软件测试的认识
- Android项目包装apk和apk反编译,xml反编译
- 查找
- 【树链剖分】【最大生成树】[NOIP2013]codevs3287 货车运输
- opencv2-3生成标定用的方格图
- JavaWeb显示器
- CSS字体属性全解析
- 第二章 SSH远程登录
- "_stringprep", referenced from:+[LibIDN prepNode:] in LibIDN.o
- aspnet生成matlab图在web上显示
- Java泛型? extends E 和 ? super E
- static使用
- 多线程(八):线程池
- No1 CoreLocation实现地理定位
- leetcode的学习心得
- 孙鑫VC++深入详解:Lesson8 Part3---如何让对话框捕获WM_KEYDOWN消息?