Sunteți pe pagina 1din 2

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

h> typedef struct node *list_ptr; struct node { int data; list_ptr next; }; list_ptr insert(list_ptr first,int p,int element); void main() { list_ptr first; first=NULL; clrscr(); first=(list_ptr)malloc(sizeof(*first)); first->data=10; first->next=NULL; printf("data in list=%d",first->data); int p,element; printf("enter the position and the element"); scanf("%d%d",&p,&element); first=insert(first,p,element); list_ptr temp2; temp2=first; while(temp2!=NULL) { printf("data=%d\n",temp2->data); temp2=temp2->next; } getch(); } list_ptr insert(list_ptr first,int p,int element) { list_ptr temp,node; node=(list_ptr)malloc(sizeof(*node)); node->data=element; int i=1; if(first==NULL) { node->next=NULL; first=node; } else { temp=first; if(p==1) { node->next=first; first=node; } else { while(temp->next!=NULL && i!=p-1) { temp=temp->next; i++; }

node->next=temp->next; temp->next=node; } } return(first); }

S-ar putea să vă placă și