链表的基本操作

头文件

#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED
#include <stdlib.h>
#include <iostream>

using namespace std;

template<class T> class LinkList;

//链表的结点类;
template<class T>
class node {                 //单链表的结点类;
public:
  node(const T dataval, node<T>* linkval =NULL):data(dataval),next(linkval){}
  //构造函数1:根据对已给结点数据和指针构造新结点;
  node(node<T>* linkval =NULL):next(linkval){} //构造函数2:根据对已给结点指针构造新结点;
  ~node(){}
  //template<class T>
  friend class LinkList<T>;
private:
  T data;                    //结点元素的值;
  node<T> *next;             //链表的下一个结点指针;
};

template<class T>
class LinkList{							// 有序链表类;
private:
  node<T>* first;					    // 表头指针;;;
public:
  LinkList();						   // 构造函数;
  LinkList(const LinkList<T> &alist);  // 拷贝构造函数;
  LinkList(T a[],int n);              //用数组a构造链表
  ~LinkList();			  			   // 析构函数;
  int Length() const;				   //计算表长度,并输出;
  int Locate(T item) const;			   //定位函数:查找item在表中位置,返回序号;
  bool IsIn(T item) const;				//判断item是否在表中;
  void Insert(int i, const T &item);	//插入数据item到链表中;
  T Delete(int i);						//删除第i个元素(i>=1),并返回被删元素的值;
  T Next(T item)const;                  //返回item的后继;
  T Prior(T item)const;                 //返回item的前驱;
  bool Empty() const;                          //判空
  bool Full()const;                            //判满
  LinkList<T>& operator=(const LinkList<T> &alist);     //重载赋值运算符
  T operator[](int i)const;				       //按位查找,返回第i个元素的值;
  void MakeEmpty();							   //将表置为空表;
  void PrintList();//打印链表

};
template<class T>
LinkList<T> Union(const LinkList<T> & A, const LinkList<T> &B);
template<class T>
LinkList<T> Intersection(const LinkList<T> & A, const LinkList<T> &B);

template<class T>
LinkList<T>::LinkList()						   // 构造函数;
{
	first = new node<T>;
	if(!first)
		cout<<"分配失败\n";
	first->next = NULL;
	cout<<"初始化成功!\n";
}
template<class T>
LinkList<T>::~LinkList()			  			   // 析构函数;
{
	node<T> *p;
	while(first != NULL)
	{
		p = first;
		first = first->next;
		delete p;
	}
	cout<<"析构函数调用成功\n";
}

//用数组a构造链表,头插法
template<class T>
 LinkList<T>::LinkList(T a[],int n)
 {
	 first = new node<T>;
	 if(!first)
		 cout<<"用数组a构造链表的空间分配失败!\n";
	 node<T> *s;
	 first->next = NULL;
	 for(int i = 0; i < n; i++)
	 {
		 s = new node<T>;
		 if(!s)
			 cout<<"新加入结点空间分配失败"<<endl;
		 else
		 {
		 s->data = a[i];
		 s->next = first->next;
		 first->next = s;
		 }
	 }

 }
 template <class T>
 void LinkList<T>::PrintList()
 {
	 node<T> *p;
	 p = first->next;
	 if(Empty())
		 cout<<"没有元素可以输出,链表为空!\n";
	 while(p)
	 {
		 cout<<p->data<<"\t";
		 p = p->next;
	 }
	 cout<<endl;
 }

template<class T>
int LinkList<T>:: Length() const				   //计算表长度,并输出;
{
	node<T> *p;
	p = first->next;
	int count = 0;
	while(p != NULL)
	{
		p = p->next;
		count++;
	}
	return count;

}

