Professional Documents
Culture Documents
Dhruv 1.3 AP
Dhruv 1.3 AP
Experiment -1.3
Student Name: Dhruv UID: 21BCS3484
Branch: BE-CSE Section/Group: 802-B
Semester: 5th Date of Performance: 22-08-23
Subject Name: Advance Programming Lab Subject Code: 21CSP-314
2. Objective:-You’re given the pointer to the head nodes of two linked lists. Compare
the data in the nodes of the linked lists to check if they are equal. If all data attributes are
equal and the lists are the same length, return 1. Otherwise, return 0.
Example :-
The two lists have equal data attributes for the first 3 nodes.llist2 is longer,
though, so the lists are not equal. Return 0.
program and output:
1. Compare List:-
bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode*
head2) {
int len1 = 0, len2 = 0;
while (head1 != NULL && head2 != NULL) {
if (head1 -> data == head2 -> data) {
head1 = head1 -> next;
head2 = head2 -> next;
len1++;
len2++;
}
else {
return false;
}
1
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
} Source
while (head1 != NULL) {
len1++;
head1 = head1 -> next;
}
while (head2 != NULL) {
len2++;
head2 = head2 -> next;
}
Output:-
2
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if (temp == NULL){
head = insert;
}
else if(temp->data > data){
insert->next = temp;
temp->prev = insert;
head = insert;
}
else{
while((temp->next!=NULL)&&(temp->next->data < data)){
temp=temp->next;
}
insert->prev = temp;
if (temp->next!=NULL){
insert->next = temp->next;
temp->next->prev = insert;
}
temp->next = insert;
}
return head;
}
3
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:-