-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedListFromArray.c
More file actions
47 lines (44 loc) · 919 Bytes
/
Copy pathlinkedListFromArray.c
File metadata and controls
47 lines (44 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<stdio.h>
#include<stdlib.h>
struct Node *createLinkedList(int arr[],int size);
int searceLinkedlist(struct Node *head, int value);
struct Node{
int data;
struct Node *next;
};
int main(){
int a[]={15,30,45,60,80};
struct Node *head=NULL;
head=createLinkedList(a,5);
struct Node *current=head;
printf("Index:%d",searceLinkedlist(head,45));
return 0;
}
int searceLinkedlist(struct Node *head, int value){
int index=1;
while(head!=NULL){
if(head->data==value){
return index;
}
index++;
head=head->next;
}
return -1;
}
struct Node *createLinkedList(int arr[],int size){
struct Node *head=NULL,*temp=NULL,*current=NULL;
int i;
for(i=0;i<size;i++){
temp=(struct Node *)malloc(sizeof(struct Node));
temp->data=arr[i];
temp->next=NULL;
if(head==NULL){
head=temp;
current=temp;
}else{
current->next=temp;
current=current->next;
}
}
return head;
}