的code(链表C)说明 [英] Explanation of code (linked list C)

查看:139
本文介绍了的code(链表C)说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是不是我的code。我把这个code关闭此网站:

<一个href=\"http://www.macs.hw.ac.uk/~rjp/Coursewww/Cwww/linklist.html\">http://www.macs.hw.ac.uk/~rjp/Coursewww/Cwww/linklist.html

我使用的参考材料关于如何建立一个链表。我对正在发生的事情有点困惑。可有人请向我解释是怎么回事。我还是选什么困惑我用1-5。

 #包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;结构list_el {
   INT VAL;
   结构list_el *接下来的;
};typedef结构list_el项目;无效的主要(){
   项目* CURR,*头;
   INT I;   头= NULL; // 1   对于(i = 1; I&LT; = 10;我++){
      CURR =(项目*)malloc的(sizeof的(项目));
      curr-&GT; VAL = I;
      curr-&gt;接下来=头; // 2
      头= CURR; // 3
   }   CURR =头; // 4   而(CURR){// 5
      的printf(%d个\\ N,curr-&GT; VAL);
      CURR = curr-&gt;接下来,
   }


  1. 头= NULL→为什么头被设置为NULL?我知道,你应该(我这样做是出于习惯),但我真的不知道为什么。


  2. curr-> =旁边头→我从来没有真正理解这一点。也许我有我的头的定义错了,但在常规的链表,它是起始节点或名单上的最后一个节点?我一直以为这是起始节点,但在这条线上,它看起来像它的最后一个节点。


  3. 头= CURR→我们为什么要设置它等于CURR?


  4. CURR =头部→再设置CURR =头部循环完成后。


  5. 而(CURR)→只是为了确保,这是通过遍历列表,它等同于同时(CURR!= NULL)吧?



解决方案

  

#1:头= NULL


初始化指针。它的一般的建议初始化指针为NULL是(1)在声明或(2)声明后。如果程序员错误地取消引用未初始化的指针,返回垃圾值。很多时候,这是非常难于调试,如果静态分析和编译器不显示未初始化的指针,警告或错误信息。

有关详细信息,请参阅史蒂夫·麦康奈尔的的 code齐全:实用软件建设手册或在维基百科页面的防御性编程


  

2: curr-&gt;接下来=头


构建链表。序列中的 CURR 节点正在被挂到$​​ P $ pviously创建的节点。


  

3: =头CURR


更新头指针。指针被更新为指向最近的malloc 编辑节点。

下面说明可视化的步骤#2和#3:





  

#4: CURR =头


重新初始化指针:此步骤是类似于步骤2: curr-&gt;接下来=头。通过 CURR 节点设置为 CURR 获得准备在,而循环链表遍历。类比来说,这就像在循环的开始初始化迭代变量为0(即 I = 0 )。为了形象化这一步,请参考下面的插图显示前/执行该语句后:


  

#5:而(CURR)


遍历列表。
鉴于 CURR 指向第一个节点(从步骤#4),本,而循环遍历列表,直到 curr-&gt;接下来返回NULL。在不太抽象的形式,我们可以重写这个语句而(CURR!= NULL)

This is not my code. I took this code off this website:

http://www.macs.hw.ac.uk/~rjp/Coursewww/Cwww/linklist.html

I am using for reference material on how to build a linked list. I'm a little confused on what is going on. Can someone please explain to me what is going on. I'll mark what is confusing me with 1-5.

#include<stdlib.h>
#include<stdio.h>

struct list_el {
   int val;
   struct list_el * next;
};

typedef struct list_el item;

void main() {
   item * curr, * head;
   int i;

   head = NULL;   //1

   for(i=1;i<=10;i++) {
      curr = (item *)malloc(sizeof(item));
      curr->val = i;
      curr->next  = head; //2
      head = curr; //3
   }

   curr = head; // 4

   while(curr) {  //5
      printf("%d\n", curr->val);
      curr = curr->next ;
   }

  1. head = NULL → why is head being set to NULL? I know that you are supposed to (I do it out of habit) but I don't really know why.

  2. curr->next = head → I never really understood this as well. Maybe I have my definition of "head" wrong but in a regular linked list, is it the starting node or the last node on the list? I've always assumed it was the starting node but in this line it looks like it's the last node.

  3. head = curr → Why are we setting it equal to curr?

  4. curr = head → and then setting curr = head after the loop is done.

  5. while(curr) → Just to make sure, this is traversing through the list and it is equivalent to while(curr != NULL) right?

解决方案

#1: head = NULL

Initializing the pointer. It's generally recommended to initialize the pointer to NULL either (1) at declaration or (2) immediately after declaration. If programmers mistakenly dereference uninitialized pointers, garbage values are returned. Often times, this is extremely hard to debug if your static analyzer and compiler don't display warning or error messages for uninitialized pointers.

For more information, please refer to Steve McConnell's Code Complete: A Practical Handbook of Software Construction or Wikipedia page on Defensive Programming.

#2: curr->next = head

Building the linked list. The curr node is being "linked" to previously created node in the sequence.

#3: head = curr

Updating the head pointer. The head pointer is being updated to point to the most recently malloced node.

Below illustrations visualize Steps #2 and #3:

#4: curr = head

Re-initializing the pointer. This step is similar to step #2:curr->next = head. By setting curr node to head, curr gets "ready" for linked-list traversal in the while loop. Analogically speaking, it's like initializing the iterating variable to 0 in the beginning of the loop (i.e. i = 0). To visualize this step, please refer to the below illustrations showing before/after this statement is executed:

#5: while(curr)

Traversing the list. Given that curr is pointing to the first node (from step #4), this while loop traverses the list until curr->next returns NULL. In a less abstract form, we can rewrite this statement as while(curr != NULL).

这篇关于的code(链表C)说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