最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么打印的data不是36,而是0?

為什么打印的data不是36,而是0?

C
牧羊人nacy 2023-02-12 18:18:56
#include <stdio.h>#include <stdlib.h> #define fail 0 #define success 1 #define status int typedef struct node{ int data; struct node *next; }*Snode,*pStack,*LinkedStack; LinkedStack myStack=(LinkedStack)malloc(sizeof(struct node));pStack top; //初始化鏈棧 LinkedStack makeStack(LinkedStack myStack,pStack top) { myStack->data=-1; myStack->next=NULL; top=myStack; return myStack; } //判空 bool Stack_Empty(void) { return (top==myStack); } //入棧 status Push(LinkedStack myStack,int data) { LinkedStack cursor=myStack; Snode newNode=(Snode)malloc(sizeof(struct node)); newNode->data=data; newNode->next=NULL; top=newNode; while(cursor->next) { cursor=cursor->next; } cursor->next=newNode; //此處還要補上將該新結點插入鏈棧myStack的末尾 return success; } //出棧 int* Pop(LinkedStack myStack,int *data) { pStack cursor=myStack; if(Stack_Empty()) { } else { while(cursor->next!=top) { cursor=cursor->next; } data=&(top->data); top=cursor; return data; } } //程序測試 int main(void) { int *data=NULL; myStack=makeStack(myStack,top); Push(myStack,3); Push(myStack,8); Push(myStack,19); Push(myStack,31); Push(myStack,43); Push(myStack,36); Pop(myStack,data); printf("%d ",data); } 
查看完整描述

1 回答

?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

你的地址值傳遞錯誤,按照我給你修改的試試看,一定要注意你的實參傳遞的是地址還是具體的值,如果是地址,在子函數(shù)中就不要去修改你傳遞進來的地址。
int
main(void)
{
int
data=0;
//此處隨機分配內存空間,不要用指針
myStack=makeStack(myStack,top);
Push(myStack,3);
Push(myStack,8);
Push(myStack,19);
Push(myStack,31);
Push(myStack,43);
Push(myStack,36);
Pop(myStack,&data);//此處,傳遞的是地址值
printf("%d
",data);
}
int*
Pop(LinkedStack
myStack,int
*data)
{
pStack
cursor=myStack;
if(Stack_Empty())
{
}
else
{
while(cursor->next!=top)
{
cursor=cursor->next;
}
*data=top->data;
//此處,只是在同一個內存空間修改其值,所以不要取地址
top=cursor;
return
data;
}
}


查看完整回答
反對 回復 2023-02-15
  • 1 回答
  • 0 關注
  • 132 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號