C++实现双链表和双向循环链表和静态链表

#pragma once
#include<iostream>
using namespace std;

//静态链表---一维数组

#define MAXSIZE 201//静态链表的大小---0不存放数据 从下标一开始存放数据

enum NODEUSE
{
	e_NOUSE = -1,//没有使用为-1
	e_LAST=-2,//最后一个元素为-2
};

template<class T>
struct STNode
{
	T data;
	int cur;//记录下个静态链表的节点的数组下标
};

template<class T>
class StaticList
{
public:
	StaticList();
	~StaticList();

public:
	int findAnIdlePos();//找到一个空闲的位置存放数据
	bool ListInsert(int pos,const T&e);
	bool ListDelete(int pos);

	bool GetElem(int pos, T& e);;
	int LocateElem(const T& e);

	void DispList();
	int  LengthList();
	bool EmptyList();

private:
	STNode<T> m_data[MAXSIZE];
	int m_length;
};

template<class T>
StaticList<T>::StaticList()
{
	for (int i = 1; i < MAXSIZE; i++)
	{
		m_data[i].cur = e_NOUSE;//标记这些为存放数据
	}
	m_length = 0;
}

template<class T>
int StaticList<T>::findAnIdlePos()//找到一个空闲的位置存放数据,没找到返回-1
{
	for (int i = 1; i < MAXSIZE; i++)
	{
		if (m_data[i].cur == e_NOUSE)
		{
			return i;
		}
	}
	return -1;
}

template<class T>
bool StaticList<T>::ListInsert(int pos, const T& e)
{
	if (pos<1 || pos>(m_length + 1))
	{
		cout << "不合法" << endl;
		return false;
	}

	int index;
	if ((index=findAnIdlePos()) == -1)
	{
		cout << "满了" << endl;
		return false;
	}

	int dataCount = 1;//记录元素个数
	int indexPrev;//保存插入位置前一个位置对于的数组下标

	if (pos == 1)//在第一个位置插入
	{
		m_data[index].data = e;
		if (m_length == 0)//空表---标记其为尾
		{
			m_data[index].cur = e_LAST;
		}
		else//非空表记录即cur的下标值,即下一个存放数据的位置
		{
			m_data[index].cur = m_data[0].cur;
		}
		m_data[0].cur = index;
	}
	else
	{
		int posCount = 0;//位置计数
		int tmpcur = m_data[0].cur;
		while (true)
		{
			posCount++;
			if (posCount >= (pos - 1))
			{
				indexPrev = tmpcur;
				break;
			}
		}
		int iTmpcurr = m_data[indexPrev].cur;
		m_data[indexPrev].cur = index;
		m_data[index].data = e;
		m_data[index].cur = iTmpcurr;
	}
	m_length++;
	cout << "插入成功" << endl;
	return true;
}

template<class T>
bool StaticList<T>::ListDelete(int pos)
{
	if (m_length < 1 || pos<1 || pos>m_length)
	{
		cout << "不合法" << endl;
		return false;
	}
	int tmpcur = m_data[0].cur;

	if (pos == 1)//在第一个位置删除
	{
		if (m_length != 1)
		{
			m_data[0].cur = m_data[tmpcur].cur;
		}
		m_data[tmpcur].cur = e_NOUSE;
		cout << "删除成功" << endl;
	}
	else
	{
		while (true)
		{
			posCount++;
			if (posCount >= (pos - 1))
			{
				indexPrev = tmpcur;
				break;
			}
			tmpcur= m_data[tmpcur].cur;
		}
		int iTmpcurr = m_data[indexPrev].cur;//要删除下标
		m_data[indexPrev].cur = m_data[iTmpcurr].cur;//删除节点前一个cur指向要删除的节点cur
		m_data[iTmpcurr].cur = e_NOUSE;

		cout << "删除成功" << endl;
	}
	m_length--;
	return true;
}

template<class T>
bool StaticList<T>::GetElem(int pos, T& e)
{
	if (m_length < 1 || pos<1 || pos>m_length)
	{
		cout << "不合法" << endl;
		return false;
	}

	int tmpcur = m_data[0].cur;
	int ppos = 0;
	while (true)
	{
		ppos++;
		if (ppos == pos)
		{
			e = m_data[tmpcur].data;
			cout << "找到了" << e << endl;
			return true;
		}
		tmpcur = m_data[tmpcur].cur;
	}
	return false;
}

