链栈

#include "stdafx.h"
#include<iostream>

typedef int ElemType;
using namespace std;

typedef struct stacknode {
    ElemType data;
    struct stacknode * next;
}StackNode;

typedef struct {
    StackNode *top;
}LinkStack;

LinkStack *InitStack() {
    LinkStack *s;
    s = (LinkStack*)malloc(sizeof(LinkStack));
    s->top = NULL;
    return s;
}

void Push_LinkStack(LinkStack*s, ElemType elem) {
    StackNode *p;
    p = (StackNode*)malloc(sizeof(StackNode));
    p->next = s->top;
    p->data = elem;
    s->top = p;
}

int Pop_LinkStack(LinkStack *s, ElemType *elem) {
    StackNode *p;
    if (s->top == NULL) return 0;
    else {
        p = s->top;
        *elem = p->data;
        s->top = p->next;
        free(p);
        return 1;
    }
}

int main() {
    LinkStack *s = InitStack();
    int e=0;
    for (int i = 0; i < 5; i++) {
        cin >> e;
        Push_LinkStack(s, e);
    }
    ElemType item;
    Pop_LinkStack(s, &item);
    cout << item<<endl;

    system("pause");
    return EXIT_SUCCESS;
}

 

posted @ 2019-05-24 00:30  消失的白桦林  阅读(143)  评论(0编辑  收藏  举报