-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from Gargee07/Branch-for-Stack-using-two-queue…
…s-Code Solution to Issue #56 Stack using two queues
- Loading branch information
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Implementation of Stack using two Queue | ||
*/ | ||
package stackUsingQueues; | ||
import java.util.*; | ||
|
||
class Stack{ // Stack class where all methods of stack are created | ||
Queue<Integer> q1 = new LinkedList<Integer>(); | ||
Queue<Integer> q2 = new LinkedList<Integer>(); | ||
|
||
//using Push Operation costly approach | ||
void push(int element) { | ||
|
||
q2.add(element); //adding recently entered element to q2 | ||
|
||
while (!q1.isEmpty()) { | ||
q2.add(q1.peek()); // moving all the elements of q1 to q2 | ||
q1.remove(); | ||
} | ||
|
||
Queue<Integer> temp = q1; | ||
q1 = q2; //swapping the queues | ||
q2 = temp; | ||
} | ||
|
||
|
||
int pop() { // returns and remove the recently entered element | ||
if (q1.isEmpty()) { | ||
return -1; } //returns -1 for empty queue | ||
else{ | ||
return q1.remove(); | ||
} | ||
} | ||
} | ||
public class Main { //Main class | ||
|
||
public static void main(String[] args) { | ||
Scanner input=new Scanner (System.in); | ||
Stack stack=new Stack(); //Instantiating Stack class | ||
stack.push(2); | ||
stack.push(3); | ||
System.out.print(stack.pop()+" "); | ||
stack.push(4); | ||
System.out.print(stack.pop()+" "); | ||
|
||
|
||
input.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Implementation of Stack using two Queue | ||
*/ | ||
package stackImplementation; | ||
import java.util.*; | ||
|
||
|
||
class Stack{ // Stack class where all methods of stack are created | ||
Queue<Integer> q1 = new LinkedList<Integer>(); | ||
Queue<Integer> q2 = new LinkedList<Integer>(); | ||
|
||
//using Push Operation costly approach | ||
void push(int element) { | ||
|
||
q2.add(element); //adding recently entered element to q2 | ||
|
||
while (!q1.isEmpty()) { | ||
q2.add(q1.peek()); // moving all the elements of q1 to q2 | ||
q1.remove(); | ||
} | ||
|
||
Queue<Integer> temp = q1; | ||
q1 = q2; //swapping the queues | ||
q2 = temp; | ||
} | ||
|
||
|
||
int pop() { // returns and remove the recently entered element | ||
if (q1.isEmpty()) { | ||
return -1; } //returns -1 for empty queue | ||
else{ | ||
return q1.remove(); | ||
} | ||
} | ||
} | ||
|
||
public class Stack_Using_Queues { | ||
|
||
public static void main(String[] args) { | ||
Scanner input=new Scanner (System.in); | ||
Stack stack=new Stack(); //Instantiating Stack class | ||
stack.push(2); | ||
stack.push(3); | ||
System.out.print(stack.pop()+" "); | ||
stack.push(4); | ||
System.out.print(stack.pop()+" "); | ||
|
||
|
||
|
||
input.close(); | ||
} | ||
|
||
} |