[英]While loop doesnt work?
幾天前,我們在課堂上寫了這段代碼,老師試圖解釋它,我們大多數人都不了解。 到目前為止,我幾乎已經完全理解它了,但是為什么第二次在主菜單上不起作用? 彈出它之前,應該先輸出一個名字。
#include <stdio.h>
#include<stdlib.h>
typedef struct STK_S{
char name[100];
struct STK_S *next;
}STK;
int push(STK **ppS, STK *pD);
int pop(STK **ppS, STK *pD);
int main(){
STK *pS, d;
pS = NULL;
while (1){
printf_s("Ime ");
gets_s(d.name, 100);
if (d.name[0] == 0)
break;
push(&pS, &d);
}
while (pop(&pS, &d))
printf_s("\n%s", d.name);
return 0;
}
int push(STK **ppS, STK *pD){
STK *pt;
pt = (STK *)malloc(sizeof(STK));
if (pt == NULL)
return 0;
*pt = *pD;
pt->next = *ppS;
*ppS = pt;
return 1;
}
int pop(STK **ppS, STK *pD){
STK *pt;
if (*ppS == NULL){
printf("Empty stack.\n");
return NULL;
}
*pD = **ppS;
pt = *ppS;
*ppS = pt->next;
free(pt);
return 0;
}
pop
返回NULL
或0
,兩者都轉換為false
。 因此,循環不會運行一次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.