栈的链表存储实现

来源:互联网 发布:js 是否是数字 编辑:程序博客网 时间:2024/04/29 18:14
typedef struct SNode *Stack;
struct SNode{
ElementType Data;
struct SNode *Next; 
}; 
Stack CreateStack()
{//构建一个堆栈的头结点,返回指针 
Stack S;
S=(Stack)malloc(sizeof(struct SNode));
S->Next=NULL;
return S;
}


int IsEmpty(Stack S)
{//判断堆栈S是否为空,若为空函数返回整数1,否则返回0 
return (S->Next=NULL);



void Push(ElementType item,Stack S)
{//将元素item压入堆栈S 
struct SNode *TmpCell;
TmpCell=(struct SNode *)malloc(sizeof(struct SNode));
TmpCell->Element=item;
TmpCell->Next=S->Next;
S->Next=TmpCell;
}
ElementType Pop(Stack S)
{//删除并返回堆栈S的栈顶元素 
struct SNode *FirstCell;
ElementType TopElem;
if(IsEmpty(s)) {
printf("堆栈空"); return NULL;
}else{
FirstCell=S->Next;
S->Next=FirstCell->Next;
TopElem=FirstCell->Element;
free(FirstCell);
return TopElem;
}
0 0
原创粉丝点击