如何只显示显示功能所代表的一个列表?
How can I display only one list represented from the display function?
基本上我有 10 个名为 src(=source) 的文件,每个文件都有数据包(为我的作业命名),由卷号、dest(=目的地)和 gentime(=生成时间)表示。我希望每个文件都有一个列表,所以有 10 个不同的列表,它们将传递数据包。但是,当我显示它们时,我必须可以选择只显示一个列表,例如列表 3,它将用于src 文件 3. 我的代码如下。
void output( Node **head )
{
for( Node *current =*head; current != NULL; current = current->next )
{
//printf( "%d %d %d %0.1f ",current->rollnumber, current->src, current->dst, current->gentime );
printf("Roll Number:%2d\t",current->rollnumber);
printf("src:%2d\t", current->src);
printf("dest:%2d\t", current->dst);
printf("gentime:%0.1f\n", current->gentime);
}
printf( "%s\n", "NULL" );
}
void display( Node **set, size_t n )
{
for ( size_t i = 0; i <= n; i++ )
{
output( set++ );
putchar( '\n' );
}
}
输出为:
NULL
Roll Number: 8 src: 1 dest:10 gentime:89.1
Roll Number: 7 src: 1 dest:12 gentime:76.5
Roll Number: 6 src: 1 dest:10 gentime:64.1
Roll Number: 5 src: 1 dest: 4 gentime:51.5
Roll Number: 4 src: 1 dest:17 gentime:38.0
Roll Number: 3 src: 1 dest:20 gentime:25.9
Roll Number: 2 src: 1 dest:15 gentime:13.9
Roll Number: 1 src: 1 dest: 3 gentime:1.6
NULL
Roll Number: 8 src: 2 dest:12 gentime:90.6
Roll Number: 7 src: 2 dest: 6 gentime:78.1
Roll Number: 6 src: 2 dest:17 gentime:64.8
Roll Number: 5 src: 2 dest: 6 gentime:52.6
Roll Number: 4 src: 2 dest: 5 gentime:39.5
Roll Number: 3 src: 2 dest:20 gentime:26.0
Roll Number: 2 src: 2 dest:19 gentime:14.0
Roll Number: 1 src: 2 dest: 4 gentime:1.9
NULL
Roll Number: 8 src: 3 dest: 5 gentime:89.8
Roll Number: 7 src: 3 dest: 1 gentime:75.9
Roll Number: 6 src: 3 dest: 8 gentime:63.9
Roll Number: 5 src: 3 dest:14 gentime:50.8
Roll Number: 4 src: 3 dest:11 gentime:38.4
Roll Number: 3 src: 3 dest:16 gentime:25.7
Roll Number: 2 src: 3 dest:18 gentime:13.4
Roll Number: 1 src: 3 dest: 7 gentime:1.2
NULL
以此类推,直到 src:10
此外,推送功能:
int push_front( Node **head, int rollnumber, int src, int dst, double gentime )
{
Node *new_node = malloc( sizeof( Node ) );
int success = new_node != NULL;
if ( success )
{
new_node->rollnumber=rollnumber;
new_node->src = src;
new_node->dst=dst;
new_node->gentime=gentime;
new_node->next = *head;
*head = new_node;
}
return success;
}
调用显示的代码:(请不要介意我没有提到的变量,它们与我代码的另一部分有关,仅供检查)。
for (i=1;i<=10;i++){
printf("intention[%d]=%d\n",i,intention[i]);
if (intention[i]=true){
printf("link[%d]:\n",i);
display( link, sizeof( link ) / sizeof( *link ) );
}
}
怎么每次调用函数都只显示一个列表?
谁能告诉我如何进行?
最小可重现示例:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#define N 10
typedef struct Node
{
int rollnumber, src, dst;
double gentime;
struct Node *next;
} Node;
int push_front( Node **head, int rollnumber, int src, int dst, double gentime )
{
Node *new_node = malloc( sizeof( Node ) );
int success = new_node != NULL;
if ( success )
{
new_node->rollnumber=rollnumber;
new_node->src = src;
new_node->dst=dst;
new_node->gentime=gentime;
new_node->next = *head;
*head = new_node;
}
return success;
}
void output( Node **head )
{
for( Node *current =*head; current != NULL; current = current->next )
{
printf( "%d %d %d %0.1f ",current->rollnumber, current->src, current->dst, current->gentime );
}
printf( "%s\n", "NULL" );
}
void display( Node **set, size_t n )
{
for ( size_t i = 0; i <= n; i++ )
{
output( set++ );
putchar( '\n' );
}
}
int main(void)
{
int src,dst;
int rollnumber;
double gentime;
Node * link[N] = { 0 };
struct node * head = NULL;
for(i=1;i<=10;i++){
//reading from a file : rollnumber, src, dst, gentime
push_front( &link[i], rollnumber, src, dst, gentime );
printf("link[%d]:\n",i);
display( link, sizeof( link ) / sizeof( *link ) );
}
}
有几个问题:
for (int i = 1; i <= 10; i++)
:数组索引从0开始,而不是1,所以应该是for (int i = 0; i < 10; i++)
display
中不应该有循环,你告诉display
应该显示数组中的哪一个列表,这就是它应该做的。
- 小问题:
output
应该得到 Node* head
,而不是 Node** head
,因为它只从列表中读取,不会修改列表。
- 小问题:不要使用
10
,而是使用 N
。
更正后的代码(如 mcve 删除了不相关的代码和伪造的测试数据):
#include <stdio.h>
#include <stdlib.h>
#define N 10
typedef struct Node
{
int data;
struct Node* next;
} Node;
int push_front(Node** head, int data)
{
Node* new_node = malloc(sizeof(Node));
int success = new_node != NULL;
if (success)
{
new_node->data = data;
new_node->next = *head;
*head = new_node;
}
return success;
}
void output(Node* head)
{
for (Node* current = head; current != NULL; current = current->next)
{
printf("%d ", current->data);
}
}
void display(Node** set, int i)
{
output(set[i]);
putchar('\n');
}
int main(void)
{
int testdata = 1;
Node* link[N] = { 0 };
struct node* head = NULL;
for (int i = 0; i < N; i++) {
push_front(&link[i], testdata++);
push_front(&link[i], testdata++);
}
for (int i = 0; i < N; i++) {
printf("link[%d]:", i);
display(link, i);
}
}
基本上我有 10 个名为 src(=source) 的文件,每个文件都有数据包(为我的作业命名),由卷号、dest(=目的地)和 gentime(=生成时间)表示。我希望每个文件都有一个列表,所以有 10 个不同的列表,它们将传递数据包。但是,当我显示它们时,我必须可以选择只显示一个列表,例如列表 3,它将用于src 文件 3. 我的代码如下。
void output( Node **head )
{
for( Node *current =*head; current != NULL; current = current->next )
{
//printf( "%d %d %d %0.1f ",current->rollnumber, current->src, current->dst, current->gentime );
printf("Roll Number:%2d\t",current->rollnumber);
printf("src:%2d\t", current->src);
printf("dest:%2d\t", current->dst);
printf("gentime:%0.1f\n", current->gentime);
}
printf( "%s\n", "NULL" );
}
void display( Node **set, size_t n )
{
for ( size_t i = 0; i <= n; i++ )
{
output( set++ );
putchar( '\n' );
}
}
输出为:
NULL
Roll Number: 8 src: 1 dest:10 gentime:89.1
Roll Number: 7 src: 1 dest:12 gentime:76.5
Roll Number: 6 src: 1 dest:10 gentime:64.1
Roll Number: 5 src: 1 dest: 4 gentime:51.5
Roll Number: 4 src: 1 dest:17 gentime:38.0
Roll Number: 3 src: 1 dest:20 gentime:25.9
Roll Number: 2 src: 1 dest:15 gentime:13.9
Roll Number: 1 src: 1 dest: 3 gentime:1.6
NULL
Roll Number: 8 src: 2 dest:12 gentime:90.6
Roll Number: 7 src: 2 dest: 6 gentime:78.1
Roll Number: 6 src: 2 dest:17 gentime:64.8
Roll Number: 5 src: 2 dest: 6 gentime:52.6
Roll Number: 4 src: 2 dest: 5 gentime:39.5
Roll Number: 3 src: 2 dest:20 gentime:26.0
Roll Number: 2 src: 2 dest:19 gentime:14.0
Roll Number: 1 src: 2 dest: 4 gentime:1.9
NULL
Roll Number: 8 src: 3 dest: 5 gentime:89.8
Roll Number: 7 src: 3 dest: 1 gentime:75.9
Roll Number: 6 src: 3 dest: 8 gentime:63.9
Roll Number: 5 src: 3 dest:14 gentime:50.8
Roll Number: 4 src: 3 dest:11 gentime:38.4
Roll Number: 3 src: 3 dest:16 gentime:25.7
Roll Number: 2 src: 3 dest:18 gentime:13.4
Roll Number: 1 src: 3 dest: 7 gentime:1.2
NULL
以此类推,直到 src:10
此外,推送功能:
int push_front( Node **head, int rollnumber, int src, int dst, double gentime )
{
Node *new_node = malloc( sizeof( Node ) );
int success = new_node != NULL;
if ( success )
{
new_node->rollnumber=rollnumber;
new_node->src = src;
new_node->dst=dst;
new_node->gentime=gentime;
new_node->next = *head;
*head = new_node;
}
return success;
}
调用显示的代码:(请不要介意我没有提到的变量,它们与我代码的另一部分有关,仅供检查)。
for (i=1;i<=10;i++){
printf("intention[%d]=%d\n",i,intention[i]);
if (intention[i]=true){
printf("link[%d]:\n",i);
display( link, sizeof( link ) / sizeof( *link ) );
}
}
怎么每次调用函数都只显示一个列表? 谁能告诉我如何进行?
最小可重现示例:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#define N 10
typedef struct Node
{
int rollnumber, src, dst;
double gentime;
struct Node *next;
} Node;
int push_front( Node **head, int rollnumber, int src, int dst, double gentime )
{
Node *new_node = malloc( sizeof( Node ) );
int success = new_node != NULL;
if ( success )
{
new_node->rollnumber=rollnumber;
new_node->src = src;
new_node->dst=dst;
new_node->gentime=gentime;
new_node->next = *head;
*head = new_node;
}
return success;
}
void output( Node **head )
{
for( Node *current =*head; current != NULL; current = current->next )
{
printf( "%d %d %d %0.1f ",current->rollnumber, current->src, current->dst, current->gentime );
}
printf( "%s\n", "NULL" );
}
void display( Node **set, size_t n )
{
for ( size_t i = 0; i <= n; i++ )
{
output( set++ );
putchar( '\n' );
}
}
int main(void)
{
int src,dst;
int rollnumber;
double gentime;
Node * link[N] = { 0 };
struct node * head = NULL;
for(i=1;i<=10;i++){
//reading from a file : rollnumber, src, dst, gentime
push_front( &link[i], rollnumber, src, dst, gentime );
printf("link[%d]:\n",i);
display( link, sizeof( link ) / sizeof( *link ) );
}
}
有几个问题:
for (int i = 1; i <= 10; i++)
:数组索引从0开始,而不是1,所以应该是for (int i = 0; i < 10; i++)
display
中不应该有循环,你告诉display
应该显示数组中的哪一个列表,这就是它应该做的。- 小问题:
output
应该得到Node* head
,而不是Node** head
,因为它只从列表中读取,不会修改列表。 - 小问题:不要使用
10
,而是使用N
。
更正后的代码(如 mcve 删除了不相关的代码和伪造的测试数据):
#include <stdio.h>
#include <stdlib.h>
#define N 10
typedef struct Node
{
int data;
struct Node* next;
} Node;
int push_front(Node** head, int data)
{
Node* new_node = malloc(sizeof(Node));
int success = new_node != NULL;
if (success)
{
new_node->data = data;
new_node->next = *head;
*head = new_node;
}
return success;
}
void output(Node* head)
{
for (Node* current = head; current != NULL; current = current->next)
{
printf("%d ", current->data);
}
}
void display(Node** set, int i)
{
output(set[i]);
putchar('\n');
}
int main(void)
{
int testdata = 1;
Node* link[N] = { 0 };
struct node* head = NULL;
for (int i = 0; i < N; i++) {
push_front(&link[i], testdata++);
push_front(&link[i], testdata++);
}
for (int i = 0; i < N; i++) {
printf("link[%d]:", i);
display(link, i);
}
}