Skip to content

Commit cfa89c9

Browse files
Queries Problem Solved
1 parent 33fa107 commit cfa89c9

3 files changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Queries","group":"HackerRank - Assignment 02 | Basic Data Structures | Batch 05","url":"https://www.hackerrank.com/contests/assignment-02-a-basic-data-structures-a-batch-05/challenges/queries-6-1","interactive":false,"memoryLimit":512,"timeLimit":4000,"tests":[{"input":"4\n0 10\n1 20\n1 30\n0 40\n","output":"10\n10 20\n10 20 30\n40 10 20 30\n","id":1720920500690},{"input":"11\n0 10\n2 5\n1 20\n1 30\n0 40\n2 0\n0 50\n2 2\n1 60\n2 3\n2 3\n","output":"10\n10\n10 20\n10 20 30\n40 10 20 30\n10 20 30\n50 10 20 30\n50 10 30\n50 10 30 60\n50 10 30\n50 10 30\n","id":1720920500717},{"id":1720920500759,"input":"10\n1 4\n2 1\n0 9\n0 10\n2 2\n1 5\n2 0\n2 1\n2 5\n2 2\n","output":"4\n4\n9 4\n10 9 4\n10 9\n10 9 5\n9 5\n9\n9\n9\n"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"Queries"}},"batch":{"id":"bb446321-185f-49dd-b0a1-fa8ba51d383a","size":1},"srcPath":"f:\\Tutorials\\Video Tutorials\\Phitron\\3. Basic Data Structures\\Queries.cpp"}

Queries.bin

53.1 KB
Binary file not shown.

Queries.cpp

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Node
5+
{
6+
public:
7+
int val;
8+
Node *next;
9+
Node(int val)
10+
{
11+
this->val = val;
12+
this->next = NULL;
13+
}
14+
};
15+
16+
void print_linked_list(Node *head)
17+
{
18+
Node *tmp = head;
19+
while (tmp != NULL)
20+
{
21+
cout << tmp->val << " ";
22+
tmp = tmp->next;
23+
}
24+
cout << endl;
25+
}
26+
27+
void insert_at_head(Node *&head, Node *&tail, long int v)
28+
{
29+
Node *newNode = new Node(v);
30+
if (head == NULL)
31+
{
32+
head = newNode;
33+
tail = newNode;
34+
return;
35+
}
36+
newNode->next = head;
37+
head = newNode;
38+
}
39+
40+
void insert_at_tail(Node *&head, Node *&tail, long int val)
41+
{
42+
Node *newNode = new Node(val);
43+
if (head == NULL)
44+
{
45+
head = newNode;
46+
tail = newNode;
47+
return;
48+
}
49+
tail->next = newNode;
50+
tail = newNode;
51+
}
52+
53+
int getSize(Node *head)
54+
{
55+
Node *tmp = head;
56+
int count = 0;
57+
while (tmp != NULL)
58+
{
59+
count++;
60+
tmp = tmp->next;
61+
}
62+
return count;
63+
}
64+
65+
void delete_at_index(Node *&head, Node *&tail, int pos)
66+
{
67+
Node *tmp = head;
68+
int size = getSize(tmp);
69+
70+
if (tmp == NULL)
71+
return;
72+
73+
if (pos < 0 || pos >= size)
74+
return;
75+
76+
if (pos == 0)
77+
{
78+
Node *deleteNode = head;
79+
head = head->next;
80+
delete deleteNode;
81+
if (head == NULL)
82+
tail = NULL;
83+
return;
84+
}
85+
else
86+
{
87+
for (int i = 0; i < pos - 1; i++)
88+
{
89+
tmp = tmp->next;
90+
}
91+
92+
Node *deleteNode = tmp->next;
93+
tmp->next = tmp->next->next;
94+
if (tmp->next == NULL)
95+
{
96+
tail = tmp;
97+
}
98+
delete deleteNode;
99+
}
100+
}
101+
102+
int main()
103+
{
104+
Node *head = NULL;
105+
Node *tail = NULL;
106+
int q;
107+
cin >> q;
108+
while (q--)
109+
{
110+
int x;
111+
long int v;
112+
cin >> x >> v;
113+
114+
if (x == 0)
115+
insert_at_head(head, tail, v);
116+
else if (x == 1)
117+
insert_at_tail(head, tail, v);
118+
else if (x == 2)
119+
delete_at_index(head, tail, v);
120+
121+
print_linked_list(head);
122+
}
123+
124+
return 0;
125+
}

0 commit comments

Comments
 (0)