作者在 2017-04-22 11:03:15 发布以下内容
#include<iostream>
#include<cstdlib>
#define LEN sizeof(struct Node)
using namespace std;
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Position;
struct Node //节点
{
int Element ;
struct Node *Next;
};
int IsEmpty(Position Top); //检验空栈
void GetStackTop(Position Top); //获取栈顶元素
Position Create(void); //创建一个栈
void Traverse(Position Top); //遍历
void Push(Position Top); //出栈
void Pop(Position Top); //入栈
int main() //测试
{
Position Top=Create();
Traverse(Top);
Push(Top);
return 0;
}
int IsEmpty(Position Top)
{
return Top->Next==NULL;
}
Position Create(void)
{
int i,a[8]={15,16,21,9,54,66,24,59}; //创建一个8个元素的栈
Position Top=NULL;
Top=(struct Node *)malloc(LEN);
Top->Next=NULL;
Top->Element=0;
for(i=0;i<8;i++)
{
Position New=(struct Node *)malloc(LEN);
if(New==NULL)
cout <<"空间不足" <<endl ;
New->Element=a[i]; //赋值
New->Next=Top; //New与原先的栈顶连接
Top=New; //New成为新的栈顶
}
return Top;
}
void Traverse(Position Top)
{
if(IsEmpty(Top))
cout <<"空栈!" <<endl ;
else
{
GetStackTop(Top);
while(Top->Next!=NULL) //栈底连接的是NULL
{
cout << Top->Element <<' ';
Top=Top->Next;
}
cout <<"遍历完毕" <<endl ;
}
}
void GetStackTop(Position Top)
{
if(IsEmpty(Top))
cout <<"空栈!" <<endl ;
else
cout <<"栈顶元素是:" <<Top->Element <<endl ;
}
void Push(Position Top)
{
int X;
Position New=NULL;
cout <<"请输入要入栈的元素:" ;
cin >>X ;
New=(struct Node *)malloc(LEN);
if(New==NULL)
cout <<"空间不足!" <<endl ;
New->Element=X; //跟创建的时候的插入是一样的
New->Next=Top;
Top=New;
cout <<"入栈后:" ;
Traverse(Top);
}
void Pop(Position Top) //出栈
{
int X;
Position Deletion=NULL;
if(IsEmpty(Top))
cout <<"空栈!" <<endl ;
else
{
cout <<"出栈后: " ;
Top=Top->Next; //栈顶的下面一个元素变成栈顶
Traverse(Top);
}
}
#include<cstdlib>
#define LEN sizeof(struct Node)
using namespace std;
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Position;
struct Node //节点
{
int Element ;
struct Node *Next;
};
int IsEmpty(Position Top); //检验空栈
void GetStackTop(Position Top); //获取栈顶元素
Position Create(void); //创建一个栈
void Traverse(Position Top); //遍历
void Push(Position Top); //出栈
void Pop(Position Top); //入栈
int main() //测试
{
Position Top=Create();
Traverse(Top);
Push(Top);
return 0;
}
int IsEmpty(Position Top)
{
return Top->Next==NULL;
}
Position Create(void)
{
int i,a[8]={15,16,21,9,54,66,24,59}; //创建一个8个元素的栈
Position Top=NULL;
Top=(struct Node *)malloc(LEN);
Top->Next=NULL;
Top->Element=0;
for(i=0;i<8;i++)
{
Position New=(struct Node *)malloc(LEN);
if(New==NULL)
cout <<"空间不足" <<endl ;
New->Element=a[i]; //赋值
New->Next=Top; //New与原先的栈顶连接
Top=New; //New成为新的栈顶
}
return Top;
}
void Traverse(Position Top)
{
if(IsEmpty(Top))
cout <<"空栈!" <<endl ;
else
{
GetStackTop(Top);
while(Top->Next!=NULL) //栈底连接的是NULL
{
cout << Top->Element <<' ';
Top=Top->Next;
}
cout <<"遍历完毕" <<endl ;
}
}
void GetStackTop(Position Top)
{
if(IsEmpty(Top))
cout <<"空栈!" <<endl ;
else
cout <<"栈顶元素是:" <<Top->Element <<endl ;
}
void Push(Position Top)
{
int X;
Position New=NULL;
cout <<"请输入要入栈的元素:" ;
cin >>X ;
New=(struct Node *)malloc(LEN);
if(New==NULL)
cout <<"空间不足!" <<endl ;
New->Element=X; //跟创建的时候的插入是一样的
New->Next=Top;
Top=New;
cout <<"入栈后:" ;
Traverse(Top);
}
void Pop(Position Top) //出栈
{
int X;
Position Deletion=NULL;
if(IsEmpty(Top))
cout <<"空栈!" <<endl ;
else
{
cout <<"出栈后: " ;
Top=Top->Next; //栈顶的下面一个元素变成栈顶
Traverse(Top);
}
}