⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stack.cpp

📁 简单的栈实现的源码 第一个VC++程序
💻 CPP
字号:
#include <iostream.h>
#include "Stack.h"

Stack::Stack()
{
	head=NULL;
}

void Stack::put(int item)
{
	node* p,* temp;//p为新建的结点,temp用于遍历这样就不会更改head的值
	p=new node;
	p->data=item;
	p->next=NULL;//空指针必须大写
	if (head==NULL)//"=="比较运算符,"="是赋值
		head=p;
	else
	{
		temp=head;
		while(temp->next!=NULL)//遍历
			temp=temp->next;
		temp->next=p;
	}
}

int Stack::get()
{
	node* Pnext,* Plast;//Pnext指向链表最后一个结点,Plast指向Pnext前一个结点
	int temp;
	Pnext=head;
	while(Pnext->next!=NULL)//遍历
	{
		Plast=Pnext;
		Pnext=Pnext->next;
	}
	temp=Pnext->data;
	delete Pnext;//回收Pnext的空间
	Plast->next=NULL;
	return temp;
}

void main()
{
	Stack st;

	st.put(10);
	st.put(12);
	st.put(14);

	cout<<st.get()<<endl;
	cout<<st.get()<<endl;
}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -