标签: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); }
标签:init str 顺序 des main stdio.h c语言 tac 基本
原文地址:http://www.cnblogs.com/yudongxuan/p/7728195.html