Friday, 6 February 2015

Create a Single Linked List using array and find an element in the list

#include <stdio.h> struct Node { int data; struct Node *next; }; struct Node *create_list(int *ptr,int n) { struct Node *hptr=NULL,*tptr,*temp; int i; if(n>0) { hptr=(struct Node*)malloc(sizeof(struct Node)); tptr=hptr; hptr->data=ptr[0]; for (i=1;i<n;i++) { temp=(struct Node *)malloc(sizeof(struct Node)); temp->data=ptr[i]; temp->next=NULL; tptr->next=temp; tptr=temp; } } return hptr; } char* rfind(struct Node *hptr,int d) { if(hptr==NULL) return "NOT FOUND"; else if(hptr->data==d) return "FOUND"; else return rfind(hptr->next,d); } void main() { int arr[]={10,12,53,84,35,16}; struct Node *hptr,*tptr; hptr=create_list(arr,6); printf("%s\n",rfind(hptr,45)); }

No comments:

Post a Comment