数据的存储结构

简单的顺序存储节构

template<class T>
class MyList
{//顺序存储结构
private:
	int m_length; //数组长度
	int m_Max; //数组最大上限
	T * m_element; //元素

public:
	MyList(int max = 10)
	{//构造函数
		m_length = 0;
		m_Max = max; 
		m_element = new T[max];
	}
	~MyList()
	{//析构函数
		delete[] m_element;
	}

public: //属性
	//当前长度
	int Length() const { return m_length; }
	//是否为空
	bool IsEmpty() const { return m_length == 0; }
	//是否满
	bool IsFull() const { return m_length == m_Max; }

public: //方法
	 int Add(T & x)
	 {//在尾部增加
		 if (IsFull())
		 {//如果满了, 添加失败
			 return -1;
		 }
		 m_element[m_length] = x;
		 return m_length++;
	 }

	 bool Remove(const T & x)
	 {//通过元素删
		 int index = Search(x);
		 if (index == -1)
		 {//如果元素不存在, 删除失败
			 return false;
		 }
		 return RemoveAt(index);
	 }

	 bool RemoveAt(int index)
	 {//通过下标删
		 if (index >= m_length || index < 0)
		 {//当给定下标超出范围, 删除失败
			 return false;
		 }
		 for (int i = index, n = m_length; i < n; i++)
		 {//将给定下标后的元素前移
			 m_element[i] = m_element[i + 1];
		 }
		 m_length--; //减少当前长度
		 return true;
	 }

	 int Insert(int index, const T & x)
	 {//在指定位置插入
		 if (index < 0 || index > m_Max)
		 {//index如果超出正确取值范围, 插入失败
			 return -1;
		 }
		 if (IsFull())
		 {//数组满了, 插入失败
			 return -1;
		 }
		 for (int i = m_length - 1, n = index; i >= n; i--)
		 {//从最后元素开始后移
			 m_element[i + 1] = m_element[i];
		 }
		 m_element[index] = x; //插入元素
		 m_length++; //长度加1
		 return index;
	 }

	 T & operator[](int index)
	 {//查找元素
		 return m_element[index];
	 }

	 int Search(const T& x) const
	 {//给定元素, 查找下标
		 for (int i = 0, n = m_length; i < n; i++)
		 {
			 if (m_element[i] == x)
			 {
				 return i;
			 }
		 }
		 return -1;
	 }
};

链式存储节构

template<class T>
class LinkList;

template<class T>
class LinkNode
{//节点
	T value; //值
	LinkNode<T> * next; //下一节点
	friend LinkList<T>;
};

template<class T>
class LinkList
{//链式存储结构
private:
	LinkNode<T> * first; //头节点
	int m_len; //链表长度
public: //构造
	LinkList()
	{//构造函数
		first = 0;
		m_len = 0;
	}
	~LinkList()
	{//析构函数
		LinkNode<T> * temp;
		while (first)
		{//当前节点不为空时, 释放当前节点空间
			temp = first; //next指向first
			first = first->next; //first指向下一节点
			delete temp; //释放next
		}
	}

public: //属性
	//连表是否为空
	bool IsEmpty() const { return first == 0; }
	//当前链表长度
	int Length() const { return m_len; }

public:	//方法
	int Insert(int index, const T & x)
	{//在指定信置添加
		if (index < 0 || index > m_len)
		{//超出范围
			return -1;
		}
		LinkNode<T> * node = new LinkNode<T>;
		node->value = x;
		if (!index)
		{//下标为0, 
			if (IsEmpty())
			{//链表为空
				node->next = NULL;
				first = node;
			}
			else
			{//链表不为空
				LinkNode<T> * current = first;
				node->next = current;
				first = node;
			}
		}
		else
		{//下标不为0
			LinkNode<T> * pnext = first;
			LinkNode<T> * ppre = pnext; 
			for (int i = 0; i != index; i++)
			{
				ppre = pnext;
				pnext = pnext->next;
			}
			ppre->next = node;
			node->next = pnext;
		}
		m_len++;
		return index;
	}

	bool RemoveAt(int index)
	{//通过下标删除
		if (index <0 || index >= m_len)
		{//超出范围
			return false;
		}
		LinkNode<T> * current = first;
		if (!index)
		{//删除头节点
			first = first->next;
		}
		else
		{//删除非头节点
			LinkNode<T> * ppre = current;
			for (int i = 0; i != index; i++)
			{
				ppre = current;
				current = current->next;
			}
			ppre->next = current->next;
		}
		m_len--;
		delete current;
		return true;
	}

	bool Remove(const T & x)
	{//通过给定元素删除
		LinkNode<T> * current = first;
		int index = Search(x);
		if (index == -1)
		{
			return false;
		}
		return RemoveAt(index);
	}

	int Search(const T & x) const
	{//给定元素找下标
		int index = 0;
		LinkNode<T> * current = first;
		while (current)
		{
			if (x == current->value)
			{
				return index;
			}
			current = current->next;
			index++;
		}
		return -1;
	}

	T & operator[](int index)
	{//给定下标找元素
		if (index <0 || index >m_len)
		{//超出取值范围
			throw "excption";
		}
		LinkNode<T> * current = first;
		for (int i = 0; i != index; i++)
		{
			current = current->next;
		}
		return current->value;
	}
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值