*linked list
#include<stdio.h>
#include<stdlib.h>
//----------------------------------------------
typedef struct node
{
int data;
struct node *next;
}node;
//-----------------------------------------------
node *linked_list(int n)
{
node *head = NULL;
node *temp = NULL;
node *p = NULL;
for(int i=0;i<n;i++)
{
temp = (node*)malloc(sizeof(node));
printf("\nEnter the data of the node %d: ", i+1);
scanf("%d", &(temp->data));
temp->next = NULL;
if(head == NULL)
{head = temp;}
else
{ p = head;
while(p->next!=NULL)
p = p->next;
p->next = temp;
}
return head;
}
}
//-------------------------------------------
display(node *head)
{
node *p = head;
while(p != NULL)
{
printf("\n%d -> ",p->data);
p = p->next;
}
}
//-----------------------------------------------
int main()
{
int n=0;
node *HEAD = NULL;
printf("\nHow many nodes.");
scanf("%d", &n);
HEAD = linked_list(n);
display(HEAD);
return 0;
}
Comments
Post a Comment