/*BiTree_Stack.h 操作二叉树使用的栈*/
#include "commen.h"
int InitStack(LinkStack &head){
head=(StackNode *)malloc(sizeof(StackNode));
if(!head)
exit(-1);
head->next = NULL;
return TRUE;
}
int StackEmpty(LinkStack head){
if(head->next == NULL) return TRUE;
return FALSE;
}
int Push(LinkStack head, ElemType element){
StackNode *newp;
newp = (StackNode *)malloc(sizeof(StackNode));
if(newp == NULL) return FALSE;
newp->data=element;
newp->next = head->next;
head->next = newp;
return TRUE;
}
int Pop(LinkStack head, ElemType &element){
if(StackEmpty(head)) return FALSE;
LinkStack newp = head->next;
element = newp->data;
head->next = newp->next;
free(newp);
return TRUE;
}
int GetTop(LinkStack head, ElemType &element){
if(head->next!=NULL)
{
element = head->next->data;
return TRUE;
}
//element='#';
return FALSE;
}
int StackLength(LinkStack head)
{
LinkStack p;
int i=0;
p=head->next;
while(p)
{
i++;
p=p->next;
}
return i;
}
Status ClearStack(LinkStack &S)
{
LinkStack p;
while(S->next)
{
p=S->next;
S->next=S->next->next;
delete p;
}
return TRUE;
}
Status DestroyStack(LinkStack &head)
{
if(!ClearStack(head))
return false;
delete head;
return TRUE;
}
#include "commen.h"
int InitStack(LinkStack &head){
head=(StackNode *)malloc(sizeof(StackNode));
if(!head)
exit(-1);
head->next = NULL;
return TRUE;
}
int StackEmpty(LinkStack head){
if(head->next == NULL) return TRUE;
return FALSE;
}
int Push(LinkStack head, ElemType element){
StackNode *newp;
newp = (StackNode *)malloc(sizeof(StackNode));
if(newp == NULL) return FALSE;
newp->data=element;
newp->next = head->next;
head->next = newp;
return TRUE;
}
int Pop(LinkStack head, ElemType &element){
if(StackEmpty(head)) return FALSE;
LinkStack newp = head->next;
element = newp->data;
head->next = newp->next;
free(newp);
return TRUE;
}
int GetTop(LinkStack head, ElemType &element){
if(head->next!=NULL)
{
element = head->next->data;
return TRUE;
}
//element='#';
return FALSE;
}
int StackLength(LinkStack head)
{
LinkStack p;
int i=0;
p=head->next;
while(p)
{
i++;
p=p->next;
}
return i;
}
Status ClearStack(LinkStack &S)
{
LinkStack p;
while(S->next)
{
p=S->next;
S->next=S->next->next;
delete p;
}
return TRUE;
}
Status DestroyStack(LinkStack &head)
{
if(!ClearStack(head))
return false;
delete head;
return TRUE;
}