template<class T>
  int LinkList<T>::Locate(T item) const		   //定位函数:查找item在表中位置,返回序号;
  {
	  node<T> *p;
	  p = first->next;
	  int count = 1;
	  while(p)
	  {
		  if(p->data == item)
			  return count;
		  p = p->next;
		  count++;
	  }
	  return 0;
  }

  template<class T>
  bool LinkList<T>::IsIn(T item) const				//判断item是否在表中;
  {
	  node<T> *p;
	  p = first->next;
	  while(p)
	  {
		  if(p->data == item)
			  return true;
		  p = p->next;
	  }
	  return false;
  }

  template<class T>
  void LinkList<T>::Insert(int i, const T &item)	//插入数据item到链表中;
  {
	  node<T> *p,*s;
	  p = first;
	  int count = 0;
	  while(p && count < i - 1)
	  {
		  p = p->next;
		  count++;
	  }
	  if(p == NULL)
		  cout<<"插入位置不合法!插入位置必须在链表长度范围之内!\n";
	  else
	  {
		  s = new node<T>;
		  s->data = item;
		  s->next = p->next;
		  p->next = s;
	  }
  }

  template<class T>
  T LinkList<T>::Delete(int i)						//删除第i个元素(i>=1),并返回被删元素的值;
  {
	  node<T> *p,*q;
	  int count = 0;
	  p = first;
	  while(p && count < i - 1)
	  {
		  p = p->next;
		  count++;
	  }
	  if(p == NULL)
		  return 0;
	  else
	  {
		  q = p->next;
		  T x = q->data;
		  p->next = q->next;
		  delete q;
		  return x;
	  }
  }

  template<class T>
  T LinkList<T>::Next(T item)const                 //返回item的后继;
  {
	  node<T>*p,*q;
	  p = first->next;
	  while(p && p->data != item)
	  {
		  p = p->next;
	  }
	  if(p == NULL)
	  {
		  cout<<"item不在链表中,输出0表失败\n";
		  return 0;
	  }
	  q = p->next;
	  return q->data;

  }

  template<class T>
  T LinkList<T>::Prior(T item)const                 //返回item的前驱;
  {
	  node<T> *p,*q;
	  p = first->next;
	  while(p->next&& p )
	  {
		  q = p->next;
		  if(q->data == item)
			  return p->data;
		  p = p->next;
	  }
	  cout<<"item不在链表中,输出0表失败\n";
	  return 0;
  }

  template<class T>
  bool LinkList<T>:: Empty() const                          //判空
  {
	  if(first->next == NULL)
		  return true;
	  else
		  return false;
  }
  template<class T>
  bool LinkList<T>:: Full()const                           //判满
  {
	  return false;
  }

 template<class T>
  LinkList<T>& LinkList<T>::operator=(const LinkList<T> &alist)    //重载赋值运算符
  {
	  if(first == alist.first)
	  {
		  cout<<"同一个链表,无需复制!\n";
		  return *this;
	  }
	  MakeEmpty();
	  cout<<"已清空,开始复制..\n";
	  node<T> *p,*q;
	  q = first->next;
	  p = alist.first->next;
	  for(int i = 0; i < alist.Length(); i++)
	  {
		   T x = p->data;
		   Insert(i+1,x);
		   p = p->next;
	  }
	  return *this;
  }
  template<class T>
  T LinkList<T>::operator[](int i)const			       //按位查找,返回第i个元素的值;
  {
	  node<T> *p;
	  p = first->next;
	  int count = 1;
	  while(p && count < i )
	  {
		  p = p->next;
		  count++;
	  }
	  if(p == NULL)
		  return 0;
	  else
		  return p->data;
  }

  template<class T>
  void LinkList<T>:: MakeEmpty()							   //将表置为空表;
  {
	  node<T> *p,*q;
	  p = first->next;
	  while(p)
	  {
		  q = p->next;
		  delete p;
		  p = q;
	  }
	  first->next = NULL;
  }

template<class T>
LinkList<T> Union(const LinkList<T> & A, const LinkList<T> &B) //返回集合A和B的并
{

	LinkList<T> C;
	C = B;
	int i = 1;
	T x;
	while(i < A.Length())
	{
		x = A[i];
		i++;
		if(!B.IsIn(x))
		{
			int j = B.Length()+1;
			C.Insert(j,x);
		}

	}
	return C;
}
template<class T>
LinkList<T> Intersection(const LinkList<T> & A, const LinkList<T> &B)
{
    LinkList<T> C;
    T x;
    int i = 1;
    int c = 1;
    while(i <= A.Length())
    {
        int j = i;
        i++;
        if(B.IsIn(A[j]))
        {
           C.Insert(c,A[j]);
        }
    }
    return C;

}




#endif // LINKLIST_H_INCLUDED

