C++:用类实现链表,队列,栈

用类实现链表:

cpp 复制代码
#include<iostream>
using namespace std;

class List
{
	struct Node
	{
		int Nval;
		Node* pNext;
	};
private:
	Node* pHead = NULL;
	Node* pEnd = NULL;
public:
	void AddNode(int n);
	void DelNode(int n);
	void walk();
};
void List::AddNode(int k)
{
	Node* ptemp = new Node;
	ptemp->Nval = k;
	ptemp->pNext = NULL;
	if (NULL == pHead)
	{
		pHead = ptemp;
	}
	else
	{
		pEnd->pNext = ptemp;
	}
	pEnd = ptemp;
}
void List::DelNode(int n)
{
	Node* pdel = NULL;
	if (pHead->Nval == n)
	{
		pdel = pHead;
		pHead = pHead->pNext;
		free(pdel);
		pdel = NULL;
		return;
	}
	Node* pmark = pHead;
	while (pmark->pNext != NULL)
	{
		if (pmark->pNext->Nval == n)
		{
			pdel = pmark->pNext;
			pmark->pNext = pmark->pNext->pNext;
			free(pdel);
			pdel = NULL;
			if (pmark->pNext == NULL)
			{
				pEnd = pmark;
			}
			return;
		}
		pmark = pmark->pNext;
	}
}
void List::walk()
{
	while (pHead != NULL)
	{
		cout << pHead->Nval << endl;
		pHead = pHead->pNext;
	}
}

int main()
{
	List list;
	list.AddNode(1);
	list.AddNode(2);
	list.AddNode(3);

	list.DelNode(1);
	list.DelNode(2);
	list.walk();


	return 0;
}

用类实现队列:

cpp 复制代码
#include<iostream>
using namespace std;

class Queue
{
	struct Node
	{
		int Nval;
		Node* pNext;
	};
private:
	Node* pHead = NULL;
	Node* pEnd = NULL;
public:
	void Push(int n);
	int pop();

};
void Queue::Push(int n)
{
	Node* pTemp = new Node;
	pTemp->Nval = n;
	pTemp->pNext = NULL;

	if (pHead == NULL)
	{
		pHead = pTemp;
	}
	else
	{
		pEnd->pNext = pTemp;
	}
	pEnd = pTemp;
}
int Queue::pop()
{
	if (pHead != NULL)
	{
		Node* pdel = pHead;
		pHead = pHead->pNext;
		int n = pdel->Nval;
		free(pdel);
		pdel = NULL;
		if (NULL == pHead)
		{
			pEnd = NULL;
		}
		return n;
	}
	return -1;
}


int main()
{
	Queue yan;
	yan.Push(1);
	yan.Push(2);
	yan.Push(3);

	cout << yan.pop() << endl;
	cout << yan.pop() << endl;
	cout << yan.pop() << endl;


	return 0;
}

用类实现栈:

cpp 复制代码
#include<iostream>
using namespace std;

class List
{
	struct Node
	{
		int n;
		Node* pnext;
	};
private:
	Node* ptop = NULL;
public:
	void push(int n);
	int pop();
};
void List::push(int n)
{
	Node* ptemp = new Node;
	ptemp->n = n;
	ptemp->pnext = NULL;
	if (ptop != NULL)
	{
		ptemp->pnext = ptop;
		ptop = ptemp;
	}
	else
	{
		ptop = ptemp;
	}
}
int List::pop()
{
	if (ptop != NULL)
	{
		Node* pdel = ptop;
		ptop = ptop->pnext;
		int k = pdel->n;
		free(pdel);
		pdel = NULL;
		
		return k;
	}
	return -1;
}

int main()
{
	List k;
	k.push(1);
	k.push(2);
	k.push(3);

	cout << k.pop()<< endl;
	cout << k.pop() << endl;
	cout << k.pop() << endl;

	return 0;
}
相关推荐
一律清风1 小时前
【Opencv】canny边缘检测提取中心坐标
c++·opencv
belldeep3 小时前
如何阅读、学习 Tcc (Tiny C Compiler) 源代码?如何解析 Tcc 源代码?
c语言·开发语言
LuckyTHP3 小时前
java 使用zxing生成条形码(可自定义文字位置、边框样式)
java·开发语言·python
a东方青5 小时前
蓝桥杯 2024 C++国 B最小字符串
c++·职场和发展·蓝桥杯
XiaoyaoCarter6 小时前
每日一道leetcode
c++·算法·leetcode·职场和发展·二分查找·深度优先·前缀树
Blossom.1186 小时前
使用Python实现简单的人工智能聊天机器人
开发语言·人工智能·python·低代码·数据挖掘·机器人·云计算
da-peng-song6 小时前
ArcGIS Desktop使用入门(二)常用工具条——数据框工具(旋转视图)
开发语言·javascript·arcgis
galaxy_strive6 小时前
qtc++ qdebug日志生成
开发语言·c++·qt
TNTLWT6 小时前
Qt功能区:简介与安装
开发语言·qt
Darkwanderor7 小时前
c++STL-list的模拟实现
c++·list