Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new algorithms #2108

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions 2D_Array/Transpose_matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

#include <bits/stdc++.h>
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;
}

42 changes: 42 additions & 0 deletions Queue/String_to_queue
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <ostream>
#include <istream>

int main ()
{
// Declare your queue of type std::string
std::queue<std::string> 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;
}
53 changes: 53 additions & 0 deletions stack is implemented by queue
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <bits/stdc++.h>
using namespace std;

struct Queue {
stack<int> 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;
}
Loading