数据结构(CC++)——链式栈

    技术2022-08-01  88

    #include<stdio.h> #include<stdlib.h> typedef int ElemType; typedef struct Linknode { ElemType data; struct Linknode* next; }Linknode, *LiStack; bool InitStack(LiStack &L) { L = (Linknode *)malloc(sizeof(Linknode)); L->data = NULL; L->next = NULL; return true; } //带头节点的入栈 bool Hade_Push(LiStack& L, ElemType e) { Linknode* s = (Linknode*)malloc(sizeof(Linknode)); s->data = e; s->next = L->next; L->next = s; return true; } //带头节点的出栈 bool Hade_Pop(LiStack& L, ElemType &e) { if (L->next == NULL) return false; e = L->next->data; L->next = L->next->next; return true; } //不带头节点的入栈 bool Push(LiStack &L,ElemType e){ Linknode* s = (Linknode*)malloc(sizeof(Linknode)); s->next = L->next; L->next = s; s->data = L->data; L->data = e; return true; } //不带头节点的出栈 bool Pop(LiStack& L, ElemType &e) { if (L == NULL) return false; e = L->data; L = L->next; return true; } int main() { //带头节点的测试 //Linknode* L; //InitStack(L); //Hade_Push(L, 2); //Hade_Push(L, 3); //Hade_Push(L, 4); //ElemType e; //Hade_Pop(L, e); //printf("%d",e); //Hade_Pop(L, e); //printf("%d", e); //Hade_Pop(L, e); //printf("%d", e); //不带头节点的测试 Linknode* L; InitStack(L); Push(L, 2); Push(L, 3); Push(L, 4); ElemType e; Pop(L, e); printf("%d",e); Pop(L, e); printf("%d", e); Pop(L, e); printf("%d", e); return 1; }
    Processed: 0.021, SQL: 9