码迷,mamicode.com
首页 > 其他好文 > 详细

单链表的简单操作

时间:2015-05-20 16:25:35      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

单链表是一种最简单的线性表的链式存储结构,单链表又称线性链表,每个数据元素用一个结点来存储,结点分为存放数据元素的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);
};
//函数实现
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>
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.Clear();
	cout<<"链表的长度为:"<<copy.Length()<<endl;
	cout<<"链表是否为空? ";
	if(copy.IsEmpty()==1)
	{
		cout<<"Yes!"<<endl;
	}
	else
	{
		cout<<"No!"<<endl;
	}
	system("pause");
	return 0;
}
输出结果:

技术分享


单链表的简单操作

标签:

原文地址:http://blog.csdn.net/jiang111_111shan/article/details/45871883

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!