Successfully reported this slideshow.

×

# 10- Consider the following data structure used for implementing a link.docx

10. Consider the following data structure used for implementing a linked list: struct Node int data. Node *next; Implement the function that takes a pointer to the first node of a linked list and an integer value as arguments. It returns -1 if the input value is not equal to the data value of any item in the list. Otherwise, it returns the location of an item in the list whose data value is equal to the input value. Assume that the location of the first item in the list is 0. int search List (Node *head, int value) write the function body
Solution
#include <iostream>
using namespace std;
struct Node{
int data;
Node *next;
};
//inserting element at front side in list
struct Node *temp;
}else{
temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=data;
}
}
// searching element in list
int i=0;int flag=0;
flag=1;
return i;
}
i++;
}
if(flag==0)
return -1;
}
}
}
int main()
{
if(rValue==-1){
cout<<\"Value do not exist in list\"<<endl;
}
else{
cout<<\"Value exist at position \"<<rValue<<endl;
}

return 0;
}
/*sample output*/
Value exist at 2
Value do not exist in list
.

10. Consider the following data structure used for implementing a linked list: struct Node int data. Node *next; Implement the function that takes a pointer to the first node of a linked list and an integer value as arguments. It returns -1 if the input value is not equal to the data value of any item in the list. Otherwise, it returns the location of an item in the list whose data value is equal to the input value. Assume that the location of the first item in the list is 0. int search List (Node *head, int value) write the function body
Solution
#include <iostream>
using namespace std;
struct Node{
int data;
Node *next;
};
//inserting element at front side in list
struct Node *temp;
}else{
temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=data;
}
}
// searching element in list
int i=0;int flag=0;
flag=1;
return i;
}
i++;
}
if(flag==0)
return -1;
}
}
}
int main()
{
if(rValue==-1){
cout<<\"Value do not exist in list\"<<endl;
}
else{
cout<<\"Value exist at position \"<<rValue<<endl;
}

return 0;
}
/*sample output*/
Value exist at 2
Value do not exist in list
.