#include <stdio.h>
#include <malloc.h>
#define ElementType int
/*堆栈的链式存储实现*/
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)
{ //堆栈为空返回1,否则返回0
return (S->Next==NULL);
}
void Push(ElementType item,Stack S)
{
struct SNode *TmpCell;
TmpCell=(Stack)malloc(sizeof(struct SNode));
TmpCell->Data=item;
TmpCell->Next=S->Next;
S->Next=TmpCell;
}
ElementType Pop(Stack S)
{
struct SNode *FirstCell;
ElementType TopElem;
if(IsEmpty(S))
{
printf("stack is empty.");
return NULL;
}else{
FirstCell=S->Next;
S->Next=FirstCell->Next;
TopElem=FirstCell->Data;
free(FirstCell);
return TopElem;
}
}
int main()
{
return 0;
}
堆栈的单链表实现
最新推荐文章于 2023-07-17 18:48:38 发布