Basically I have 10 files named src(=source) and each of them has packets (named so for my homework) which are represented by roll number, dest(=destination) and gentime (=generation time). I want to have one list for each file, so 10 different lists, that will pass the packets in. But, when I display them, I have to have the option to display only one list, for example list 3, which will be for src file 3. My code is below.
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:-\t",current->rollnumber);
printf("src:-\t", current->src);
printf("dest:-\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' );
}
}
The output is:
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
and so goes on until src:10
Also, the push function:
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;
}
Code that calls display: (please don't mind the variables I have not mentioned, they have to do with another part of my code, just for checking).
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 ) );
}
}
How is it possible to display only one list whenever I call the function? Can anyone please tell me how to proceed?
Minimal Reproducible Example:
#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 ) );
}
}
CodePudding user response:
There are several problems:
for (int i = 1; i <= 10; i ): array indexes start with 0, not with 1, so it should befor (int i = 0; i < 10; i )- There should not be a loop in
display, you telldisplaywhich one of the lists in the array should be displayed, and that's all it should do. - Minor problem:
outputshould get aNode* head, not aNode** head, because it only reads from the list, it does not modify the list. - Minor problem: don't use
10butN.
Corrected code (as mcve with irrelevant code stripped and with fake test data):
#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);
}
}
