當前位置:編程學習大全網 - 網站源碼 - C語言 求鏈棧的深度

C語言 求鏈棧的深度

#include<stdio.h>

#include<malloc.h>

#define?null?0

typedef?struct?node{

int?data;

struct?node?*next;

}linkstack;

linkstack?*top;

linkstack?*push(linkstack?*top,int?x){

linkstack?*p;

p=(linkstack*)malloc(sizeof(linkstack));

p->data=x;

p->next=top;

top=p;

return?top;

}

linkstack*?pop(linkstack?*top){

linkstack?*p;

if(!top){

printf("空鏈棧");

return?null;

}

p=top;

top=top->next;

free(p);//釋放存儲空間

return?top;

}

void?print(linkstack?*top){

linkstack?*p;

p=top;

while(p!=null){

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

p=p->next;

}

}

int?StackDepth(linkstack?*S){

linkstack?*p;

int?cnt;

p=S->next;

for(cnt=0;p!=NULL;cnt++){

p=p->next;

}

return?cnt;

}

//為了簡單取消了創建等操作,妳的代碼可能是其他地方出問題了

void?main(){

int?x,i;

top=(linkstack*)?malloc(sizeof(linkstack));

scanf("%d",&x);

top->data=x;

top->next=null;

for(i=1;i<=3;i++){

scanf("%d",&x);

top=push(top,x);

}

int?a?=?StackDepth(top);

printf("length:%d",a);

return?0;

}

  • 上一篇:農業物聯網與食品安全溯源有什麽聯系?
  • 下一篇:php文件加密是幹什麽的?
  • copyright 2024編程學習大全網