下面使用单链表实现链式栈:
形态:
实现:
/*******************************************
链式栈实现
by Rowandjj
2014/4/9
*******************************************/
#include<iostream>
using namespace std;
#define OVERFLOW -1
#define OK 1
#define ERROR 0
typedef int Status;
typedef int ElemType;
typedef struct _NODE_
{
ElemType data;
struct _NODE_ *next;
}Node,*pNode,*Stack;
//------------基本操作----------------------
Status InitStack(Stack &S);//构造一个空栈
Status Push(Stack &S,ElemType e);
Status Pop(Stack &S,ElemType &e);
Status StackTraverse(Stack S,void (*visit)(ElemType));//遍历
Status GetTop(Stack S,ElemType &e);//获取栈顶元素
void visit(ElemType e);
//-------------具体实现---------------------
Status InitStack(Stack &S)
{
S = (pNode)malloc(sizeof(Node));
if(!S)
{
exit(OVERFLOW);
}
S->next = NULL;
return OK;
}
Status Push(Stack &S,ElemType e)
{
pNode pNew = (pNode)malloc(sizeof(Node));
if(!pNew)
{
exit(OVERFLOW);
}
pNew->data = e;
pNew->next = S->next;
S->next = pNew;
return OK;
}
Status Pop(Stack &S,ElemType &e)
{
pNode p = S->next;
if(p)
{
e = p->data;
S->next = p->next;
free(p);
return OK;
}
return ERROR;
}
Status StackTraverse(Stack S,void (*visit)(ElemType))
{
pNode p = S->next;
while(p)
{
visit(p->data);
p = p->next;
}
cout<<endl;
return OK;
}
Status GetTop(Stack S,ElemType &e)
{
pNode p = S->next;
if(p)
{
e = p->data;
return OK;
}
else
{
return ERROR;
}
}
void visit(ElemType e)
{
cout<<e<<" ";
}
下面使用单循环链表实现链式栈:
很简单,就不画图了.
/***********************************************
链式栈实现(使用单循环链表)
by Rowandjj
2014/4/9
***********************************************/
#include<IOSTREAM>
using namespace std;
#define OVERFLOW -1
#define OK 1
#define ERROR 0
typedef int Status;
typedef int ElemType;
typedef struct _NODE_
{
ElemType data;
struct _NODE_ *next;
}Node,*Stack,*pNode;
//------------基本操作----------------------
Status InitStack(Stack &S);//构造一个空栈
Status Push(Stack &S,ElemType e);
Status Pop(Stack &S,ElemType &e);
Status StackTraverse(Stack S,void (*visit)(ElemType));//遍历
Status GetTop(Stack S,ElemType &e);//获取栈顶元素
Status StackEmpty(Stack S);
Status ClearStack(Stack S);
void visit(ElemType e);
//-------------具体实现-----------------------
Status InitStack(Stack &S)
{
S = (pNode)malloc(sizeof(Node));
if(!S)
{
exit(OVERFLOW);
}
S->next = S;
return OK;
}
Status Push(Stack &S,ElemType e)
{
pNode pNew = (pNode)malloc(sizeof(Node));
if(!pNew)
{
exit(OVERFLOW);
}
pNew->data = e;
pNew->next = S->next;
S->next = pNew;
return OK;
}
Status Pop(Stack &S,ElemType &e)
{
pNode p = S->next;
if(p)
{
e = p->data;
S->next = p->next;
free(p);
return OK;
}
return ERROR;
}
Status GetTop(Stack S,ElemType &e)
{
pNode p = S->next;
if(p != S)
{
e = p->data;
return OK;
}
return ERROR;
}
Status StackTraverse(Stack S,void (*visit)(ElemType))
{
pNode p = S->next;
while(p != S)
{
visit(p->data);
p = p->next;
}
cout<<endl;
return OK;
}
void visit(ElemType e)
{
cout<<e<<" ";
}
Status StackEmpty(Stack S)
{
return S->next == S;
}
Status ClearStack(Stack S)
{
pNode p = S->next,q;
while(p != S)
{
q = p->next;
free(p);
p = q;
}
S->next = S;
return OK;
}