C++实现封装链表
来源:互联网 发布:javascript 雪峰 编辑:程序博客网 时间:2024/06/03 20:12
C++实现封装链表
#include <iostream>
#include <iomanip>
using namespace std;
template <class T>
class node //节点
{
public:
T data;
node *next;
};
template <class T>
class list
{
public:
list();
void Create(); //创建链表
bool Empty() const; //判断链表是否为空
void InsertLast(const T e); //从尾部插入一个元素
void InsertFirst(const T e); //从头部插入一个元素
void DeleteFirst(); //从头删除一个元素
void DeleteLast(); //从尾删除元素
void Display() const; //显示链表
node<T>* GetNode(int i); //返回第i个节点
void Reverse(); //链表逆置
bool Find(const T e); //在链表中查找某个值
~list(); //销毁链表
private:
node<T> *head; //头节点
};
template <class T>
list<T>::list()
{
head=new node<T>;
head->next=NULL;
}
template <class T>
void list<T>::Create()
{
node<T> *p,*q;
p=head;
q=new node<T>;
cout<<"请输入值(按'ctrl+z'停止): ";
while(cin>>q->data)
{
p->next=q;
p=q;
q=new node<T>;
}
p->next=NULL;
}
template <class T>
bool list<T>::Empty() const
{
return (head->next==NULL);
}
template <class T>
void list<T>::InsertFirst(const T e)
{
node<T> *p=new node<T>;
p->data=e;
p->next=head->next;
head->next=p;
}
template <class T>
void list<T>::InsertLast(const T e)
{
node<T> *p,*q;
p=head;
q=new node<T>;
q->data=e;
while(p->next)
{
p=p->next;
}
p->next=q;
q->next=NULL;
}
template <class T>
void list<T>::DeleteFirst()
{
head->next=head->next->next;
}
template <class T>
void list<T>::DeleteLast()
{
node<T> *p;
p=head;
while(p->next->next)
p=p->next;
p->next=NULL;
}
template <class T>
void list<T>::Display() const //显示链表
{
node<T> *p;
p=head->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
}
template <class T>
node<T>* list<T>::GetNode(int i) //返回第i个节点
{
int k=0;
node<T> *p;
p=head;
while(p && k<i)
{
p=p->next;
++k;
}
return p;
}
template <class T>
void list<T>:: Reverse() //链表逆置
{
node<T> *p,*q;
p=head->next;
int count=0;
while(p) //求链表的长度
{
p=p->next;
count++;
}
int i,j;
i=1;
j=count;
while(i<=count/2 && j>=count/2)
{
p=GetNode(i);
q=GetNode(j);
T temp=p->data;
p->data=q->data;
q->data=temp;
++i;
--j;
}
}
template <class T>
bool list<T>::Find(const T e) //在链表中查找某个值
{
bool flag=false;
node<T> *p;
p=head->next;
while(p)
{
if(p->data==e)
{
flag=true;
break;
}
p=p->next;
}
return flag;
}
template <class T>
list<T>::~list()
{
node<T> *p;
while(head)
{
p=head->next;
delete head;
head=p;
}
}
int main()
{
list<int> ilist;
ilist.Create(); //创建链表
ilist.Display(); //输出链表
cout<<endl;
ilist.InsertFirst(123); //从头插入一个值
ilist.InsertLast(456); //从尾插入一个值
ilist.Display();
cout<<endl;
if(ilist.Find(123))
cout<<"123 在链表中"<<endl;
else
cout<<"123 不在链表中"<<endl;
ilist.DeleteFirst(); //从头删除一个值
ilist.DeleteLast(); //从尾删除一个值
ilist.Display();
cout<<endl;
ilist.Reverse(); //逆序
ilist.Display();
cout<<endl;
system("pause");
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
template <class T>
class node //节点
{
public:
T data;
node *next;
};
template <class T>
class list
{
public:
list();
void Create(); //创建链表
bool Empty() const; //判断链表是否为空
void InsertLast(const T e); //从尾部插入一个元素
void InsertFirst(const T e); //从头部插入一个元素
void DeleteFirst(); //从头删除一个元素
void DeleteLast(); //从尾删除元素
void Display() const; //显示链表
node<T>* GetNode(int i); //返回第i个节点
void Reverse(); //链表逆置
bool Find(const T e); //在链表中查找某个值
~list(); //销毁链表
private:
node<T> *head; //头节点
};
template <class T>
list<T>::list()
{
head=new node<T>;
head->next=NULL;
}
template <class T>
void list<T>::Create()
{
node<T> *p,*q;
p=head;
q=new node<T>;
cout<<"请输入值(按'ctrl+z'停止): ";
while(cin>>q->data)
{
p->next=q;
p=q;
q=new node<T>;
}
p->next=NULL;
}
template <class T>
bool list<T>::Empty() const
{
return (head->next==NULL);
}
template <class T>
void list<T>::InsertFirst(const T e)
{
node<T> *p=new node<T>;
p->data=e;
p->next=head->next;
head->next=p;
}
template <class T>
void list<T>::InsertLast(const T e)
{
node<T> *p,*q;
p=head;
q=new node<T>;
q->data=e;
while(p->next)
{
p=p->next;
}
p->next=q;
q->next=NULL;
}
template <class T>
void list<T>::DeleteFirst()
{
head->next=head->next->next;
}
template <class T>
void list<T>::DeleteLast()
{
node<T> *p;
p=head;
while(p->next->next)
p=p->next;
p->next=NULL;
}
template <class T>
void list<T>::Display() const //显示链表
{
node<T> *p;
p=head->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
}
template <class T>
node<T>* list<T>::GetNode(int i) //返回第i个节点
{
int k=0;
node<T> *p;
p=head;
while(p && k<i)
{
p=p->next;
++k;
}
return p;
}
template <class T>
void list<T>:: Reverse() //链表逆置
{
node<T> *p,*q;
p=head->next;
int count=0;
while(p) //求链表的长度
{
p=p->next;
count++;
}
int i,j;
i=1;
j=count;
while(i<=count/2 && j>=count/2)
{
p=GetNode(i);
q=GetNode(j);
T temp=p->data;
p->data=q->data;
q->data=temp;
++i;
--j;
}
}
template <class T>
bool list<T>::Find(const T e) //在链表中查找某个值
{
bool flag=false;
node<T> *p;
p=head->next;
while(p)
{
if(p->data==e)
{
flag=true;
break;
}
p=p->next;
}
return flag;
}
template <class T>
list<T>::~list()
{
node<T> *p;
while(head)
{
p=head->next;
delete head;
head=p;
}
}
int main()
{
list<int> ilist;
ilist.Create(); //创建链表
ilist.Display(); //输出链表
cout<<endl;
ilist.InsertFirst(123); //从头插入一个值
ilist.InsertLast(456); //从尾插入一个值
ilist.Display();
cout<<endl;
if(ilist.Find(123))
cout<<"123 在链表中"<<endl;
else
cout<<"123 不在链表中"<<endl;
ilist.DeleteFirst(); //从头删除一个值
ilist.DeleteLast(); //从尾删除一个值
ilist.Display();
cout<<endl;
ilist.Reverse(); //逆序
ilist.Display();
cout<<endl;
system("pause");
return 0;
}
- C语言实现封装
- C语言实现封装
- LINUX C 链表封装
- C++实现封装链表
- C实现-CPP的-封装
- C语言实现封装,继承
- 【C#】用C#实现封装
- 用C语言封装的链表
- C语言之单向链表封装
- 使用C语言封装链表函数
- C语言实现简单的日志封装
- C语言封装成object-c的过程,实现原理
- C#--封装
- 【C#】封装
- 双向链表的两种实现与封装性
- C# 泛型类的实现,链表的封装LinkedListNode
- 单向循环链表的实现C++封装
- 在C中实现对struct内部数据的封装
- 在64位windows下使用instsrv.exe和srvany.exe创建windows服务
- NIO Socket实现文件传输
- 解决"System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高
- oracle密码大小写问题
- 内存泄露方式;抽象类;虚函数;
- C++实现封装链表
- TinyOS之Blink(三) - 默认系统主时钟初始化分析
- MFC多文档框架详细介绍
- ubuntu下安装mysql
- spring自定义scope
- Bundle savedInstanceState的作用
- internet中网络终端如何访问tinyos节点
- 使用者自定义User-Defined的迭代器
- 外部的.sql文件导入Navicat