template<class T>
int StaticList<T>::LocateElem(const T&e)
{
	if (m_length < 1 || pos<1 || pos>m_length)
	{
		cout << "不合法" << endl;
		return -1;
	}

	int tmpcur = m_data[0].cur;
	int ppos = 0;
	while (true)
	{
		ppos++;
		if (m_data[tmpcur].data == e && m_data[tmpcur].cur != e_NOUSE)
		{
			cout << "找到了" << ppos << endl;
			return tmpcur;
		}
		if (m_data[tmpcur].cur == e_LAST)
		{
			break;
		}
		tmpcur = m_data[tmpcur].cur;
	}
	cout << "没找到" << endl;
	return -1;
}


template<class T>
void StaticList<T>::DispList()
{
	if (m_length < 1)
	{
		cout << "链表为空" << endl;
	}

	int tmpcur = m_data[0].cur;
	while (true)
	{
		cout << m_data[tmpcur].data << " ";
		if ((tmpcur = m_data[tmpcur].cur) == e_LAST)
		{
			break;
		}
		cout << endl;
	}

}


template<class T>
int  StaticList<T>::LengthList()
{
	return m_length;
}

template<class T>
bool StaticList<T>::EmptyList()
{
	if (m_length < 1)
		return true;
	return false;
}


template<class T>
StaticList<T>::~StaticList()
{

}

#pragma once
#include<iostream>
using namespace std;

//双链表

template<class T>
struct DNode
{
	T data;
	DNode<T>* prior;
	DNode<T>* next;
};

template<class T>
class DList
{
public:
	DList();
	~DList();

public:
	bool DListInsert(int i, const T& e);
	bool DListDelete(int i);

	bool GetElem(int i,T&e);
	int LocateElem(const T& e);

	int GetLength();
	bool Empty();
	void DispDList();

private:
	DNode<T>* m_head;
	int m_length;
};

template<class T>
DList<T>::DList()
{
	m_head = new DNode<T>;
	m_head->next = nullptr;
	m_head->prior = nullptr;
	m_length = 0;
}

template<class T>
bool DList<T>::DListInsert(int i, const T& e)
{
	if (i<1 || i>(m_length + 1))
	{
		cout << "不合法" << endl;
		return false;
	}

	DNode<T>* p_curr = m_head;
	for (int j = 0; j < (i - 1); j++)
	{
		p_curr = p_curr->next;
	}
	DNode<T>* node = new DNode<T>;
	node->data = e;
	node->next = p_curr->next;
	node->prior = p_curr;
	if (p_curr->next!= nullptr)
	{
		p_curr->next->prior = node;
	}
	p_curr->next = node;
	m_length++;

	cout << "插入成功" << endl;
	return true;

}

template<class T>
bool DList<T>::DListDelete(int i)
{
	if (i<1 || i>(m_length + 1)||m_length<1)
	{
		cout << "删除不合法" << endl;
		return false;
	}

	DNode<T>* p_curr = m_head;
	for (int j = 0; j < (i - 1); j++)
	{
		p_curr = p_curr->next;
	}
	DNode<T>* p_willptr = p_curr->next;
	p_curr->next = p_willptr->next;
	if (p_willptr->next != nullptr)
	{
		p_willptr->next->prior = p_curr;
	}
	delete p_willptr;
	m_length--;

	cout << "删除成功" << endl;
	return true;
}

template<class T>
bool DList<T>::GetElem(int i, T& e)
{
	if (i<1 || i>m_length || m_length < 1)
	{
		cout << "链表为空" << endl;
		return false;
	}

	DNode<T>* p_curr = m_head;
	for (int j = 0; j < (i - 1); j++)
	{
		p_curr = p_curr->next;
	}
	e = p_curr->next->data;
	cout << "查找值:" << p_curr->next->data << endl;
	return true;
}

template<class T>
int DList<T>::LocateElem(const T& e)
{
	DNode<T>* p_curr = m_head;
	for (int j = 0; j <= m_length; j++)
	{
		if (p_curr->next->data == e)
		{
			cout << "找到:" << p_curr->next->data << " ";
			return j;
		}
		p_curr = p_curr->next;
	}
	cout << "找不到" << endl;

	return -1;
}

template<class T>
int DList<T>::GetLength()
{
	return m_length;
}

