用C++实现链表操作

首先写两个类1.Node类,表示节点,2.List类表示链表

class Node
{
public:
	Node():m_next(NULL){}//用冒号语法初始化
	Node(int v):m_value(v),m_next(NULL){}
	int m_value;//值
	Node* m_next;//指针指向下一个结点
};
class List
{
public:
	List():m_pHead(NULL) ,m_pTail(NULL),m_size(0){}
	void InsertHead(int v);//头插
	void InsertTail(int v);//尾插
	void InsertPosValue(int pos, int v);//在pos位置插入值v
	void DelHead();//头删
	void DelTail();//尾删
	void DelPos(int pos);//删除pos位置的结点
	Node* GetPosPtr(int pos);//获取pos位置的结点,返回值为指针类型
	Node* SearchValue(int value);//查找
	void Show();//输出链表
	Node* Reverse();//反转链表
	int Size()const//获取结点个数
	{
		return m_size;
	}
	bool IsEmpty()const//判空
	{
		return m_pHead == NULL;
	}
private:
	Node* m_pHead;//头结点
	Node* m_pTail;//尾结点
	int m_size;//结点个数
};

具体的成员函数

头插

void List ::InsertHead(int v)
{
	Node* p = new Node(v);
	if (m_pHead == NULL)//头结点为空,则头结点和尾结点都是p
		m_pHead = m_pTail = p;
	else
	{
		p->m_next = m_pHead;
		m_pHead = p;//p为新的头结点
	}
	m_size++;
}

尾插

void List::InsertTail(int v)
{
	Node* p = new Node(v);
	if (m_pHead == NULL)
	{
		m_pHead = m_pTail = p;
	}
	else
	{
		m_pTail->m_next = p;
		m_pTail = p;
	}
}

pos位置插入一个结点

void List::InsertPosValue(int pos, int v)
{
	if (pos == 1)
		InsertHead(v);
	else if (pos == m_size + 1)
		InsertTail(v);
	else if (pos > 1 && pos <= m_size)
	{
		Node* q = GetPosPtr(pos - 1);//pos的前驱
		Node* p = new Node(v);
		p->m_next=q->m_next;
		q->m_next = p;
	}
	else
		cout << "pos不合法" << endl;
}

头删

void List::DelHead()
{
	Node* p = m_pHead;
	if (IsEmpty())
		return;
	if (m_pHead->m_next == NULL)
	{
		m_pHead = m_pTail = NULL;
	}
	else
	{
		m_pHead = p->m_next;
	}
	delete(p);
	p = NULL;
	m_size--;
}

尾删

void List::DelTail()
{
	Node* q = m_pTail;
	if (IsEmpty())
		return;
	else if (m_pHead->m_next == NULL)
	{
		m_pHead = m_pTail = NULL;
	}
	else
	{
		Node* p = GetPosPtr(m_size-1);
		p->m_next = NULL;
		m_pTail = p;
	}
	delete(q);//释放空间
	q = NULL;
	m_size--;
}

删除pos位置的结点

void List::DelPos(int pos)
{
	if (pos == 1)
		DelHead();
	else if (pos == m_size)
		DelTail();
	else if (pos > 1 && pos < m_size)
	{
		Node* p = GetPosPtr(pos-1);//删除要找前驱(没有前驱删结点的方法主页也有)
		Node* q = p->m_next;
		p->m_next = q->m_next;
		delete(q);
		q = NULL;
		m_size--;
	}
	else
		return;
}

获取pos位置的结点

Node* List::GetPosPtr(int pos)
{
	if (pos<1 || pos>m_size)
		return NULL;
	Node* p = m_pHead;
	while (pos-- > 1)
	{
		p = p->m_next;
	}
	return p;
}

查找某值的结点位置

Node* List::SearchValue(int value)
{
	Node* p = m_pHead;
	while (p || p->m_value != value)
	{
		p = p->m_next;
	}
	return p;
}

打印该链表

void List::Show()
{
	Node* p = m_pHead;
	while (p)
	{
		cout << p->m_value << "->";
		p = p->m_next;
	}
	cout << "NULL" << endl;
}

反转链表

Node* List::Reverse()
{
	if (m_pHead == NULL)
		return NULL;
	Node* newhead = m_pHead;//newhead是新的头结点
	m_pTail = m_pHead;
	Node* p = m_pHead->m_next;
	newhead->m_next = NULL;//必须要给后面置空,不然可能形成双向链表
	while (p)
	{
		Node* q = p->m_next;//保存下一个结点
		p->m_next = newhead;
		newhead = p;
		p = q;
	}
	m_pHead = newhead;//这个必须写,C语言不用写
	return newhead;
}

主函数以及测试

void main() 
{
	List ll;
	ll.DelTail();
	ll.DelHead();
	ll.InsertPosValue(1, 100);
	ll.DelHead();
	ll.InsertTail(10);
	ll.DelPos(1);
	ll.InsertHead(1);
	ll.InsertHead(2);
	ll.InsertHead(3);
	ll.InsertHead(4);
	ll.InsertHead(5);
	//Node *q=ll.Reverse();
	ll.Show();
	/*Node* p = ll.GetPosPtr(3);
	if (p)
	{
		cout << p->m_value << endl;
	}*/
}

你可能感兴趣的:(C++,c++)