Skip to content

Commit 06ddbd4

Browse files
Same or Not Using Doubly Linked List, Stack & Queue Solved
1 parent b71c225 commit 06ddbd4

3 files changed

+167
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"name":"Same or Not II","group":"HackerRank - Assignment 03 | Basic Data Structure | Batch 05","url":"https://www.hackerrank.com/contests/assignment-03-a-basic-data-structure-a-batch-05/challenges/same-or-not-4","interactive":false,"memoryLimit":512,"timeLimit":4000,"tests":[{"input":"5 5\n10 20 30 40 50\n50 40 30 20 10\n","output":"YES\n","id":1723011278114},{"input":"4 4\n10 20 30 40\n10 20 30 40\n","output":"NO\n","id":1723011278154},{"input":"5 4\n1 2 3 4 5\n5 4 3 2\n","output":"NO\n","id":1723011278140}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"SameOrNotII"}},"batch":{"id":"9f4f7301-443d-4fb2-bf72-b064f7afd343","size":1},"srcPath":"c:\\xampp\\htdocs\\Phitron\\Basic_Data_Structures\\Same_or_Not_II.cpp"}
1+
{"name":"Same or Not II","group":"HackerRank - Assignment 03 | Basic Data Structure | Batch 05","url":"https://www.hackerrank.com/contests/assignment-03-a-basic-data-structure-a-batch-05/challenges/same-or-not-4","interactive":false,"memoryLimit":512,"timeLimit":4000,"tests":[{"input":"5 5\n10 20 30 40 50\n50 40 30 20 10\n","output":"YES\n","id":1723011278114},{"input":"4 4\n10 20 30 40\n10 20 30 40\n","output":"NO\n","id":1723011278154},{"id":1723011278140,"input":"5 4\n1 2 3 4 5\n5 4 3 2\n","output":"NO\n"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"SameOrNotII"}},"batch":{"id":"9f4f7301-443d-4fb2-bf72-b064f7afd343","size":1},"srcPath":"c:\\xampp\\htdocs\\Phitron\\Basic_Data_Structures\\Same_or_Not_II.cpp"}

.vscode/settings.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"files.associations": {
3+
"*.js": "javascriptreact",
34
"iostream": "cpp",
45
"ostream": "cpp",
5-
"*.tcc": "cpp"
6+
"*.tcc": "cpp",
7+
"new": "cpp"
68
}
79
}

Same_or_Not_II.cpp

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Node
5+
{
6+
public:
7+
int val;
8+
Node *prev;
9+
Node *next;
10+
Node(int val)
11+
{
12+
this->val = val;
13+
this->next = NULL;
14+
this->prev = NULL;
15+
}
16+
};
17+
18+
class myStack
19+
{
20+
Node *head = NULL, *tail = NULL;
21+
int sz = 0;
22+
23+
void push(int x)
24+
{
25+
sz++;
26+
Node *newNode = new Node(x);
27+
if (head == NULL)
28+
{
29+
head = newNode;
30+
tail = newNode;
31+
return;
32+
}
33+
tail->next = newNode;
34+
newNode->prev = tail;
35+
tail = newNode;
36+
}
37+
38+
void pop()
39+
{
40+
if (tail == NULL)
41+
return;
42+
Node *deleteNode = tail;
43+
tail = tail->prev;
44+
if (tail == NULL)
45+
head = NULL;
46+
else
47+
tail->next = NULL;
48+
delete deleteNode;
49+
sz--;
50+
}
51+
52+
int top()
53+
{
54+
return tail->val;
55+
}
56+
57+
int size()
58+
{
59+
return sz;
60+
}
61+
62+
bool empty()
63+
{
64+
return sz == 0;
65+
}
66+
};
67+
68+
class myQueue
69+
{
70+
Node *head = NULL, *tail = NULL;
71+
int sz = 0;
72+
73+
void push(int x)
74+
{
75+
sz++;
76+
Node *newNode = new Node(x);
77+
if (head == NULL)
78+
{
79+
head = newNode;
80+
tail = newNode;
81+
return;
82+
}
83+
tail->next = newNode;
84+
newNode->prev = tail;
85+
tail = newNode;
86+
}
87+
88+
void pop()
89+
{
90+
Node *deleteNode = head;
91+
head = head->next;
92+
if (head == NULL)
93+
tail = NULL;
94+
else
95+
head->prev = NULL;
96+
delete deleteNode;
97+
sz--;
98+
}
99+
100+
int front()
101+
{
102+
return head->val;
103+
}
104+
105+
int size()
106+
{
107+
return sz;
108+
}
109+
110+
bool empty()
111+
{
112+
return sz == 0;
113+
}
114+
};
115+
116+
int main()
117+
{
118+
int n, m;
119+
cin >> n >> m;
120+
121+
if (n != m)
122+
{
123+
cout << "NO" << endl;
124+
return 0;
125+
}
126+
127+
stack<int> st;
128+
queue<int> q;
129+
130+
for (int i = 0; i < n; i++)
131+
{
132+
int x;
133+
cin >> x;
134+
st.push(x);
135+
}
136+
137+
for (int i = 0; i < m; i++)
138+
{
139+
int x;
140+
cin >> x;
141+
q.push(x);
142+
}
143+
144+
bool flag = true;
145+
146+
while (!st.empty() && !q.empty())
147+
{
148+
if (st.top() != q.front())
149+
{
150+
flag = false;
151+
break;
152+
}
153+
st.pop();
154+
q.pop();
155+
}
156+
157+
if (flag)
158+
cout << "YES" << endl;
159+
else
160+
cout << "NO" << endl;
161+
162+
return 0;
163+
}

0 commit comments

Comments
 (0)