Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.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
C 访问链接列表中的结构_C_Pointers_Struct_Linked List - Fatal编程技术网

C 访问链接列表中的结构

C 访问链接列表中的结构,c,pointers,struct,linked-list,C,Pointers,Struct,Linked List,我有一个指向链表中第一个结构的指针,但是我怎么能只打印出链表的第五个结构呢 struct human { char name[STR]; char manuf[STR]; int age; float weight; struct human *next; } struct human *current; //Points to current structure. struct human *first = NULL; ...... for

我有一个指向链表中第一个结构的指针,但是我怎么能只打印出链表的第五个结构呢

struct human {
    char name[STR]; 
    char manuf[STR];
    int age;
    float weight;
    struct human *next; 
}

struct human *current; //Points to current structure.
struct human *first = NULL;

......

 for (current = first; current != NULL; current = current->next) {
     fprintf(stdout, "%s  %s  %d  %f \n", out->name, out->surname,
             out->age, out->weight);
 }
链表由100个结构组成,所有结构都按顺序排序,但如何仅打印链表的第4和第7个结构

struct human {
    char name[STR]; 
    char manuf[STR];
    int age;
    float weight;
    struct human *next; 
}

struct human *current; //Points to current structure.
struct human *first = NULL;

......

 for (current = first; current != NULL; current = current->next) {
     fprintf(stdout, "%s  %s  %d  %f \n", out->name, out->surname,
             out->age, out->weight);
 }

“first”将指向链表的第一个元素(它是通过代码中的某个函数定义的)。这段代码将打印出链表中的每一个结构,但我只想打印出选定的结构,如第四个和第七个。我怎样才能做到这一点呢?

您应该首先通过一些youtube视频了解链表遍历。 你需要从头开始

int position= 4;//considering u need to get the 4th element
int output;// stores the final output
struct human *temp= current;
for(i=0; i<position-1;i++)
{
   temp=temp->next;
}
您可能还希望在for循环中执行某些错误检查,如-

for(i=0; i<position-1;i++)
{
   temp=temp->next;
   if(temp==NULL)
   {
     break;
   }
}
(i=0;不精确; if(temp==NULL) { 打破 } }
您可以定义跳过函数,例如

struct human * skip(struct human * head, int num)
{
    while (num-- > 0)
    {
        head = head -> next;
    }
    return head;
}

struct human * fifth = skip(first, 4);
fprintf(stdout, "%s  %s  %d  %f \n", out->name, out->surname,
         out->age, out->weight);
如果要打印非连续节点,则可以执行以下操作:

int markers[] = { 3, 5, 7};
int index = 0;

for (current = first; current != null ; current = current -> next, index++)
{
    int j;
    for (j=0; j < sizeof(markers)/sizeof(markers[0]); j ++)
    {
       if (markers[j] == index)
       {
             fprintf(stdout, "%s  %s  %d  %f \n", out->name, out->surname,
                              out->age, out->weight);
       }
    }
}
int标记[]={3,5,7};
int指数=0;
对于(当前=第一个;当前!=null;当前=当前->下一个,索引++)
{
int j;
对于(j=0;j姓名,输出->姓氏,
外出->年龄,外出->体重);
}
}
}

首先,您需要定义如何获取这些数字;它们是以静态方式定义的,还是由用户定义的

一旦正确,您可以将这些数字存储到从最小到最大的数组中,并在循环中检查迭代器是否等于数组中的下一个数字

int myNumbers[] = {1,2,5};//You will set this values as mentioned in the first place
int iterator = 0, indexToNext=0;
for(current = first; current != NULL; current = current->next, iterator++){
     if (iterator == myNumbers[indextToNext]){
         fprintf(stdout, "%s  %s  %d  %f \n", out->name, out->surname,
                 out->age, out->weight);
         indexToNext++;
     }
}

一个简单的解决方案是使用一种方法,该方法采用整数的顺序数组,指定要打印的位置,如[4,7]。记住要考虑数组的大小。数组的顺序很重要,否则必须在链表中循环多次才能获得所有位置

然后,像您正在做的那样,创建一个循环,增加数组中的计数器和位置。如果计数器==位置[x]打印。当数组完成或链表完成时,中断循环


您可能还需要检查数组中的int是否大于链表的大小。

只需前进到下一个节点,但要到达目标节点需要前进多少次。如果您想(例如,第三个节点),则必须前进到下一个节点2次。一次从节点1到节点2,另一次从否de 2到节点3。您的for循环正是这样做的,但它不计算从一个节点到下一个节点的遍历次数。