#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);

}

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

 

相关文章:

  • 2021-11-03
  • 2022-12-23
  • 2022-12-23
  • 2018-12-01
  • 2021-04-08
  • 2021-08-20
  • 2021-08-14
  • 2022-12-23
猜你喜欢
  • 2021-05-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-19
  • 2021-07-21
相关资源
相似解决方案