Skip to content

Create Stack using Two Queue #191

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions Peak_Element_CPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// A C++ program to find a peak element
#include <bits/stdc++.h>
using namespace std;

// Find the peak element in the array
int findPeak(int arr[], int n)
{
// first or last element is peak element
if (n == 1)
return arr[0];
if (arr[0] >= arr[1])
return 0;
if (arr[n - 1] >= arr[n - 2])
return n - 1;

// check for every other element
for (int i = 1; i < n - 1; i++) {

// check if the neighbors are smaller
if (arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1])
return i;
}
}

// Driver Code
int main()
{
int arr[] = { 1, 3, 20, 4, 1, 0 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Index of a peak point is "
<< findPeak(arr, n);
return 0;
}
94 changes: 94 additions & 0 deletions Stack using Two Queue
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include<iostream>
using namespace std;

struct qu1// queue1 declaration {
qu1 *n1;
int d1;
}*f1 = NULL, *r1 = NULL, *q1 = NULL, *p1 = NULL, *np1 = NULL;

struct qu2// queue2 declaration {
qu2 *n2;
int d2;
}*f2 = NULL, *r2 = NULL, *q2 = NULL, *p2 = NULL, *np2 = NULL;

void enqueue1(int a) {
np1 = new qu1;
np1->d1 = a;
np1->n1 = NULL;
if (f1 == NULL) {
r1 = np1;
r1->n1 = NULL;
f1 = r1;
} else {
r1->n1 = np1;
r1 = np1;
r1->n1 = NULL;
}
}

int dequeue1() {
int a;
if (f1 == NULL) {
cout<<"no elements present in queue\n";
} else {
q1 = f1;
f1 = f1->n1;
a = q1->d1;
delete(q1);
return a;
}
}

void enqueue2(int a) {
np2 = new qu2;
np2->d2 = a;
np2->n2 = NULL;
if (f2 == NULL) {
r2 = np2;
r2->n2 = NULL;
f2 = r2;
} else {
r2->n2 = np2;
r2 = np2;
r2->n2 = NULL;
}
}

int dequeue2() {
int a;
if (f2 == NULL) {
cout<<"no elements present in queue\n";
} else {
q2 = f2;
f2 = f2->n2;
a = q2->d2;
delete(q2);
return a;
}
}

int main() {
int n, a, i = 0;
cout<<"Enter the number of elements to be entered into stack\n";
cin>>n;
while (i < n) {
cout<<"enter the element to be entered\n";
cin>>a;
enqueue1(a);
i++;
}
cout<<"\n\nElements popped\n\n";
while (f1 != NULL || f2 != NULL)// if both queues are not null {
if (f2 == NULL)// if queue 2 is null {
while (f1->n1 != NULL) {
enqueue2(dequeue1());
}
cout<<dequeue1()<<endl;
} else if (f1 == NULL)//if queue 1 is null {
while (f2->n2 != NULL) {
enqueue1(dequeue2());
}
cout<<dequeue2()<<endl;
}
}
}
28 changes: 28 additions & 0 deletions Toogle_bits_given_range.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// C++ implementation to toggle bits in
// the given range
#include <bits/stdc++.h>
using namespace std;

// function to toggle bits in the given range
unsigned int toggleBitsFromLToR(unsigned int n,
unsigned int l, unsigned int r)
{
// calculating a number 'num' having 'r'
// number of bits and bits in the range l
// to r are the only set bits
int num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);

// toggle bits in the range l to r in 'n'
// and return the number
//Besides this, we can calculate num as: num=(1<<r)-l .
return (n ^ num);
}

// Driver program to test above
int main()
{
unsigned int n = 50;
unsigned int l = 2, r = 5;
cout << toggleBitsFromLToR(n, l, r);
return 0;
}