.cpp文件

#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED
#include <stdlib.h>
#include <iostream>

using namespace std;

template<class T> class LinkList;

//链表的结点类;
template<class T>
class node {                 //单链表的结点类;
public:
  node(const T dataval, node<T>* linkval =NULL):data(dataval),next(linkval){}
  //构造函数1:根据对已给结点数据和指针构造新结点;
  node(node<T>* linkval =NULL):next(linkval){} //构造函数2:根据对已给结点指针构造新结点;
  ~node(){}
  //template<class T>
  friend class LinkList<T>;
private:
  T data;                    //结点元素的值;
  node<T> *next;             //链表的下一个结点指针;
};

template<class T>
class LinkList{							// 有序链表类;
private:
  node<T>* first;					    // 表头指针;;;
public:
  LinkList();						   // 构造函数;
  LinkList(const LinkList<T> &alist);  // 拷贝构造函数;
  LinkList(T a[],int n);              //用数组a构造链表
  ~LinkList();			  			   // 析构函数;
  int Length() const;				   //计算表长度,并输出;
  int Locate(T item) const;			   //定位函数:查找item在表中位置,返回序号;
  bool IsIn(T item) const;				//判断item是否在表中;
  void Insert(int i, const T &item);	//插入数据item到链表中;
  T Delete(int i);						//删除第i个元素(i>=1),并返回被删元素的值;
  T Next(T item)const;                  //返回item的后继;
  T Prior(T item)const;                 //返回item的前驱;
  bool Empty() const;                          //判空
  bool Full()const;                            //判满
  LinkList<T>& operator=(const LinkList<T> &alist);     //重载赋值运算符
  T operator[](int i)const;				       //按位查找,返回第i个元素的值;
  void MakeEmpty();							   //将表置为空表;
  void PrintList();//打印链表

};
template<class T>
LinkList<T> Union(const LinkList<T> & A, const LinkList<T> &B);
template<class T>
LinkList<T> Intersection(const LinkList<T> & A, const LinkList<T> &B);

template<class T>
LinkList<T>::LinkList()						   // 构造函数;
{
	first = new node<T>;
	if(!first)
		cout<<"分配失败\n";
	first->next = NULL;
	cout<<"初始化成功!\n";
}
template<class T>
LinkList<T>::~LinkList()			  			   // 析构函数;
{
	node<T> *p;
	while(first != NULL)
	{
		p = first;
		first = first->next;
		delete p;
	}
	cout<<"析构函数调用成功\n";
}

//用数组a构造链表,头插法
template<class T>
 LinkList<T>::LinkList(T a[],int n)
 {
	 first = new node<T>;
	 if(!first)
		 cout<<"用数组a构造链表的空间分配失败!\n";
	 node<T> *s;
	 first->next = NULL;
	 for(int i = 0; i < n; i++)
	 {
		 s = new node<T>;
		 if(!s)
			 cout<<"新加入结点空间分配失败"<<endl;
		 else
		 {
		 s->data = a[i];
		 s->next = first->next;
		 first->next = s;
		 }
	 }

 }
 template <class T>
 void LinkList<T>::PrintList()
 {
	 node<T> *p;
	 p = first->next;
	 if(Empty())
		 cout<<"没有元素可以输出,链表为空!\n";
	 while(p)
	 {
		 cout<<p->data<<"\t";
		 p = p->next;
	 }
	 cout<<endl;
 }

template<class T>
int LinkList<T>:: Length() const				   //计算表长度,并输出;
{
	node<T> *p;
	p = first->next;
	int count = 0;
	while(p != NULL)
	{
		p = p->next;
		count++;
	}
	return count;

}

