Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 如何仅显示列表中的前10个输入?_C_List_Struct_Singly Linked List_Function Definition - Fatal编程技术网

C 如何仅显示列表中的前10个输入?

C 如何仅显示列表中的前10个输入?,c,list,struct,singly-linked-list,function-definition,C,List,Struct,Singly Linked List,Function Definition,我在一个struct列表中插入了一些细节,每个fourplet表示一个包含源、目标、生成时间和卷号的数据包? 这是我的display函数,在main()中我只调用她:display();在主代码的末尾。我怎样才能只显示列表中的前10个数据包(四个数据包),这是我之前使用下面的插入功能插入的数据包 struct Packet{ int rollnumber; int src; int dest; double gentime; struct Packet *next; }* head;

我在一个struct列表中插入了一些细节,每个fourplet表示一个包含源、目标、生成时间和卷号的数据包? 这是我的display函数,在main()中我只调用她:display();在主代码的末尾。我怎样才能只显示列表中的前10个数据包(四个数据包),这是我之前使用下面的插入功能插入的数据包

struct Packet{
 int rollnumber;
 int src;
 int dest;
 double gentime;
 struct Packet *next;
 }* head;    

void display(){
 struct Packet * temp = head;
 while(temp!=NULL){
    printf("Roll Number: %d\n",temp->rollnumber);
    printf("src: %d\n", temp->src);
    printf("dest: %d\n", temp->dest);
    printf("gentime: %0.1f\n\n", temp->gentime);
    temp = temp->next;
    }
 }

 void insert(int rollnumber, int src, int dest, double gentime){
  struct Packet * packet = (struct Packet *) malloc(sizeof(struct Packet));
  packet->rollnumber = rollnumber;
  packet->src=src;
  packet->dest=dest;
  packet->gentime = gentime;
  packet->next = NULL;

  if(head==NULL){
    head = packet;
  }
  else{
    packet->next = head;
    head = packet;
   }
  }

用以下方法编写函数
显示

void display( size_t n )
{
    if ( n == 0 ) n = ( size_t )-1;

    for ( struct Packet * temp = head; n-- && temp != NULL; temp = temp->next )
    {  
        printf("Roll Number: %d\n",temp->rollnumber);
        printf("src: %d\n", temp->src);
        printf("dest: %d\n", temp->dest);
        printf("gentime: %0.1f\n\n", temp->gentime);
    }
}
并调用函数,例如

display( 10 );

当使用等于
0
的参数调用该函数时,该函数将输出整个列表。

问题是什么?仅显示10个数据包。我可以对插入函数执行同样的操作吗?@wajaap您可以做任何您想做的事情。:)我尝试了它,但它没有显示任何内容,这使我认为它与插入函数的工作方式不同。@wajaap您可以在新问题中提出新问题,关闭此问题并选择最佳答案。