How does the functions for creating nodes can be used to create a linear linked list with n nodes? The linear linked list that will be created will be assigned values from 1 to n, right? The newly created linear linked list is returned by the function create linear() below.
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
struct node *createlinear(int n)
{
int i;
struct node *ptr, *tail, *list;
/*create the nodes*/
list=NULL;
}
for(i=1;i<=n;i++)
{
ptr=(struct node*)malloc(sizeof(struct node));
ptr=>value=i;
if(i>1)
{
tail->next=ptr;
ptr->next=NULL;
tail=ptr;
}
else{
list=ptr;
list->next=NULL;
tail=list;
}
}
return(list);
}
In the code, the pointer variable tail is used to point to the last node inserted in the linked list. A new node (pointed by ptr) is always inserted after the node pointed to by tail (this is clear from tail->next=ptr;).
NULL and malloc are undeclared. How is that? And where will I put int main()?