template<class T>
void DList<T>::DispDList()
{
	DNode<T>* p = m_head->next;
	while (p != nullptr)
	{
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}

template<class T>
bool DList<T>::Empty()
{
	if (m_length == 0)
	{
		return true;
	}
	return false;
}

template<class T>
DList<T>::~DList()
{
	DNode<T>* pnode = m_head->next;
	DNode<T>* ptmp;
	while (pnode != nullptr)
	{
		ptmp = pnode;
		pnode = pnode->next;
		delete ptmp;
	}
	delete m_head;
	m_head = nullptr;
	m_length = 0;
}
#pragma once
#include<iostream>
using namespace std;

//双向循环链表

template<class T>
struct SNode
{
	T data;
	SNode<T>* prior;
	SNode<T>* next;
};

template<class T>
class SList
{
public:
	SList();
	~SList();

public:
	bool SListInsert(int i,const T& e);
	bool DListDelete(int i);

	bool GetElem(int i, T& e);
	int LocateElem(const T& e);

	int GetLength();
	bool Empty();
	void DispDList();

private:
	int m_length;
	SNode<T>* m_head;
};

template<class T>
SList<T>::SList()
{
	m_head = new SNode<T>;
	m_head->next = m_head;
	m_head->prior = m_head;
	m_length = 0;
}

template<class T>
bool SList<T>::SListInsert(int i, const T& e)
{
	if (i<1 || i>(m_length + 1))
	{
		cout << "不合法" << endl;
		return false;
	}

	SNode<T>* p_curr = m_head;
	for (int j = 0; j < (i - 1); j++)
	{
		p_curr = p_curr->next;
	}
	SNode<T>* node = new SNode<T>;
	node->data = e;
	node->next = p_curr->next;
	node->prior = p_curr;
	if (p_curr->next != m_head)
	{
		p_curr->next->prior = node;
	}
	p_curr->next = node;
	m_length++;

	cout << "插入成功" << endl;
	return true;
}
template<class T>
bool SList<T>::DListDelete(int i)
{
	if (i<1 || i>(m_length + 1))
	{
		cout << "不合法" << endl;
		return false;
	}

	SNode<T>* p_curr = m_head;
	for (int j = 0; j < (i - 1); j++)
	{
		p_curr = p_curr->next;
	}
	SNode<T>* p_willptr = p_curr->next;
	p_curr->next = p_willptr->next;
	if (p_willptr->next != m_head)
	{
		p_willptr->next->prior = p_curr;
	}
	delete p_willptr;
	m_length--;

	cout << "删除成功" << endl;
	return true;
}

template<class T>
bool SList<T>::GetElem(int i, T& e)
{
	if (i<1 || i>m_length || m_length < 1)
	{
		cout << "链表为空" << endl;
		return false;
	}

	SNode<T>* p_curr = m_head;
	for (int j = 0; j < (i - 1); j++)
	{
		p_curr = p_curr->next;
	}
	e = p_curr->next->data;
	cout << "查找值:" << p_curr->next->data << endl;
	return true;
}

template<class T>
int SList<T>::LocateElem(const T& e)
{
	SNode<T>* p_curr = m_head;
	for (int j = 0; j <= m_length; j++)
	{
		if (p_curr->next->data == e)
		{
			cout << "找到:" << p_curr->next->data << " ";
			return j;
		}
		p_curr = p_curr->next;
	}
	cout << "找不到" << endl;

	return -1;
}

template<class T>
int SList<T>::GetLength()
{
	return m_length;
}

template<class T>
bool SList<T>::Empty()
{
	if(m_head->next==m_head)
	{
		return true;
	}
	return false;
}

template<class T>
void SList<T>::DispDList()
{
	SNode<T>* p = m_head;
	while (p->next != m_head )
	{
		cout << p->next->data << " ";
		p = p->next;
	}
	cout << endl;
}

template<class T>
SList<T>::~SList()
{
	SNode<T>* pnode = m_head->next;
	SNode<T>* ptmp;
	while (pnode != m_head)
	{
		ptmp = pnode;
		pnode=pnode->next;
		delete ptmp;
	}
	delete m_head;
	m_head = nullptr;
	m_length = 0;
}

void test_List_D()//双链表
{
	DList<int> dl;
	//插入
	dl.DListInsert(1, 10);
	dl.DListInsert(1, 20);
	dl.DListInsert(1, 30);
	dl.DispDList();

	//删除
	dl.DListDelete(1);
	dl.DispDList();

	int val = 0;
	dl.GetElem(1, val);

	int pos = dl.LocateElem(10);
	if (pos != -1)
	{
		cout << "该值下标为:" << pos << endl;
	}
}
void test_List_S()//双向循环链表
{
	SList<int> sl;
	sl.SListInsert(1, 10);
	sl.SListInsert(1, 20);
	sl.DispDList();

	sl.DListDelete(1);
	sl.DispDList();

}
int main()
{
	test_List_D();
	test_List_S();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值