欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

The LinkStNode for Linked stack (Continuous updates) | Data

程序员文章站 2022-04-25 19:30:52
...

The code in Data book (5th Edition) from the 83 page to 86 page

Continuous updates

#define MaxSize 50
typedef int ElemType;

typedef struct linknode {
    ElemType data;		//Data domain
    struct linknode *next;	//Pointer domain
} LinkStNode;

//1 Initialize the stack
void InitStack(LinkStNode *&s) {
    s=(LinkStNode *)malloc(sizeof(LinkStNode));
    s->next=NULL;
}

//2 Destroy stack
void DestroyStack(LinkStNode *&s) {
    LinkStNode *p=s, *q=s->next;
    while (q!=NULL) {
        free(p);
        p=q;
        q=p->next;
    }
    free(p);	//Free up its space
}

//3 Determine if the stack is empty
bool StackEmpty(LinkStNode *s) {
    return(s->next==NULL);
}

//4 Into the stack
void Push(LinkStNode *&s,ElemType e) {
    LinkStNode *p;
    p=(LinkStNode *)malloc(sizeof(LinkStNode));
    p->data=e;
    p->next=s->next;
    s->next=p;
}

//5 Out stack
bool Pop(LinkStNode *&s,ElemType &e) {
    LinkStNode *p;
    if (s->next==NULL)	
    return false;
    p=s->next;
    e=p->data;
    s->next=p->next;
    free(p);
    return true;
}

//6 Top element of the stack
bool GetTop(LinkStNode *s,ElemType &e) {
    if (s->next==NULL)	
        return false;
    e=s->next->data;
    return true;
}

如有侵权,请联系作者删除

相关标签: Data