Insert at End of Link List

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include<stdio.h> #include<conio.

h> struct node { int data; struct node *link; }; void insert_end (); void display(); void main() { int i,ch; struct node *p; clrscr(); p=NULL; printf("enter your choice 1 for insert and 2 for display"); scanf("%d", &ch); if (ch==1) { printf("enter the number you want to insert"); for(i=0;i<5;i++) { scanf("%d",&i); insert_end( &p, i); //display(p); } display(p); } else display(p); getch(); } void insert_end( struct node **q, int num) { struct node *temp, *r; if (*q==NULL) { temp= (struct node *) malloc (sizeof (struct node)); temp -> data = num; temp ->link = NULL; *q= temp; } else { temp = *q; while(temp->link != NULL) { temp= temp->link; } r= (struct node *)malloc (sizeof(struct node)); r->data= num; r-> link= NULL; temp-> link=r; } } void display(struct node *q) {

while( q!=NULL) { printf("%d", q->data); q=q->link; } }

You might also like