单链表的简单操作

单链表是一种最简单的线性表的链式存储结构,单链表又称线性链表,每个数据元素用一个结点来存储,结点分为存放数据元素的data和存放指向链表下一个结点的指针next。

链表实现:(LinkList.h)

//单链表
#ifndef LINKLIST_H_
#define LINKLIST_H_
#include <iostream>
//using namespace std;
template <typename T>
struct Node
{
	//数据成员
	T data;
	Node<T> *next;
	//构造函数
	Node();
	Node(T item, Node<T> *link=NULL);
};
//结点类实现部分
template <typename T>
Node<T>::Node()
{//构造
	next=NULL;
}
template <typename T>
Node<T>::Node(T item, Node<T> *link)
{//构造一个数据域为item和指针域为link的结点
	data=item;
	next=link;
}

//简单线性链表类
template <typename T>
class SimpleLinkList
{
protected:
	Node<T> *head;//头结点指针
	//
	//Node<T> *GetElemPtr(int position) const;
	void Init();//初始化线性表
public:
	Node<T> *GetElemPtr(int position) const;
	SimpleLinkList();//无参数构造函数
	virtual ~SimpleLinkList();//析构函数
	int Length() const;
	bool IsEmpty() const;
	void Clear();
	void Traverse(void(* Visit)(T &));
	int GetElem(int position, T &e) const;
	SimpleLinkList<T> &SetElem(int position, const T &e);
	SimpleLinkList<T> &Delete(int position, T &e);
	SimpleLinkList<T> &Insert(int position, const T &e);
	SimpleLinkList(const SimpleLinkList<T> &copy);
	SimpleLinkList<T> &operator =(const SimpleLinkList<T> &copy);
	void Reverse();
	//friend std::ostream & operator<<(std::ostream &os, SimpleLinkList<T>& LinkList);
};
//函数实现
template<typename T>
Node<T> *SimpleLinkList<T>::GetElemPtr(int position) const
{
	Node<T> *tempPtr=head;
	int curPosition=0;
	while(tempPtr!=NULL && curPosition<position)
	{
		tempPtr=tempPtr->next;
		++curPosition;
	}
	if(tempPtr!=NULL && curPosition==position)
	{//查找成功
		return tempPtr;
	}
	else
	{//查找失败
		return NULL;
	}
}

