单链表的封装
来源:互联网 发布:外教网络一对一收费 编辑:程序博客网 时间:2024/05/19 00:53
#include <iostream>
using namespace std;
typedef struct Node
{
int m_data;
Node *m_next;
}Node;
class IntList
{
public:
IntList();
~IntList();
bool append(int value);
bool remove(int value);
void printList();
Node *find(int value);
private:
Node *m_head;
};
IntList::IntList()//构造
{
m_head = new Node;//空头结点
m_head->m_next = NULL;
}
IntList::~IntList()//析构
{
Node *p = m_head;
while(p!=NULL)
{
Node *temp = p;
p = p->m_next;
delete temp;
}
}
bool IntList::append(int value)//尾部添加
{
Node * temp = new Node;
if(temp==NULL)
{
return false;
}
Node *p = m_head;
while(p->m_next!=NULL)
{
p = p->m_next;
}
temp->m_data = value;
temp->m_next = p->m_next;
p->m_next = temp;
return true;
}
bool IntList::remove(int value)//删除
{
Node * pre = m_head;
Node * p = m_head->m_next;
if(p == NULL)
return false;
while(p->m_data!=value&&p->m_next!=NULL)
{
pre = p;
p = p->m_next;
}
if(p->m_data == value)
{
pre ->m_next = p->m_next;
delete p;
}
else
{
return false;
}
return true;
}
void IntList::printList()//打印
{
Node *p = m_head->m_next;
while(p!=NULL)
{
cout<<p->m_data<<' ';
p = p->m_next;
}
cout<<endl;
}
Node *IntList::find(int value)//查找
{
Node *p = m_head->m_next;
if(p==NULL)
{
return NULL;
}
while(p->m_data!= value&&p->m_next!=NULL)
{
p = p->m_next;
}
if(p->m_data == value)
{
return p;
}
else
{
return NULL;
}
}
int main(int argc, char* argv[])
{
IntList L;
L.append(10);
L.append(13);
L.append(8);
L.append(6);
L.printList();
L.remove(8);
L.printList();
Node * f = L.find(10);
if(f==NULL)
{
cout<<"NULL"<<endl;
}
else
{
cout<<f<<'\t'<<f->m_data<<endl;
}
return 0;
}
- c++ 封装的单链表
- 单链表的封装
- 单链表的实现C++封装
- 封装机制封装的概念
- 封装:树形节点的封装
- 简单的jdbc封装+servlet的封装
- 工具类的封装-sharePreference的封装
- 封装性是指对外部的封装
- 强大的封装标签封装类
- 什么是封装,封装类的使用!
- java的封装(如何封装类)
- alertController的封装(在TYAlertController基础上封装)
- 工具类的封装--Log封装
- 工具类的封装--SharedPreferences封装
- 封装尺寸与封装的对应关系
- 什么是javascript封装,封装的方法有几种
- 关于封装的设计
- 函数的封装解释
- Hashmap原理
- Lua 应用
- NYOJ 21题 三个水杯
- java好的编码习惯
- 接口原理与应用
- 单链表的封装
- CodeForces 113B Petr#(KMP + 字符串hash)
- 是什么浪费了我的时间
- 1586 计算组合数
- 输入10个数字,输出最大值
- An internal error occurred during: "Importing Maven projects". Unsupported IClasspathEntry kind=4
- win7无线网密码破译以及密码保护
- CentOS 6.0 安装Atheros ar8151网卡驱动
- 把Eclipse打造成Emacs——写给Eclipse下挣扎的Emacser