SCHOOL OF CODE BUILDERS
Learn To CODE. Become A DEVELOPER.
Pages
HOME
DATA STRUCTURES
STRINGS
ARRAYS
MATRIX
BINARY TREES
LINKED LIST
STACK
QUEUE
SORTING
SEARCHING
C
PYTHON
PSEUDOCODE
CONTEST PROBLEMS
ALGORITHMS
PATTERNS
PHP
C PUZZLES
C INTERVIEW QUESTIONS
JAVA
C++
HASHING
RECURSION
BASIC C PROGRAMS
TCS-CODEVITA
FACEBOOK
CONTACT US
Linked List Implementation(Insert the nodes in the tail of the linked list)
Linked List Implementation(Insert the nodes in the tail of the linked list)
Implementation:
#include
#include
/* structure of the node of the linked list */ struct node { int data; struct node *next; }; /* Create a new node and assign the data to the node's data */ struct node* newnode(int data) { /* allocate memory for the new node of the linked list */ struct node *new=(struct node*)malloc(sizeof(struct node)); /* assign the data */ new->data=data; /* assign the next pointer as NULL */ new->next=NULL; /* return the address of the created new node */ return new; } /* function to insert every input node in the tail of the linked list */ void insertastail(struct node **head,int data) { struct node *temp; struct node *current=*head; /* if the list is empty , the newly created node will be the head node */ if(current==NULL) { *head=newnode(data); } /* Move the current pointer to the end of the linked list */ else { temp=newnode(data); while(current->next!=NULL) current=current->next; /* insert the newly created node at the end of the linked list */ current->next=temp; } } /* function to print the nodes in the linked list */ void printlist(struct node *head) { struct node *current=head; while(current!=NULL) { printf("%d ",current->data); current=current->next; } } int main(void) { struct node *head=NULL; int data; int i,n; scanf("%d",&n); for(i=1;i<=n;i++) insertastail(&head,i); printlist(head); return 0; }
PREVIOUS
NEXT
HOME