template<class T>
  int LinkList<T>::Locate(T item) const		   //定位函数:查找item在表中位置,返回序号;
  {
	  node<T> *p;
	  p = first->next;
	  int count = 1;
	  while(p)
	  {
		  if(p->data == item)
			  return count;
		  p = p->next;
		  count++;
	  }
	  return 0;
  }

  template<class T>
  bool LinkList<T>::IsIn(T item) const				//判断item是否在表中;
  {
	  node<T> *p;
	  p = first->next;
	  while(p)
	  {
		  if(p->data == item)
			  return true;
		  p = p->next;
	  }
	  return false;
  }

  template<class T>
  void LinkList<T>::Insert(int i, const T &item)	//插入数据item到链表中;
  {
	  node<T> *p,*s;
	  p = first;
	  int count = 0;
	  while(p && count < i - 1)
	  {
		  p = p->next;
		  count++;
	  }
	  if(p == NULL)
		  cout<<"插入位置不合法!插入位置必须在链表长度范围之内!\n";
	  else
	  {
		  s = new node<T>;
		  s->data = item;
		  s->next = p->next;
		  p->next = s;
	  }
  }

  template<class T>
  T LinkList<T>::Delete(int i)						//删除第i个元素(i>=1),并返回被删元素的值;
  {
	  node<T> *p,*q;
	  int count = 0;
	  p = first;
	  while(p && count < i - 1)
	  {
		  p = p->next;
		  count++;
	  }
	  if(p == NULL)
		  return 0;
	  else
	  {
		  q = p->next;
		  T x = q->data;
		  p->next = q->next;
		  delete q;
		  return x;
	  }
  }

  template<class T>
  T LinkList<T>::Next(T item)const                 //返回item的后继;
  {
	  node<T>*p,*q;
	  p = first->next;
	  while(p && p->data != item)
	  {
		  p = p->next;
	  }
	  if(p == NULL)
	  {
		  cout<<"item不在链表中,输出0表失败\n";
		  return 0;
	  }
	  q = p->next;
	  return q->data;

  }

  template<class T>
  T LinkList<T>::Prior(T item)const                 //返回item的前驱;
  {
	  node<T> *p,*q;
	  p = first->next;
	  while(p->next&& p )
	  {
		  q = p->next;
		  if(q->data == item)
			  return p->data;
		  p = p->next;
	  }
	  cout<<"item不在链表中,输出0表失败\n";
	  return 0;
  }

  template<class T>
  bool LinkList<T>:: Empty() const                          //判空
  {
	  if(first->next == NULL)
		  return true;
	  else
		  return false;
  }
  template<class T>
  bool LinkList<T>:: Full()const                           //判满
  {
	  return false;
  }

 template<class T>
  LinkList<T>& LinkList<T>::operator=(const LinkList<T> &alist)    //重载赋值运算符
  {
	  if(first == alist.first)
	  {
		  cout<<"同一个链表,无需复制!\n";
		  return *this;
	  }
	  MakeEmpty();
	  cout<<"已清空,开始复制..\n";
	  node<T> *p,*q;
	  q = first->next;
	  p = alist.first->next;
	  for(int i = 0; i < alist.Length(); i++)
	  {
		   T x = p->data;
		   Insert(i+1,x);
		   p = p->next;
	  }
	  return *this;
  }
  template<class T>
  T LinkList<T>::operator[](int i)const			       //按位查找,返回第i个元素的值;
  {
	  node<T> *p;
	  p = first->next;
	  int count = 1;
	  while(p && count < i )
	  {
		  p = p->next;
		  count++;
	  }
	  if(p == NULL)
		  return 0;
	  else
		  return p->data;
  }

  template<class T>
  void LinkList<T>:: MakeEmpty()							   //将表置为空表;
  {
	  node<T> *p,*q;
	  p = first->next;
	  while(p)
	  {
		  q = p->next;
		  delete p;
		  p = q;
	  }
	  first->next = NULL;
  }

template<class T>
LinkList<T> Union(const LinkList<T> & A, const LinkList<T> &B) //返回集合A和B的并
{

	LinkList<T> C;
	C = B;
	int i = 1;
	T x;
	while(i < A.Length())
	{
		x = A[i];
		i++;
		if(!B.IsIn(x))
		{
			int j = B.Length()+1;
			C.Insert(j,x);
		}

	}
	return C;
}
template<class T>
LinkList<T> Intersection(const LinkList<T> & A, const LinkList<T> &B)
{
    LinkList<T> C;
    T x;
    int i = 1;
    int c = 1;
    while(i <= A.Length())
    {
        int j = i;
        i++;
        if(B.IsIn(A[j]))
        {
           C.Insert(c,A[j]);
        }
    }
    return C;

}




#endif // LINKLIST_H_INCLUDED


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值