Worksheet 1.3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Worksheet 1.

StudentName: Abhishek Nautiyal UID:19BCS2103


Branch: BE CSE Section/Group: Cl 4A
Semester:6 SubjectName:TechnicalTraining
Subject Code: CSB-356-b

1. Aim/Overview of the practical:


Problem 1: https://www.hackerrank.com/challenges/reverse-a-linked-list/problem

2. Code for experiment/practical:

problem 1 code:
#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);


string rtrim(const string &);

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;

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;
}
}
}

/*
* Complete the 'reverse' function below.
*
* The function is expected to return an INTEGER_SINGLY_LINKED_LIST.
* The function accepts INTEGER_SINGLY_LINKED_LIST llist as parameter.
*/

/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*
*/

SinglyLinkedListNode* reverse(SinglyLinkedListNode* head) {


if (!head) return head;
SinglyLinkedListNode* prev = head;
SinglyLinkedListNode* current = head->next;
head->next = nullptr;

while(current) {
SinglyLinkedListNode* next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string tests_temp;
getline(cin, tests_temp);
int tests = stoi(ltrim(rtrim(tests_temp)));

for (int tests_itr = 0; tests_itr < tests; tests_itr++) {


SinglyLinkedList* llist = new SinglyLinkedList();

string llist_count_temp;
getline(cin, llist_count_temp);

int llist_count = stoi(ltrim(rtrim(llist_count_temp)));

for (int i = 0; i < llist_count; i++) {


string llist_item_temp;
getline(cin, llist_item_temp);

int llist_item = stoi(ltrim(rtrim(llist_item_temp)));

llist->insert_node(llist_item);
}

SinglyLinkedListNode* llist1 = reverse(llist->head);

print_singly_linked_list(llist1, " ", fout);


fout << "\n";
}

fout.close();

return 0;
}

string ltrim(const string &str) {


string s(str);

s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);

return s;
}

string rtrim(const string &str) {


string s(str);

s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);

return s;
}

3. Result/Output/Writing Summary:

problem 1:
Evaluation Grid:

Sr. No. Parameters Marks Obtained Maximum Marks


1. Demonstration and Performance 5
(Pre Lab Quiz)
2. Worksheet 10
3. Post Lab Quiz 5

You might also like