How to create single linked list? | Data structure Problem | coding for beginners

coding courses about computer coding for beginners free online in data structures and alogorithms in c,java,python with best programming resources

For coding courses about computer coding for beginners free online in data structures and alogorithms in c,java,python with best programming resources follow our blog
 
In our programming website we teach computer coding to kids and pros for free online in easy way and gives knowledge about Data structure and alogorithm in C,Java,Python and other programming languages that may help you land an online job and make your programmers day successfull.We use best data structure notes and best programming resources.

Here goes the article,dont forget to share with your friends 😉

#include"stdio.h"
#include"stdlib.h"
struct node
{
int data;
struct node *next;
};
struct node *start; //set the head of the list as global
struct node *createlist(int d)
{
struct node *ptr,*p;
int i;

ptr=(struct node *)malloc(sizeof(struct node)); //allocating memory for the added node
ptr->data=d; //allocate data part
if(start==NULL) //when list is empty
{
ptr->next=start; //set the address part of the node to head which is null
start=ptr; //set the added node as head 
}
else
{
p=start;
while(p->next!=NULL)
p=p->next; //traverse the list untill p points to the lat node
p->next=ptr; //link the address part of the last node to new node
ptr->next=NULL; //make the address part of the new node null so that it can become last node
}
return start; //return head
}
void display(struct node *start)
{
struct node *p;
p=start;
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
}


int main()
{
int n,i,d;
printf("enter the number of nodes\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%dth node value:",i);
scanf("%d",&d);
createlist(d);
}
printf("The list is:");
display(start);
return 0;
}
Here I also attach the output of the program:
coding courses about computer coding for beginners free online in data structures and alogorithms in c,java,python with best programming resources
feel free to comment for any problem regarding this solution😃😃
Check out our other social media handels below,where we update you when our next post is coming:
Facebook
linkedin
Instagram
Quora


Post a Comment

0 Comments