栈的最主要的特性:后进先出(Last in First Out),简称LIFO线性表。
由于栈也是线性表,因此线性表的存储结构对栈也适用,通常栈有顺序栈和链栈两种存储结构,这两种存储结构的不同,则使得实现栈的基本运算的算法也有所不同。其结构如下所示:
栈结构图
栈的链式存储实现代码如下:
#include <iostream>
#include <windows.h>
using namespace std;
typedef struct Stack
{
int number;
Stack *pNext;
}ST;
ST *pHead=NULL;
ST *PEnd=NULL;
ST *ps=NULL;
void Push(int num);
void Pop();
bool Empty();
void Length();
void Traverse();
void Clear_Stack();
void Destory_Stack();
void Get_Top();
void Push(int num)//入栈
{
ps=new ST;
ps->number=num;
if(pHead==NULL)
{
ps->pNext=NULL;
pHead=ps;
PEnd=pHead;
}
else
{
ps->pNext=pHead;
pHead=ps;
}
}
void Pop()//出栈;
{
ST *temp=NULL;
if(Empty())
{
cout<<"the stack is NULL!"<<endl;
return;
}
else
{
temp=pHead;
cout<<"出栈数据:"<<pHead->number<<" ";
pHead=pHead->pNext;
delete temp;
}
}
void Get_Top() //得到栈顶元素;
{
if(Empty())
{
cout<<"the stack is NULL!"<<endl;
return;
}
else
cout<<"THE top: "<<pHead->number<<endl;
}
bool Empty() //是否为空;
{
if(NULL==pHead)
return true;
else
return false;
}
void Length() //栈里的元素;
{
ST *p=pHead;
int count=1;
if(Empty())
{
cout<<"the stack is NUL!"<<endl;
return;
}
for(p;p->pNext;p=p->pNext)
count++;
cout<<"the length of the stack: "<<count<<endl;
}
void Traverse() //遍历栈;
{
ST *p=pHead;
if(Empty())
{
cout<<"the stack is NULL!"<<endl;
return;
}
while(p)
{
cout<<p->number<<" ";
p=p->pNext;
}
}
void Clear_Stack()//清空栈
{
if(Empty())
{
cout<<"the stack is NULL!"<<endl;
return;
}
while(!Empty())
{
Pop();
}
cout<<"the stack has been cleared!"<<endl;
}
void Destory_Stack() //销毁栈;
{
ST *temp=NULL;
if(Empty())
{
cout<<"the stack is NULL!"<<endl;
return;
}
while(pHead)
{
temp=pHead;
pHead=pHead->pNext;
delete temp;
}
cout<<"the stack has been destoryed!"<<endl;
}
void Create_Stack() //创建栈;
{
int num;
cout<<"please input the number to stack,end with 0: "<<endl;
cin>>num;
while(num!=0)
{
Push(num);
cin>>num;
}
}
void menue()
{
while(1)
{
int choice=0;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN);
//设置代码输出的颜色代码,green,red,blue;
cout<<"请选择你要执行的操作:"<<endl<<endl;
cout<<"1.插入数据到栈:"<<endl;
cout<<"2.遍历栈:"<<endl;
cout<<"3.返回栈顶的元素:"<<endl;
cout<<"4.栈的长度:"<<endl;
cout<<"5.出栈:"<<endl;
cout<<"6.入栈:"<<endl;
cout<<"7.清空:"<<endl;
cout<<"8.销毁栈:"<<endl;
cout<<"9.退出:"<<endl;
cin>>choice;
switch(choice)
{
case 1:Create_Stack();
break;
case 2:Traverse();
break;
case 3:Get_Top();
break;
case 4:Length();
break;
case 5:Pop();
break;
case 6:
int num;
cout<<"please input the data to stack,end with 0:"<<endl;
cin>>num;
while(0!=num)
{
Push(num);
cin>>num;
}
break;
case 7:Clear_Stack();
break;
case 8:Destory_Stack();
break;
case 9:
return;
default:
cout<<"你正确输入你要执行的操作:"<<endl;
}
}
}
int main()
{
menue();
system("pause");
return 0;
}
运行结果:
栈的顺序存储:
实现代码如下:
#include <iostream>
#include <windows.h>
using namespace std;
#define FULL 20
#define EMPTY -1
#define MAXSIZE 20
static int top=-1;
int data[MAXSIZE];
void Pop();
void Push();
bool Empty();
bool Full();
void Clear_Stack();
void InStack();
void Get_Top();
void Length();
void Traverse();
void Push(int number)
{
if(Full())
{
cout<<"the stack is full!"<<endl;
return ;
}
else
{
top++;
data[top]=number;
}
}
void Pop()
{
if(Empty())
{
cout<<"the stack is NULL!"<<endl;
return ;
}
else
{
cout<<"出栈的元素:"<<data[top]<<" ";
top--;
}
}
void Get_Top()
{
if(Empty())
{
cout<<"THE stack is null!"<<endl;
return ;
}
else
{
cout<<"the top: "<<data[top];
cout<<endl;
}
}
void Clear_Stack()
{
data[MAXSIZE]=0;
cout<<"the stack has cleared!"<<endl;
}
void Length()
{
cout<<"栈的长度:"<<top+1<<endl;
}
bool Empty()
{
if(EMPTY>=top)
return true;
else
return false;
}
bool Full()
{
if(MAXSIZE<=top)
return true;
else
return false;
}
void Traverse()
{
int count=top;
if(Empty())
{
cout<<"THE stack is NULL!"<<endl;
return;
}
else
{
while(count>EMPTY)
{
cout<<data[count]<<" ";
count--;
}
}
}
void InStack()
{
int num;
cout<<"please input the data to stack,end with 0:"<<endl;
cin>>num;
while(0!=num)
{
Push(num);
cin>>num;
}
}
void menue()
{
while(1)
{
int choice=0;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN);
//设置代码输出的颜色代码,green,red,blue;
cout<<"请选择你要执行的操作:"<<endl<<endl;
cout<<"1.插入数据到栈:"<<endl;
cout<<"2.遍历栈:"<<endl;
cout<<"3.返回栈顶的元素:"<<endl;
cout<<"4.栈的长度:"<<endl;
cout<<"5.出栈:"<<endl;
cout<<"6.入栈:"<<endl;
cout<<"7.清空栈:"<<endl;
cout<<"8.退出:"<<endl;
cin>>choice;
switch(choice)
{
case 1:InStack();
break;
case 2:Traverse();
break;
case 3:Get_Top();
break;
case 4:Length();
break;
case 5:Pop();
break;
case 6:
int num;
cout<<"please input the data to stack,end with 0:"<<endl;
cin>>num;
while(0!=num)
{
if(Full())
{
cout<<"the stack is full!"<<endl;
return;
}
Push(num);
cin>>num;
}
break;
case 7:Clear_Stack();
case 8:
return;
default:
cout<<"你正确输入你要执行的操作:"<<endl;
}
}
}
int main()
{
menue();
system("pause");
return 0;
}
运行结果: