Home > Mobile >  How to remove "->" in my C program (Linked List)
How to remove "->" in my C program (Linked List)

Time:10-20

I have a Linked list homework in C programming and we are tasked to write a program that accepts two integer input, and create two nodes with the values. Make the node with greater value the first node. The next node of the first node is the other input.

Although I got the program right, for some reason, there's an extra "->" in my output I have been trying to analyze the problem and solve it, but I'm still struggling with it. Can I ask for assistance?

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
    int value;
    struct node* next;
} node;

node* create_node(int value){
    node* new_node = (node*)malloc(sizeof(node));
    new_node -> value = value;
    new_node -> next = NULL;
    return new_node;
}

void printList(node* head){
    while(head){
        printf("%d -> ", head -> value);
        head = head -> next;
    }
}

int main(){
    int num1, num2;
    struct node node1;
    
    node* head = NULL;
    node* temp = NULL;

    printf("Enter number 1: ");
    scanf("%d", &num1);
    printf("Enter number 2: ");
    scanf("%d", &num2);

    if(num1 > num2){
        head = create_node(num1);
        temp = create_node(num2);
        head -> next = temp;
    } else {
        head = create_node(num2);
        temp = create_node(num1);
        head -> next = temp;
    }

    printList(head);
    return 0;
}

the Output should be

Enter number 1: 1
Enter number 2: 2
2 -> 1

but the Output I got is

Enter number 1: 1
Enter number 2: 2
2 -> 1 ->

CodePudding user response:

This is how you print the value of any and every node:

        printf("%d -> ", head -> value);

That emits a "->" after the node value. Why, then, would there not be one after the last node?

If you want to arrange the code so that it prints the "->" after the node number, then you must detect the special case of the last node, and avoid printing the "->" in that case. But it might be easier to instead make the first node the special case, as @Fe2O3 suggested in comments. That would involve printing the "->" before each subsequent node.

CodePudding user response:

This is Stack Overflow. "Assisting" is what we are here for!

Here's a slightly altered version of your code showing some alternatives for you to consider.

#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    int value;
    struct node *next;
} node;

// calloc() will save you having to ensure the block of heap is initialised
node *create_node( int value ) {
    node *nn = calloc( 1, sizeof *nn ); // note 'sizeof' relates to object, not struct
    /* omitting check of return code */
    nn->value = value;
    return nn; // short variable names
}

// printing data values with "prefix" string
// short name, again.
void printList( node *p ) {
    for( char *prfx = ""; p; p = p->next, prfx = " -> " )
        printf( "%s%d", prfx, p->value );
    putchar( '\n' ); // missing newline
}

int main() {
    int num1, num2;

    printf("Enter number 1: ");
    scanf("%d", &num1);
    /* omitting check of return code */
    printf("Enter number 2: ");
    scanf("%d", &num2);
    /* omitting check of return code */

    // declare variables close to their first use.
    // better to use clever code than copy/paste
    // larger value into 1st node. sum then subtract pushes smaller value into 2nd
    node *head = create_node( num1 > num2 ? num1 : num2 );
    head->next = create_node( num1   num2 - head->value );

    printList( head );

    return 0;
}
  •  Tags:  
  • c
  • Related