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->