Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用for循环在C中创建链表以赋值_C_For Loop_Linked List_Malloc_Structure - Fatal编程技术网

使用for循环在C中创建链表以赋值

使用for循环在C中创建链表以赋值,c,for-loop,linked-list,malloc,structure,C,For Loop,Linked List,Malloc,Structure,我正在尝试使用for循环为我的程序创建一个链表,该循环分配值。在创建这个链表时,我希望能够跟踪头部,并将for循环中的第一个值指定给头部。例如,如果我正在创建一个从0到n-1的列表,我希望头部指向0,列表的其余部分后跟1-2-3-4-…-n-1。我已经编写了一个循环,可以实现这个功能,但是for循环必须倒计时,而不是倒计时。这是我的密码: // Structure typedef struct node { int value; struct node * next; } ListNod

我正在尝试使用for循环为我的程序创建一个链表,该循环分配值。在创建这个链表时,我希望能够跟踪头部,并将for循环中的第一个值指定给头部。例如,如果我正在创建一个从0到n-1的列表,我希望头部指向0,列表的其余部分后跟1-2-3-4-…-n-1。我已经编写了一个循环,可以实现这个功能,但是for循环必须倒计时,而不是倒计时。这是我的密码:

// Structure
typedef struct node {
  int value;
  struct node * next;
} ListNode;

  int size = "some value"; 

  ListNode * head = NULL; // beginning of the linked list
  ListNode * temp; // temporary node  

  for(int count = size - 1; count >= 0; count--)
  {
    // creates temporary nodes //
    ListNode * tempNode = malloc(sizeof(ListNode));
    tempNode -> value = count;
    tempNode -> next = NULL;
    // creation of node completed

    temp = tempNode;
    temp -> next = head;
    head = temp;
  }

虽然在这个程序中,正如我所希望的,头部指向0,但是有没有一种方法可以让for循环从0开始一直到n,并且仍然产生相同的输出。我希望它看起来像(int表示count=0;count 首先,在代码中,您不需要额外的
tempNode
,只需使用
temp
并将其设置为内部块的局部:

for (int count = size; count--; ) {
    ListNode *temp = malloc(sizeof(*temp));

    temp->value = count;
    temp->next = head;
    head = temp;
}
如果要在末尾附加元素,则应保留指向最后一个节点的指针,即
尾部

ListNode *head = NULL;
ListNode *tail = NULL;

for (int count = 0; count < size; count++) {
    ListNode *temp = malloc(sizeof(*temp));

    temp->value = count;
    temp->next = NULL;

    if (tail == NULL) {
        head = temp;
        tail = temp;
    } else {
        tail->next = temp;
        tail = temp;
    }
}
ListNode*head=NULL;
ListNode*tail=NULL;
对于(int count=0;count数值=计数;
temp->next=NULL;
if(tail==NULL){
压头=温度;
尾=温度;
}否则{
尾部->下一步=温度;
尾=温度;
}
}
有一种更优雅的方法可以做到这一点:与其保留指向最后一个节点的指针,不如保留指向下一个元素应该去的空指针的指针:

ListNode *head = NULL;
ListNode **tail = &head;

for (int count = 0; count < size; count++) {
    ListNode *temp = malloc(sizeof(*temp));

    temp->value = count;
    temp->next = NULL;

    *tail = temp;
    tail = &(*tail)->next;
}
ListNode*head=NULL;
ListNode**tail=&head;
对于(int count=0;count数值=计数;
temp->next=NULL;
*尾=温度;
尾部=&(*尾部)->下一步;
}
开始时,
*tail
保存
head
的地址,然后保存最后一个节点的
下一个
成员的地址。您可以通过指针
tail
更新这两个列表,而无需检查列表是否为空

最后一种方法最初看起来有点吓人,它的
ListNode**tail
,但一旦你掌握了窍门,它就是一个有用的工具。如果您对它感到不舒服,请使用第一种变体

仅仅创建列表转发是否值得?插入到前面的列表很容易,在整理之后,我觉得您原来的变体看起来干净紧凑