#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;
}
堆栈的单链表实现
最新推荐文章于 2022-04-21 12:58:56 发布
本文介绍了一种使用链式存储结构实现堆栈的方法。通过C语言编程,定义了堆栈的基本操作,包括创建堆栈、判断堆栈是否为空、元素入栈及出栈等。该实现方式适用于动态变化的数据集合。
312

被折叠的 条评论
为什么被折叠?



