#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct Linknode{
int data;
struct Linknode *next;
} StackNode,*SqStack;
SqStack InitStack(SqStack&L)
{
L=new StackNode;
L->data=1;
L->next=NULL;
return L;
}
bool GetTop(SqStack L,int &x)
{
if(L==NULL) return false;
x=L->data;
return true;
}
bool isEmpty(SqStack L)
{
if(L==NULL) return true;
else return false;
}
void Push(SqStack &L,int x)
{
StackNode *s=new StackNode;
s->data=x;
s->next=L;
L=s;
}
void Pop(SqStack &L)
{
StackNode *s=L;
L=L->next;
free(s);
}
void PrintStack(SqStack L)
{
SqStack s=L;
while(s)
{
printf("%d\n",s->data);
s=s->next;
}
}
int main()
{
SqStack L;
bool logo;
L=InitStack(L);
Push(L,2);
Push(L,3);
Push(L,4);
Pop(L);
int x;
GetTop(L,x);
logo=isEmpty(L);
cout<<"the top of the Stack is "<<x<<endl;
PrintStack(L);
return 0;
}
c++数据结构栈的链式结构
最新推荐文章于 2024-07-14 01:31:01 发布