Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
987 views
in Technique[技术] by (71.8m points)

for loop - Print linked list up to specific number in C

Suppose I have a linked list of unknown length. I would like to have a function to print out whole list if the length is less than 10, and if its length more than 10, then display only first 10 nodes.

However, because I'm comparing the pointer and integer to print out first 10 nodes, I'm getting the following error:

ordered comparison between pointer and integer ('NodeT *' (aka 'struct node *') and 'int')
        for (current = list; current < 30; current = current->next)

If I change it to have a count and loop while count is less than 10, I'm getting a segmentation fault.

How can I display first 10 nodes if list is length is more than 10?

I have the following function:

void *printNodes(NodeT *list) {
    
    int length = countNodes(list); // finds length of a linked list
    int count = 0; 

    NodeT *current;

    if (list == NULL) {
        printf("Empty list.
");
    }

    if (length < 10) {
        // Display all if number of nodes is less than 10
        for (current = list; current != NULL; current = current->next) {
            printf("%s
", current->data); 
        }

    } else {
        // Display first 10 if number of nodes more than 10 
        for (current = list; current < 10; current = current->next) {
            printf("%s
", current->data);

        // for (current = list; count < 10; current = current->next) {
        //     printf("%s
", current->data);
        //     count++; 

        }
    }
    return 0; 
}
question from:https://stackoverflow.com/questions/65908703/print-linked-list-up-to-specific-number-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For starters the return type void * of the function printNodes

void *printNodes(NodeT *list) {

does not make a sense.

You should declare the function at least like

void printNodes( const NodeT *list) {

As the number of potentially outputted nodes is known there is no need to determine how many nodes there are in the list.

If the condition of this if statement

if (list == NULL) {
    printf("Empty list.
");
}

evaluates to the logical true the function shall return.

The condition of the for loop where a pointer is compared with an integer

    for (current = list; current < 10; current = current->next) {

does not make a sense.

The function can be declared and defined the following way.

void printNodes( const NodeT *list ) 
{
    size_t n = 10;
    
    if ( list == NULL ) 
    {
        printf("Empty list.
");
    }
    else
    {
        for ( ; list != NULL && n--; list = list->next )
        {
            printf( "%s
", list->data );
        }
    }
}

Another approach is to declare one more parameter that will specify how many nodes of the list you want to output. For example

void printNodes( const NodeT *list, size_t n ) 
{
    if ( list == NULL ) 
    {
        printf("Empty list.
");
    }
    else
    {
        for ( ; list != NULL && n--; list = list->next )
        {
            printf( "%s
", list->data );
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.5k users

...