栈(链式存储)
typedef struct SNode *PtrToSNode;
struct SNode{
ElemType data;
PtrToSNode next;
};
typedef PtrToSNode Stack;
stack CreateStack(){
Stack s;
s=(Stcak)malloc(sizeof(struct SNode));
s->next=NULL;
return s;
}
bool isEmpty(){
return s->next==null;
}
bool push(Stack s,ElemType e){
PtrToSNode temp;
temp=(PtrToSNode)malloc(sizeof(struct SNode));
temp->data=e;
temp->next=s->next;
s->next=temp;
return ture;
}
ElemType pop(Stack s){
PtrToSNode first;
ElemType topElem;
if(isEmpty()){
print("栈空");
return ERROR;
}
else {
first=s->next;
topElem=first->data;
s->next=first->next;
free(first);
return topElem;
}
}
本文详细介绍了使用链式存储方式实现栈数据结构的过程,包括创建空栈、判断栈是否为空、元素入栈和出栈等核心操作。通过具体的C语言代码示例,深入解析了链式栈的内部机制。
3881

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



