通常,在我們的單鏈表推送函式中,我們必須檢查我們是否正在查看尾節點,以及它是否已被賦值,對嗎?因為如果它是尾節點還沒有被賦值,我們不應該創建一個新節點;我們必須簡單地為其存盤變數分配一個值。在其余情況下 - 我們必須在分配之前創建一個。
但是檢查尾部(具有垃圾值/零/未使用/用戶未觸及)的正確方法是什么? 或者是否有其他所有人都在使用的其他方法來解決所有這些問題(包括在推入 SLL 時將未使用的尾部視為特殊情況) ?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct ListNode
{
int val;
struct ListNode *next;
};
void linked_init(struct ListNode **p)
{
(*(p)) = malloc(sizeof(struct ListNode));
(*(p))->next = NULL;
//Basically saying that for every linked list that I'll create and initialize, using the "ListNode" struct definition, I will assign to...
//...its Tail Node, a "Unique Code (int value)", which will indicate that no SENSICAL value had been assigned to the tail, yet.
(*(p))->val = 98989;
}
void linked_push(struct ListNode **p, int curval)
{
//If it's the tail, and within the tail, the storage hasn't been used - aka - a usable int value hasn't been assigned.
if ((*(p))->next == NULL && (*(p))->val == 98989)
{
//Push it into the already existing Tail Node
(*(p))->val = curval;
}
else
{
//It's either not a tail, or it is indeed a tail which has already been assigned a value.
//Therefore, we must create a new node before assigning into its tail node, a value.
struct ListNode *temp;
temp = malloc(sizeof(struct ListNode));
temp->val = curval;
temp->next = (*(p));
(*(p)) = temp;
}
}
void linked_display(struct ListNode *p)
{
struct ListNode *temp;
temp = p;
while (temp)
{
printf("%d", (temp)->val);
printf("\n");
temp = temp->next;
}
}
int main(int argc, char **argv)
{
struct ListNode *list;
linked_init(&list);
// Initial push would be targeted at the tail, since we were the ones who initialized it; using the linked_init() function.
// This whole thing only works if the linked list belonged to us; if we initialized it.
// If we are applying these functions on a list imported from external sources - ex. from questions on online coding platforms, etc - then this wouldn't work.
linked_push(&list, 14);
linked_push(&list, 17);
linked_push(&list, 20);
linked_push(&list, 90);
linked_display(list);
return 0;
}
這就是我目前的做法,但我認為這不是一種實用的方法。我在網上找不到有關此問題的任何資訊。
uj5u.com熱心網友回復:
沒有必要分配一個具有魔法值的節點作為尾節點。空串列只是一個空指標。使用哨兵值是令人困惑和容易出錯的:應該忽略提倡這種方法的教程。
這是一個簡化版本:
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
void linked_init(struct ListNode **p) {
*p = NULL;
}
void linked_push(struct ListNode **p, int curval) {
// allocate a new node and insert it at the head of the list
struct ListNode *temp = malloc(sizeof(*temp));
if (temp) {
temp->val = curval;
temp->next = *p;
*p = temp;
}
}
int linked_pop(struct ListNode **p) {
struct ListNode *temp = *p;
int val = 0;
if (temp) {
val = temp->val;
*p = temp->next;
free(temp);
}
return val;
}
void linked_display(const struct ListNode *p) {
const struct ListNode *temp;
for (temp = p; temp; temp = temp->next) {
printf("%d ", temp->val);
}
printf("\n");
}
int main(int argc, char **argv) {
struct ListNode *list;
// Initialize the list. Could just write: struct ListNode *list = NULL;
linked_init(&list);
// Insert values
linked_push(&list, 14);
linked_push(&list, 17);
linked_push(&list, 20);
linked_push(&list, 90);
// Print the list values: 90 20 17 14
linked_display(list);
// Free the list.
while (list) {
linked_pop(&list);
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/520287.html
標籤:C单链表
