0

This is my code for creating and printing a linked list. I wrote it in the Eclipse IDE, but when I run it, there is a problem as shown below. I first enter a integer input to be read into n, but it isn't printed as intended.

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

typedef struct node {
    int data;
    struct node *next;
} nodex;

int main() {
    nodex *mainhead;
    int n;
    **printf("Enter no. of nodes : ");**
    **scanf("%d",&n);**
    nodex *head=NULL,*isotemp,*p;
    int i;
    for(i=0;i<n;i++) {
        isotemp = (nodex*)malloc(sizeof(nodex));
        **printf("Enter the data of node %d: " , i );**
        **scanf("%d", &(isotemp->data) );**
        isotemp->next = NULL;

        if(head==NULL) {
            head = isotemp;
        } else {
            p = head;
            while(p->next != NULL) {
                p = p->next;
            }
            p->next = isotemp;
        }
    }
    mainhead=head;
    **printf("\nLinked LIst : \n");**
    nodex *ptr;
    ptr=head;
    while(ptr!=NULL) {
       ** printf(" %d-> ",ptr->data);**
        ptr=ptr->next;
    }
    return 0;
}

OUTPUT:

 1
 22
 Enter no. of nodes : Enter the data of node 0: 
 Linked LIst : 
 22->
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • 1
    When you're doing something very simple and it doesn't work, the problem is nearly without exceptions not with the compiler. – Guy Incognito Aug 08 '20 at 12:47
  • True, @GuyIncognito, but the I/O windows to which IDEs connect programs' standard streams do tend to have behaviors that differ a bit from *bona fide* terminals. Some of those may be related to the IDE specifically, and some simply to the fact that it's not a terminal. That doesn't appear to be the source of the OP's problem, but it *does* affect which solutions are viable for running the program inside Eclipse. – John Bollinger Aug 08 '20 at 12:55

0 Answers0