stack_link.cpp
来自「这是用链表实现的栈数据结构。链表数据结构的最大优点是不受元素个数的限制」· C++ 代码 · 共 87 行
CPP
87 行
#include<iostream.h>
#define null 0
template <typename T>
class Stack_L
{
private:
struct node{
T data;
struct node * link; //
};
struct node *top;
public:
Stack_L() //
{top=null;}
~Stack_L() //
{
struct node * temp; //
while(top)
{
temp=top->link;
delete top;
top=temp;
}
}
bool Add(const T item) //add a new node
{
struct node * temp=new node;
if(temp){
temp->data=item;
temp->link=top;
top=temp;
return true;
}
else
{cout<<"Out of space!"<<endl;return false;}
}
bool Delete(T & item) //delete the top node
{
if(StackEmpty())
{ cout<<"Stack is empty!"<<endl;return false;}
else
{ struct node * temp;
item=top->data;
temp=top;
top=top->link;
delete temp;
return true;
}
}
bool StackEmpty() //is the stack empty?
{
if(top) return false;
else return true;
}
};
void main()
{
Stack_L<double> C;
double i=1;
while(i<=20)
{
C.Add((i+0.5));
i++;
}
while(!C.StackEmpty())
{
C.Delete(i);
cout<<i<<" "<<endl;}
cout<<endl;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?