Successfully reported this slideshow.
Your SlideShare is downloading. ×

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

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad

Check these out next

1 of 3 Ad

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

Download to read offline

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
void insert(Node **head,int data){
struct Node *temp;
if(*head==NULL){
(*head)=(struct Node*)malloc(sizeof(struct Node));
(*head)->data=data;
(*head)->next=NULL;
}else{
temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=data;
temp->next=(*head);
(*head)=temp;
}
}
// searching element in list
int searchList(Node *head, int value){
int i=0;int flag=0;
while(head!=NULL){
if(head->data==value){
flag=1;
return i;
}
i++;
head=head->next;
}
if(flag==0)
return -1;
}
void printElement(Node *head){
while(head!=NULL){
cout<<head->data;
head=head->next;
}
}
int main()
{
struct Node *head=NULL;
insert(&head,10);
insert(&head,20);
insert(&head,30);
insert(&head,40);
insert(&head,50);
//printElement(head);
int rValue=searchList(head,30);
if(rValue==-1){
cout<<\"Value do not exist in list\"<<endl;
}
else{
cout<<\"Value exist at position \"<<rValue<<endl;
}

return 0;
}
/*sample output*/
searchList(head,30);
Value exist at 2
searchList(head,60);
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
void insert(Node **head,int data){
struct Node *temp;
if(*head==NULL){
(*head)=(struct Node*)malloc(sizeof(struct Node));
(*head)->data=data;
(*head)->next=NULL;
}else{
temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=data;
temp->next=(*head);
(*head)=temp;
}
}
// searching element in list
int searchList(Node *head, int value){
int i=0;int flag=0;
while(head!=NULL){
if(head->data==value){
flag=1;
return i;
}
i++;
head=head->next;
}
if(flag==0)
return -1;
}
void printElement(Node *head){
while(head!=NULL){
cout<<head->data;
head=head->next;
}
}
int main()
{
struct Node *head=NULL;
insert(&head,10);
insert(&head,20);
insert(&head,30);
insert(&head,40);
insert(&head,50);
//printElement(head);
int rValue=searchList(head,30);
if(rValue==-1){
cout<<\"Value do not exist in list\"<<endl;
}
else{
cout<<\"Value exist at position \"<<rValue<<endl;
}

return 0;
}
/*sample output*/
searchList(head,30);
Value exist at 2
searchList(head,60);
Value do not exist in list
.

Advertisement
Advertisement

More Related Content

Similar to 10- Consider the following data structure used for implementing a link.docx (20)

More from todd991 (20)

Advertisement

Recently uploaded (20)

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

  1. 1. 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 void insert(Node **head,int data){ struct Node *temp; if(*head==NULL){ (*head)=(struct Node*)malloc(sizeof(struct Node)); (*head)->data=data; (*head)->next=NULL; }else{ temp=(struct Node*)malloc(sizeof(struct Node)); temp->data=data; temp->next=(*head); (*head)=temp; } } // searching element in list int searchList(Node *head, int value){ int i=0;int flag=0; while(head!=NULL){ if(head->data==value){ flag=1;
  2. 2. return i; } i++; head=head->next; } if(flag==0) return -1; } void printElement(Node *head){ while(head!=NULL){ cout<<head->data; head=head->next; } } int main() { struct Node *head=NULL; insert(&head,10); insert(&head,20); insert(&head,30); insert(&head,40); insert(&head,50); //printElement(head); int rValue=searchList(head,30); if(rValue==-1){ cout<<"Value do not exist in list"<<endl; } else{ cout<<"Value exist at position "<<rValue<<endl; } return 0; } /*sample output*/ searchList(head,30); Value exist at 2 searchList(head,60); Value do not exist in list

×