From b9453b2e2b8fb8f5b5010a6f60c0d784cfe9bd19 Mon Sep 17 00:00:00 2001 From: Yash Raghuvanshi Date: Fri, 31 May 2024 18:17:14 +0530 Subject: [PATCH 1/3] Create stack is implemented by queue --- stack is implemented by queue | 53 +++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 stack is implemented by queue diff --git a/stack is implemented by queue b/stack is implemented by queue new file mode 100644 index 000000000..77aac6a8a --- /dev/null +++ b/stack is implemented by queue @@ -0,0 +1,53 @@ +#include +using namespace std; + +struct Queue { + stack s1, s2; + + void enQueue(int x) + { + // Move all elements from s1 to s2 + while (!s1.empty()) { + s2.push(s1.top()); + s1.pop(); + } + + // Push item into s1 + s1.push(x); + + // Push everything back to s1 + while (!s2.empty()) { + s1.push(s2.top()); + s2.pop(); + } + } + + // Dequeue an item from the queue + int deQueue() + { + // if first stack is empty + if (s1.empty()) { + return -1; + } + + // Return top of s1 + int x = s1.top(); + s1.pop(); + return x; + } +}; + +// Driver code +int main() +{ + Queue q; + q.enQueue(1); + q.enQueue(2); + q.enQueue(3); + + cout << q.deQueue() << '\n'; + cout << q.deQueue() << '\n'; + cout << q.deQueue() << '\n'; + + return 0; +} From 23fd39e2fa88abffb4b8f96f49ce6be386293fd6 Mon Sep 17 00:00:00 2001 From: Yash Raghuvanshi Date: Sat, 1 Jun 2024 22:09:00 +0530 Subject: [PATCH 2/3] Create String_to_queue --- Queue/String_to_queue | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Queue/String_to_queue diff --git a/Queue/String_to_queue b/Queue/String_to_queue new file mode 100644 index 000000000..729bd773c --- /dev/null +++ b/Queue/String_to_queue @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include + +int main () +{ + // Declare your queue of type std::string + std::queue q; + + // Push 1 to 3 + q.push ("1"); + q.push ("2"); + q.push ("3"); + + // Declare a string variable + std::string input; + + // Prompt + std::cout << "- Please input a string: " << std::endl; + + // Catch user input and store + std::cin >> input; + + // Push value inputted by the user + q.push(input); + + // Loop while the queue is not empty, while popping each value + while (not q.empty ()) + { + // Output front of the queue + std::cout << q.front () << std::endl; + // Pop the queue, delete item + q.pop (); + } + // New line, formatting purposes + std::cout << std::endl; + + return 0; +} From c51e79ada2228b3e516fc4ee2c5065942c151c31 Mon Sep 17 00:00:00 2001 From: Yash Raghuvanshi Date: Mon, 10 Jun 2024 15:49:22 +0530 Subject: [PATCH 3/3] Create Transpose_matrix.cpp --- 2D_Array/Transpose_matrix.cpp | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2D_Array/Transpose_matrix.cpp diff --git a/2D_Array/Transpose_matrix.cpp b/2D_Array/Transpose_matrix.cpp new file mode 100644 index 000000000..009ed6963 --- /dev/null +++ b/2D_Array/Transpose_matrix.cpp @@ -0,0 +1,40 @@ + +#include +using namespace std; +#define N 4 + +class solution{ + void transpose(int A[][N], int B[][N]) + { + int i, j; + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + B[i][j] = A[j][i]; + } +}; + + +int main() +{ + solution s; + int A[N][N] = { { 1, 1, 1, 1 }, + { 2, 2, 2, 2 }, + { 3, 3, 3, 3 }, + { 4, 4, 4, 4 } }; + + + int B[N][N], i, j; + + + s.transpose(A, B); + + cout << "Result matrix is \n"; + for (i = 0; i < N; i++) { + for (j = 0; j < N; j++) + cout << " " << B[i][j]; + + cout << "\n"; + } + return 0; +} +