有人可以幫助我瞭解在下列功能中發生了什麼? 具體使用s1-> top?而s1-> top在功能上的動作是什麼,按& pop &顯示?因爲如果在功能推動中,每當一個數字被推動時,s1-> top向右移動?那麼爲什麼在顯示功能中,它表示s1-> top在遍歷中是第一個,而在s1-> top中是n在右邊,而在打印值時,我們需要先在左邊然後遍歷。爲什麼?有人可以幫助我瞭解在下列功能中發生了什麼?
Stack createStack() {
Stack s1;
s1 = (Stack) malloc(sizeof(Stack_Head));
s1 - > count = 0;
s1 - > top = NULL;
return s1;
}
Nodeptr createNode(dataitem item) {
Nodeptr temp;
temp = (Nodeptr) malloc(sizeof(Node));
temp - > data = item;
temp - > next = NULL;
return temp;
}
void push(Stack s1, dataitem item) {
Nodeptr temp = createNode(item);
temp - > next = s1 - > top;
s1 - > top = temp;
s1 - > count++;
}
void display(Stack s1) {
Nodeptr ptr = s1 - > top;
while (ptr1 = NULL) {
printf("%d", ptr - > data);
ptr = ptr - > next;
}
printf("\n");
}
void pop(Stack s1) {
Nodeptr temp;
if (isEmpty(s1))
printf("List is Empty");
else {
temp = s1 - > top;
s1 - > top = temp - > next;
temp - > next = NULL;
free(temp);
s1 - > count;
}
int isEmpty(Stack s1) {
return s1 - > top == NULL;
}
你想看看'Stack'類型的定義。 – alk
由於可怕的縮進,我沒有閱讀代碼。 –
你的意思是' - >'運算符嗎?你不知道它是什麼?也許你應該閱讀[點(。)運算符和 - >在C++中有什麼區別?](http://stackoverflow.com/questions/1238613/what-is-the-difference-between-the-dot- operator-and-in-c)(如果這是主要問題,可能應該將其投票重複)。 –