#pragma once
#include<iostream>
using namespace std;
class Node
{
public:
Node();
~Node();
friend class Stack;
private:
int data;
Node *next;
};
class Stack
{
public:
Stack();
~Stack();
void Pop();
void Push();
bool IsEmpty();
void Show_Stack();
private:
Node *head;
};
#include"堆栈的链式存储.h"
#include<iostream>
using namespace std;
Node::Node()
{
}
Node::~Node()
{
}
Stack::Stack()
{
this->head = new Node;
this->head->next = NULL;
}
Stack::~Stack()
{
}
void Stack::Pop()
{
int x = 0;
if (this->IsEmpty())
{
cout << "堆栈为空!" << endl;
}
else
{
Node *ptemp = this->head->next;
this->head->next = ptemp->next;
x = ptemp->data;
delete ptemp;
cout << "出栈数据为" << x << endl;
}
}
void Stack::Push()
{
Node *ptemp = new Node;
int x = 0;
cout << "请输入你想要压栈的数据:" << endl;
cin >> x;
ptemp->data = x;
ptemp->next = this->head->next;
this->head ->next= ptemp;
cout << "数据压栈成功" << endl;
}
bool Stack::IsEmpty()
{
return this->head->next == NULL ? true : false;
}
void Stack::Show_Stack()
{
Node*p = this->head->next;
while (p!= NULL)
{
cout << p->data << " ";
p = p->next;
}
cout<< endl;
}
#include"堆栈的链式存储.h"
#include <iostream>
using namespace std;
int main()
{
Stack s;
int i = 9;
while (i!=0)
{
cout << "1.压栈 2.出栈 3.遍历 4.判断是否栈空 0.退出" << endl;
cin >> i;
switch (i)
{
case 0:
break;
case 1:
s.Push();
break;
case 2:
s.Pop();
break;
case 3:
s.Show_Stack();
break;
case 4:
s.IsEmpty();
break;
}
}
}