码迷,mamicode.com
首页 > 编程语言 > 详细

链栈的基本操作 C语言

时间:2017-10-25 13:23:08      阅读:342      评论:0      收藏:0      [点我收藏+]

标签:init   str   顺序   des   main   stdio.h   c语言   tac   基本   

#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct Stack *LStack;
struct Stack {
    ElemType data;
    LStack next;
};
//初始化
LStack initStack() {
    LStack L;
    L = (LStack)malloc(sizeof(struct Stack));
    L->next = NULL;
    printf("初始化成功\n");
    return L;
}
//入栈
int InStack(LStack L, ElemType e) {
    LStack S;
    S = (LStack)malloc(sizeof(struct Stack));
    S->data = e;
    S->next = NULL;               
    S->next = L->next;        //这种插法逻辑顺序和输入顺序相反,输入123,从头指针开始遍历时是321
    L->next = S;              
}
//出栈
int DeStack(LStack L) {
    if (L->next == NULL) {
        printf("空栈\n");
        return 0;
    }
    ElemType e;
    LStack P;
    P = L->next;
    e = P->data;
    L->next = P->next;
    printf("出栈元素为%d\n", e);
    free(P);
    return 1;
}
//求栈长
int LengthStack(LStack L) {
    int i = 0;
    L = L->next;
    while (L) {
        i++;
        L = L->next;
    }
    printf("栈长为%d\n", i);
}
int main() {
    LStack L;
    L = initStack();
    InStack(L, 1);
    InStack(L, 2);
    InStack(L, 3);
    DeStack(L);
    DeStack(L);
    DeStack(L);
    DeStack(L);
    LengthStack(L);
}

 

链栈的基本操作 C语言

标签:init   str   顺序   des   main   stdio.h   c语言   tac   基本   

原文地址:http://www.cnblogs.com/yudongxuan/p/7728195.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!