template <typename T>
void SimpleLinkList<T>::Init()
{
	head=new Node<T>;//构造头指针
}
//构造函数
template <typename T>
SimpleLinkList<T>::SimpleLinkList()
{
	Init();
}
//析构函数
template<typename T>
SimpleLinkList<T>::~SimpleLinkList()
{
	Clear();//清空线性表
	delete head;//释放头结点所指空间
}
//返回链表长度
template<typename T>
int SimpleLinkList<T>::Length() const
{
	int count=0;
	for(Node<T> *tempPtr=head->next; tempPtr!=NULL; tempPtr=tempPtr->next)
	{
		++count;
	}
	return count;
}
//判断是否为空
template<typename T>
bool SimpleLinkList<T>::IsEmpty() const
{
	if(head->next==NULL)
		return 1;
	else 
		return 0;
	//return head->next==NULL;
}
//清空链表
template <typename T>
void SimpleLinkList<T>::Clear()
{
	T temp;
	while(Length()>0)
	{
		Delete(0,temp);
	}
}
//遍历链表
template<typename T>
void SimpleLinkList<T>::Traverse(void(* Visit)(T &))
{
	for(Node<T> *tempPtr=head->next; tempPtr!=NULL; tempPtr=tempPtr->next)
	{
		(*Visit)(tempPtr->data);//对线性表的每个元素调用函数(*Visit)
		//std::cout<<tempPtr->data<<std::endl;
	}
}
//获取结点值
template <typename T>
int SimpleLinkList<T>::GetElem(int position, T &e) const
{
	if(position<0 || position>Length())
	{
		std::cout<<"您说访问的位置超出链表范围!"<<std::endl;
		return -1;
	}
	else
	{
		Node<T> *tempPtr;
		tempPtr=GetElemPtr(position);
		e=tempPtr->data;
		return 0;
	}
}
//设置结点值
template<typename T>
SimpleLinkList<T> &SimpleLinkList<T>::SetElem(int position, const T &e)//是不是要加一个引用
{
	if(position<0 || position>Length())
	{
		std::cout<<"超出范围!"<<std::endl;
		return *this;//这个this
	}
	else
	{
		Node<T> *tempPtr;
		tempPtr=GetElemPtr(position);
		tempPtr->data=e;
		return *this;
	}
}
//删除结点
template<typename T>
SimpleLinkList<T> &SimpleLinkList<T>::Delete(int position, T &e)
{
	if(position<0 || position>Length())
	{
		std::cout<<"超出范围!"<<std::endl;
		return *this;//这个this
	}
	else
	{
		Node<T> *tempPtr;
		tempPtr=GetElemPtr(position);
		Node<T> *nextPtr=tempPtr->next;
		tempPtr->next=nextPtr->next;
		e=nextPtr->data;
		delete nextPtr;
		return *this;
	}
}
//插入结点
template<typename T>
SimpleLinkList<T> &SimpleLinkList<T>::Insert(int position, const T &e)
{
	if(position<0 || position>Length())
	{
		std::cout<<"超出范围!"<<endl;
		return *this;
	}
	else
	{
		Node<T> *tempPtr;
		tempPtr=GetElemPtr(position);
		Node<T>* newPtr;
		newPtr=new Node<T>(e,tempPtr->next);
		tempPtr->next=newPtr;
		return *this;

	}
}
template <typename T>
SimpleLinkList<T>::SimpleLinkList(const SimpleLinkList<T> &copy)
{
	int copyLength=copy.Length();
	Init();
	Node<T> *tempPtr;
	for(int curPosition=0; curPosition<copyLength; curPosition++)
	{
		//Node<T> *tempPtr;
		tempPtr=copy.GetElemPtr(curPosition+1);
		Insert(curPosition,tempPtr->data);
	}
	//return *this;
}
template<typename T>
SimpleLinkList<T> &SimpleLinkList<T>::operator =(const SimpleLinkList<T> &copy)
{
	if(&copy!=this)
	{
		int copyLength=copy.Length();
		Node<T> *tempPtr;
		Init();
		for(int curPosition=0; curPosition<=copyLength; curPosition++)
		{
			//Node<T> *tempPtr;
			tempPtr=copy.GetElemPtr(curPosition);
			Insert(curPosition,tempPtr->data);
		}
	}
	//return this;
}
//倒置
template <typename T>
void SimpleLinkList<T>::Reverse()
{
	T a,b;
	int Len=Length();
	for(int curPosition=1; curPosition<=Length()/2; curPosition++)
	{
		GetElem(curPosition,a);
		GetElem(Length()-curPosition+1,b);//参数待调整
		SetElem(curPosition,b);
		SetElem(Length()-curPosition+1,a);
	}
}
//重载<<,输出
template<typename T>
std::ostream & operator<<(std::ostream &os, SimpleLinkList<T>& LinkList)
{
	std::cout<<"输出链表: ";
	for(int curPosition=1; curPosition<=LinkList.Length(); curPosition++)
	{
		std::cout<<((LinkList.GetElemPtr(curPosition))->data)<<" ";
	}
	return os;
}
#endif
主程序:LinkList.cpp

// LinkList.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "LinkList.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	SimpleLinkList<int> LinkList;
	cout<<"链表的长度为:"<<LinkList.Length()<<endl;
	cout<<"链表是否为空? ";
	if(LinkList.IsEmpty()==1)
	{
		cout<<"Yes!"<<endl;
	}
	else
	{
		cout<<"No!"<<endl;
	}
	LinkList.Insert(0,1);
	LinkList.Insert(1,2);
	LinkList.Insert(2,3).Insert(3,4).Insert(4,5).Insert(5,6);
	cout<<LinkList<<endl;
	cout<<"链表的长度为:"<<LinkList.Length()<<endl;
	cout<<"链表是否为空? ";
	if(LinkList.IsEmpty()==1)
	{
		cout<<"Yes!"<<endl;
	}
	else
	{
		cout<<"No!"<<endl;
	}
	//复制链表
	//SimpleLinkList<int> copy(LinkList);
	SimpleLinkList<int> copy=LinkList;
	copy.Insert(2,0).Insert(3,0);
	cout<<"链表的长度为:"<<copy.Length()<<endl;
	cout<<"链表是否为空? ";
	if(copy.IsEmpty()==1)
	{
		cout<<"Yes!"<<endl;
	}
	else
	{
		cout<<"No!"<<endl;
	}
	cout<<copy<<endl;
	cout<<"设置值之后:"<<endl;
	copy.SetElem(1,100);
	cout<<copy<<endl;
	copy.Reverse();
	cout<<"链表倒置:";
	cout<<copy<<endl;
	//清除链表
	cout<<"清空链表"<<endl;
	copy.Clear();
	cout<<"链表的长度为:"<<copy.Length()<<endl;
	cout<<"链表是否为空? ";
	if(copy.IsEmpty()==1)
	{
		cout<<"Yes!"<<endl;
	}
	else
	{
		cout<<"No!"<<endl;
	}
	system("pause");
	return 0;
}
结果:








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值