Ap 3
Ap 3
Ap 3
2. Objective:
• To learn about Linked List data Structure
• To learn different approaches to compare two linked lists
3. Algorithm:
• Read the number of test cases.
• For each test case:
o Read the size and elements of the first linked list and construct it.
o Read the size and elements of the second linked list and construct it.
o Compare the two linked lists by checking each corresponding node.
o If all corresponding nodes match and both lists are of the same length, they
are identical.
o Otherwise, they are not identical.
• Print 1 if the linked lists are identical, otherwise print 0.
4. Source Code:
#include <iostream>
using namespace std;
struct SinglyLinkedListNode {
int data;
SinglyLinkedListNode* next;
SinglyLinkedListNode(int node_data) {
data = node_data;
next = nullptr;
}
};
SinglyLinkedListNode* insert_node(SinglyLinkedListNode* head, int data) {
SinglyLinkedListNode* new_node = new SinglyLinkedListNode(data);
if (head == nullptr) {
return new_node;
} else {
SinglyLinkedListNode* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = new_node;
}
return head;
}
bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {
while (head1 != nullptr && head2 != nullptr) {
if (head1->data != head2->data) {
return false;
}
head1 = head1->next;
head2 = head2->next;
}
return (head1 == nullptr && head2 == nullptr);
}
int main() {
int test_cases;
cin >> test_cases;
for (int t = 0; t < test_cases; t++) {
int n1, n2, data;
cin >> n1;
SinglyLinkedListNode* head1 = nullptr;
for (int i = 0; i < n1; i++) {
cin >> data;
head1 = insert_node(head1, data);
}
cin >> n2;
SinglyLinkedListNode* head2 = nullptr;
for (int i = 0; i < n2; i++) {
cin >> data;
head2 = insert_node(head2, data);
}
bool result = compare_lists(head1, head2);
if (result) {
cout << 1 << endl;
} else {
cout << 0 << endl;
}
}
return 0;
}
5. Output:
a. Aim:
To demonstrate the concept of Linked List.
b. Objective:
• To learn about Linked List data Structure.
• To insert a node into a sorted Doubly Linked List.
3. Algorithm:
• Input the number of test cases (t).
• For each test case:
o Input the number of elements in the list (n).
o Initialize an empty list ‘lst’.
o For each element in the list:
a. Input the element nodeValue.
b. Add nodeValue to the end of the list ‘lst’.
o Input the value data that needs to be inserted into the list in sorted order.
o Insert data into ‘lst’ while maintaining sorted order:
a. Traverse the list from the beginning to find the first position where
the current element is greater than or equal to data.
b. Insert data at that position.
o Print the updated list.
a. Traverse the list and print each element.
3. Source Code:
#include <iostream>
#include <list>
using namespace std;
int main() {
int t, n, data;
cin >> t;
while (t--) {
cin >> n;
list<int> lst;
for (int i = 0; i < n; ++i) {
int nodeValue;
cin >> nodeValue;
lst.push_back(nodeValue);
}
cin >> data;
insertSorted(lst, data);
printList(lst);
}
return 0;
}
4. Output: