-2
想與社區覈對該函數是否能正常工作get_end()
。 A typedef struct queue
有一個頭部和一個尾巴,如下所示。C中的堆棧函數移除結束函數
stack
有一個頭部和一個尾巴,頭部始終指向stack
的「前/後」,而尾部跟蹤「尾部/頂部」以去除尾部。該結構是一個通用術語,在這種情況下是一個int結構。
get_end()
要比編寫pop
和push
函數更棘手,並且希望與社區一起檢查此函數是否正確。
typedef struct node{
int a, b;
struct node *next;
}Node;
typedef struct stack{
Node* head;
Node* tail;
}Stack;
Node* get_end(Stack* q){
Node *right, *left, *temp;
right = (Node*) malloc(sizeof(Node));
left = (Node*) malloc(sizeof(Node));
if(q->head == NULL && q->tail == NULL){
temp = NULL;
return temp;
}
else if(q->head == q->tail){
temp = q->tail;
q->head = q->tail = NULL;
return temp;
}
else{
right = q->head;
while(right->next != NULL){
left = right;
right = right->next;
}
left->next = NULL;
q->tail = left;
return right;
}
}
@BLUEPIXY我將編輯這個 – JJL
堆棧溢出不是一個調試或教程服務。這是一個問答網站。參加[遊覽]並查看[問]。 – Olaf
我真的很想知道如何編寫這樣的代碼。無論如何,標準評論:不要在C語言中輸入'malloc'和朋友的結果! – Olaf