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;
}
相关推荐
Matlab光学5 小时前
MATLAB仿真:离轴干涉法实现光学全息加密与解密
开发语言·matlab
小鸡吃米…6 小时前
Python - JSON
开发语言·python·json
JAVA+C语言6 小时前
C#——接口
开发语言·c#
黎雁·泠崖6 小时前
吃透指针通用用法:回调函数与 qsort 的使用和模拟
c语言·开发语言
whn19776 小时前
达梦数据库的整体负载变化查看
java·开发语言·数据库
脏脏a6 小时前
聊聊 C 里的进制转换、移位操作与算术转换
c语言·开发语言·移位操作符
陳10306 小时前
C++:string(4)
开发语言·c++
ZHang......6 小时前
synchronized(三)
开发语言·笔记·juc
许泽宇的技术分享6 小时前
AgentFramework:错误处理策略
开发语言·c#
阿里嘎多学长6 小时前
2025-12-21 GitHub 热点项目精选
开发语言·程序员·github·代码托管