堆栈的单链表实现

来源:互联网 发布:量子网络代替互联网 编辑:程序博客网 时间:2024/06/05 18:59
#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; }
原创粉丝点击