链栈的进栈,出栈,头插法

#include

#include

typedef struct LNode{

int data;

   struct LNode *next;

}LNode; 

struct LNode *initstack(LNode *L){//注意函数类型 struct LNode

L=(LNode *)malloc(sizeof(LNode));

L->next=NULL;

return L;

}

struct LNode * push(LNode *L)

{

LNode *P;

int a,b;

printf("输入栈长:"); 

scanf("%d",&b);

for(int i=0;i

P=(LNode *)malloc(sizeof(LNode));

printf("输入数据:"); 

scanf("%d",&a);

//头插法 

P->data=a;

P->next=L->next;

L->next=P;

return L; 

}

 int pop(LNode *L){

  LNode *p; //需要设置指针,否则报错 

      p=L->next;  

      while(p!=NULL)  

      {  

        printf("%d ",p->data);  

        p=p->next;  

      }  

// while(L->next!=NULL){

// printf("%d",L->data) ;

// L=L->next;

// } 区别这段代码与上面的区别;这段代码会报错,LNode *p;设置了一个指针 

 } 

int main(){

LNode *L,*L1;

L=initstack(L);

L1=push(L);

pop(L1);

}

链栈的进栈,出栈,头插法
 

猜你喜欢

转载自blog.csdn.net/ganghaodream/article/details/88092178