flag45 链栈的书写以及规范性问题

xiaoxiao2021-02-28  7

# include <stdio.h> # include <malloc.h> typedef struct node { int  data; struct node* next; }Node,*Pnode; typedef struct stu { Pnode base; Pnode top,bottom; }Base,*Pbase; bool isempty(Pbase S); void init(Pbase S); void push(Pbase S,int val); void pop (Pbase S,int* val); void bianli(Pbase S); int main() { int val; Base S; init (&S); push(&S,1); push(&S,2); push(&S,3); push(&S,4); pop(&S,&val); printf("%d \n",val); bianli(&S); return 0; } void init(Pbase S) { S->base=(Pnode)malloc(sizeof(Node)); S->bottom=S->base; S->top=S->base; } void push(Pbase S,int val) { Pnode pnew=(Pnode)malloc(sizeof(Node)); pnew->data=val; pnew->next=S->top; S->top=pnew; } void pop (Pbase S,int* val) { if(!isempty(S)) { *val=S->top->data; Pnode r=S->top; S->top=S->top->next; free(r); } } bool isempty(Pbase S) { if(S->bottom==S->top) return true; else return false; } void bianli(Pbase S) { Pnode L=S->top; while(L!=S->bottom) { printf("%d ",L->data); L=L->next; } printf("\n"); }
转载请注明原文地址: https://www.6miu.com/read-1750124.html

最新回复(0)