首页 > 编程语言 > 详细

数据结构-线性表的一些基础操作 c++代码

时间:2015-03-31 22:23:53      阅读:266      评论:0      收藏:0      [点我收藏+]
//线性表的顺序存储结构
template <class T>
class Linearlist
{
	public:
		Linearlist(int MaxListSize == 10);
		~Linearlist() 
		{
			delete []element;
		}

		bool IsEmpty() const 
		{
			return length == 0;
		}
		bool IsFull() const 
		{
			return length == MaxSize;
		}
		int Length() const 
		{
			return length;
		}

		bool Find(int k, T &item) const;
		int search(const T &item) const;
		void Delete(int k, T &item);
		void Insert(int k, const T &item);
	private:
		int MaxSize, length, T *element;
};

template <class T>
Linearlist<T>::Linearlist(int MaxListSize == 10)
{
	MaxSize = MaxListSize;
	element = new T[MaxSize];   //申请规模为MaxSize的数组空间
	length = 0;                // 初始时没有真正的表结点,故表长度为0
}

//存取:将下标为k的结点的字段值赋给item并返回true,若不存在则返回false;
template <class T>
bool Linearlist<T>::Find(int k, T &item)  const
{
	if (k < 0 || length == 0 || k >length-1)
	{
		cout << " unreasonable position or empty list!" << endl;
		return false;
	}
	else
	{
		item = element[k];
		return true;
	}
}

//查找:在表中查找字段值为item的结点并返回其下标;若表中没有item,则返回-1;
template <class T>
int Linearlist<T>::search(const T &item) const
{
	for (int k = 0; k <= length-1; k++)
	{
		if(element[k] == item)
			return k;
	}
	return -1;
}

//删除表中下表为k的结点并将其值赋给item
template <class T>
void Linearlist<T>::Delete(int k, T &item)
{
	if (find(k, item))   //若找到下标为k的结点,则将其后面所有结点均向前移动一个位置
	{
		for (int i=k; i<length; i++)
		{
			element[i] = element[i+1];
		}
		item = element[k];
		length--;
	}
}

//在下表为k的结点后插入item
template <class T>
void Linearlist<T>::Insert(int k, const T &item)
{
	if(IsFull())                   //若表已满,无法插入新结点
	{
		cout << "The list is full!" << endl;
		exit(1);
	}
	else if (k<0 || k>length-1)
	{
		cout << "The node does not exist!" << endl;
		exit(1);
	}
	else
	{
		for (i=length-1; i>=k+1; i--)
		{
			element[i+1] = element[i];
		}
	}
	element[k+1] = item;
	length++;
}

//单链表结点结构SLNode
template<class T>
struct SLNode
{
	T data;                      //数据域
	SLNode<T> *next;             //指针域

	SLNode(SLNode *nextNode = NULL)
	{
		next = nextNode;
	}

	SLNode(const T &item, SLNode *nextNode = NULL)
	{
		data = item;
		next = nextNode;
	}
};

// SLNode 的定义参考博文数据结构-栈的一些基础操作c++代码
//单链表的链式存储结构
template <class T>
class SLList
{
	public:
		SLList(){	head = new SLNode<T>()};   //构造函数,构造一个只有哨位结点的空表
		SLList(T &item);                       //构造函数
		~SLList();
		bool IsEmpty() const {return head->next == NULL;};
		int length() const;
//		bool Find(int k, T &item) const;
		int Search(const T &item) const;
		void Delete(int k, T &item);
		void Insert(int k, const T &item);
	private:
		SLNode<T> *head;
		SLNode<T> *tail;
		SLNode<T> *currptr;
};

//单链表的构造函数,生成一个只有哨位结点的空表
template<class T>
SLList<T>::SLList()
{
	head = tail = currptr = new SLNode<T>();
	size = 0;
}

//单链表的构造函数,生成含有哨位结点和一个表结点的表
template<class T>
SLList<T>::SLList(T &item)
{
	tail = currptr = new SLNode<T>(item);         //生成一个表结点
	head = new SLNode<T>(currptr);               //生成哨位结点
	size = 1;
}

//单链表的析构函数
template <class T>
SLList<T>::~SLList()
{
	while (!IsEmpty())
	{
		currptr =  head->next;
		head->next = currptr->next;
		delete currptr;
	}
	delete head;
}

//算法Insert
//在当前结点后插入一个data域值为item的结点
template <class T>
void SLList<T>::Insert(const T &item)
{
	currptr->next = new SLNode<T>(item, currptr->next);
	if (tail == currptr)
		tail = currptr->next;
	size++;
}

//在表尾插入一个data域值为item的结点
template <class T>
void SLList<T>::InsertFromTail(const T &item)
{
	tail->next = new SLNode<T>(item, NULL);
	tail = tail->next;
	size++;
}

//在哨位结点后插入
template <class T>
void SLList<T>::InsertFromHead(const T &item)
{
	if(IsEmpty())
	{
		head->next = new SLNode<T>(item, head->next);
		tail = head->next;
	}
	else
	{
		head->next = new SLNode<T>(item, head->next);
	}
	size++;
}

//算法Delete
//删除当前结点的后继结点并将其data值返回给变量de_item

template <class T>
bool SLList<T>::Delete(T &de_item)
{
	if(currptr == tail || IsEmpty())
	{
		cout << "No next node or empty list!";
		return false;
	}
	SLNode<T> *temp = currptr->next;
	currptr->next = temp->next;
	size--;
	de_item = temp->data;
	
	if(temp == tail)
		tail = currptr;
	delete temp;
	return true;
}

// 删除哨位结点后的第一个真正表结点并将其data值返回给变量de_item
template <class T>
bool SLList<T>::DeleteFromHead(T &de_item)
{
	if (IsEmpty())
	{
		cout << "Empty list!";
		return false;
	}

	SLNode<T> *temp = head->next;
	head->next = temp->next;
	size--;
	de_item = temp->data;
	if (temp == tail)
	{
		tail = head;
	}
	delete temp;
	return true;
}

//删除表尾结点并将其data值返回给变量de_item

template <class T>
bool SLList<T>::DeleteFromTail(T &de_item)
{
	if (IsEmpty())
	{
		cout << "Empty list!";
		return false;
	}
	//令当前指针指向表尾结点的前驱结点
	SetEnd();
	Prev();
	de_item = tail->data;
	currptr->next = NULL;
	size--;
	delete tail;
	tail = currptr;
	return true;
}

数据结构-线性表的一些基础操作 c++代码

原文:http://blog.csdn.net/u012421537/article/details/44784875

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!