链表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>;//q 作为临时节点
	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 为要插进去的节点
	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 为要插进去的节点
	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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值