DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 1.3
Student Name: Sahil Shreyansh UID: 21BCS8687
Branch: CSE Section/Group: 638 B
Semester: 5 Date of Performance:05/09/23
Subject Name: Advance Programming Lab-1 Subject Code: 21CSP-314
1. Aim:
To understand and implement the concept of a linked list
2. Objective:
1. To compare two linked lists and check if they are equal.
2. Determine the optimal starting point for a truck's journey along a
circular route with multiple petrol pumps.
3. Script and Output:
Code:
1. #include <bits/stdc++.h>
using namespace std;
class SinglyLinkedListNode {
public:
int data;
SinglyLinkedListNode *next;
SinglyLinkedListNode(int node_data) {
this->data = node_data;
this->next = nullptr;
}
};
class SinglyLinkedList {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
public:
SinglyLinkedListNode *head;
SinglyLinkedListNode *tail;
SinglyLinkedList() {
this->head = nullptr;
this->tail = nullptr;
}
void insert_node(int node_data) {
SinglyLinkedListNode* node = new
SinglyLinkedListNode(node_data);
if (!this->head) {
this->head = node;
} else {
this->tail->next = node;
}
this->tail = node;
}
};
void print_singly_linked_list(SinglyLinkedListNode* node, string sep,
ofstream& fout) {
while (node) {
fout << node->data;
node = node->next;
if (node) {
fout << sep;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
}
}
void free_singly_linked_list(SinglyLinkedListNode* node) {
while (node) {
SinglyLinkedListNode* temp = node;
node = node->next;
free(temp);
}
}
bool compare_lists(SinglyLinkedListNode* head1,
SinglyLinkedListNode* head2) {
int res=1;
while(head1 != NULL || head2 != NULL){
if(head1 == NULL) {res=0; break;}
if(head2 == NULL) {res=0; break;}
if(head1->data != head2->data){res=0;break;}
head1=head1->next;
head2=head2->next;
}
return res;
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
int tests;
cin >> tests;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int tests_itr = 0; tests_itr < tests; tests_itr++) {
SinglyLinkedList* llist1 = new SinglyLinkedList();
int llist1_count;
cin >> llist1_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int i = 0; i < llist1_count; i++) {
int llist1_item;
cin >> llist1_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
llist1->insert_node(llist1_item);
}
SinglyLinkedList* llist2 = new SinglyLinkedList();
int llist2_count;
cin >> llist2_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int i = 0; i < llist2_count; i++) {
int llist2_item;
cin >> llist2_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
llist2->insert_node(llist2_item);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
bool result = compare_lists(llist1->head, llist2->head);
fout << result << "\n";
}
fout.close();
return 0;
}
2.#include <bits/stdc++.h>
using namespace std;
class SinglyLinkedListNode {
public:
int data;
SinglyLinkedListNode *next;
SinglyLinkedListNode(int node_data) {
this->data = node_data;
this->next = nullptr;
}
};
class SinglyLinkedList {
public:
SinglyLinkedListNode *head;
SinglyLinkedListNode *tail;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
SinglyLinkedList() {
this->head = nullptr;
this->tail = nullptr;
}
void insert_node(int node_data) {
SinglyLinkedListNode* node = new
SinglyLinkedListNode(node_data);
if (!this->head) {
this->head = node;
} else {
this->tail->next = node;
}
this->tail = node;
}
};
void print_singly_linked_list(SinglyLinkedListNode* node, string sep,
ofstream& fout) {
while (node) {
fout << node->data;
node = node->next;
if (node) {
fout << sep;
}
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
void free_singly_linked_list(SinglyLinkedListNode* node) {
while (node) {
SinglyLinkedListNode* temp = node;
node = node->next;
free(temp);
}
}
bool has_cycle(SinglyLinkedListNode* head) {
SinglyLinkedListNode *t = head;
SinglyLinkedListNode *r = head;
if(head == NULL || head->next==NULL) // Condition 1
{
return false;
}
while( r!=NULL&&r->next!=NULL) // Condition 2
{
t = t->next; // Tortoise node
r = r->next->next; // Hare node
if(t==r) // Condition 3
{
return true;
break;
}
}
return false;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int tests;
cin >> tests;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int tests_itr = 0; tests_itr < tests; tests_itr++) {
int index;
cin >> index;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
SinglyLinkedList* llist = new SinglyLinkedList();
int llist_count;
cin >> llist_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int i = 0; i < llist_count; i++) {
int llist_item;
cin >> llist_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
llist->insert_node(llist_item);
}
SinglyLinkedListNode* extra = new SinglyLinkedListNode(-1);
SinglyLinkedListNode* temp = llist->head;
for (int i = 0; i < llist_count; i++) {
if (i == index) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
extra = temp;
}
if (i != llist_count-1) {
temp = temp->next;
}
}
temp->next = extra;
bool result = has_cycle(llist->head);
fout << result << "\n";
}
fout.close();
return 0;
}
Output:
1.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
2.