From be1d426374d4c8f5f57b2e8ecbc8c161f7b923d3 Mon Sep 17 00:00:00 2001 From: lmalshan96 <115359843+lmalshan96@users.noreply.github.com> Date: Mon, 10 Oct 2022 09:00:12 +0530 Subject: [PATCH] Create compare_linked_list.cpp --- compare_linked_list.cpp | 70 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 compare_linked_list.cpp diff --git a/compare_linked_list.cpp b/compare_linked_list.cpp new file mode 100644 index 0000000..f8a80ce --- /dev/null +++ b/compare_linked_list.cpp @@ -0,0 +1,70 @@ +#include + #include + + struct Node + { + int data; + struct Node *next; + }; + + struct Node* input() + { + int data1; struct Node*temp; + printf("enter -1 to stop\n"); + int i=3; + struct Node *first=(struct Node*)malloc(sizeof(struct Node)); + printf("enter 1st data\n"); + scanf("%d",&data1); + first->data=data1; + first->next=NULL; + temp=first; + printf("enter 2nd data\n"); + scanf("%d",&data1); + while(data1!=-1) + { + struct Node* t=(struct Node*)malloc(sizeof(struct Node)); + t->data=data1; + t->next=NULL; + temp->next=t; + temp=t; + printf("enter %d data\n",i); + scanf("%d",&data1); + i++; + } + return first; + + } + int size(struct Node*p) + { + if(p==NULL) + return 0; + else + return size(p->next)+1; + } + int compare(struct Node *head1,struct Node *head2) + { + if(size(head1)!=size(head2)) + return -1; + int i=0; + while(head2!=NULL) + { + if(head1->data==head2->data) + { + i++; + head1=head1->next;head2=head2->next; + } + else + break; + } + return i; + } + int main() + { + struct Node *head1=input(); + struct Node *head2=input(); + int f=compare(head1,head2); + if(f==size(head1)) + printf("compatible\n"); + else + printf("not campatible\n"); + }