java-57-Queue && Implement a stack with two queues with two stacks

Source: Internet
Author: User

Transferred from: http://bylijinnan.iteye.com/blog/1450125

————————————————————————————————————————————

Java code
  1. Import java.util.ArrayList;
  2. Import java.util.List;
  3. Import Java.util.Stack;
  4. /*  
  5. * Q 57 with two stacks to implement a queue
  6. */
  7. Public class Queueimplementbytwostacks {
  8. private stack<integer> Stack1;
  9. private stack<integer> Stack2;
  10. Queueimplementbytwostacks () {
  11. stack1=new stack<integer> ();
  12. stack2=new stack<integer> ();
  13. }
  14. Public Integer Poll () {
  15. Integer re=null;
  16. if (!stack2.empty ()) {
  17. Re=stack2.pop ();
  18. }else{
  19. While (!stack1.empty ()) {//move to Stack2 to make Stack1 has only oneelement.   Then pops this element.
  20. Re=stack1.pop ();
  21. Stack2.push (re);
  22. }
  23. if (!stack2.empty ()) {
  24. Re=stack2.pop ();
  25. }
  26. }
  27. return re;
  28. }
  29. Public Integer Offer (int o) {
  30. Stack1.push (o);
  31. return o;
  32. }
  33. public static void Main (string[] args) {
  34. Queueimplementbytwostacks queue=New Queueimplementbytwostacks ();
  35. list<integer> re=New arraylist<integer> ();
  36. Queue.offer (1);
  37. Queue.offer (2);
  38. Queue.offer (3);
  39. Re.add (Queue.poll ());
  40. Queue.offer (4);
  41. Re.add (Queue.poll ());
  42. Queue.offer (5);
  43. Re.add (Queue.poll ());
  44. Re.add (Queue.poll ());
  45. Re.add (Queue.poll ());
  46. System.out.println (Re.tostring ());
  47. }
  48. }
Import Java.util.arraylist;import java.util.list;import java.util.stack;/* * Q 57 Implement Queue */public class with two stacks queueimplementbytwostacks {private stack<integer> stack1;private stack<integer> Stack2; Queueimplementbytwostacks () {stack1=new stack<integer> (); stack2=new stack<integer> ();} Public Integer Poll () {Integer re=null;if (!stack2.empty ()) {Re=stack2.pop ();} Else{while (!stack1.empty ()) {//move to Stack2 and make Stack1 has only one element. Then pops this element.re=stack1.pop (); Stack2.push (re);} if (!stack2.empty ()) {Re=stack2.pop ();}} return re;} Public Integer offer (int o) {stack1.push (o); return o;} public static void Main (string[] args) {queueimplementbytwostacks queue=new queueimplementbytwostacks (); List<integer> re=new arraylist<integer> (); Queue.offer (1); Queue.offer (2); Queue.offer (3); Re.add ( Queue.poll ()); Queue.offer (4); Re.add (Queue.poll ()); Queue.offer (5); Re.add (Queue.poll ()); Re.add (Queue.poll ()); Re.add (Queue.poll ()); System.out.println (Re.tostring ());}}



Java code
    1. Import java.util.LinkedList;
    2. /*  
    3. * Q 57 Implementation of a stack with two queues
    4. */
    5. Public class Stackimplementbytwoqueues {
    6. //use ' queue1 ' and ' queue2 ' as a queue.   That means only use the method ' AddLast ' and ' Removefirst '.
    7. private linkedlist<integer> queue1;
    8. private linkedlist<integer> queue2;
    9. Stackimplementbytwoqueues () {
    10. queue1=new linkedlist<integer> ();
    11. queue2=new linkedlist<integer> ();
    12. }
    13. Public Integer Pop () {
    14. Integer re=null;
    15. if (queue1.size () = =0&&queue2.size () = =0) {
    16. return null;
    17. }
    18. if (queue2.size () = =0) {
    19. While (Queue1.size () >0) {
    20. Re=queue1.removefirst ();
    21. if (queue1.size ()! =0) {//do not add the last element of Queue1 to Queue2
    22. Queue2.addlast (re);
    23. }
    24. }
    25. }Else if (queue1.size () = =0) {
    26. While (Queue2.size () >0) {
    27. Re=queue2.removefirst ();
    28. if (queue2.size ()! =0) {//do not add the last element of Queue2 to Queue1
    29. Queue1.addlast (re);
    30. }
    31. }
    32. }
    33. return re;
    34. }
    35. Public integer push (integer o) {
    36. if (queue1.size () = =0&&queue2.size () = =0) {
    37. Queue1.addlast (o); //queue2.addlast (o); is also OK
    38. }
    39. if (queue1.size ()! =0) {
    40. Queue1.addlast (o);
    41. }Else if (queue2.size ()! =0) {
    42. Queue2.addlast (o);
    43. }
    44. return o;
    45. }
    46. public static void Main (string[] args) {
    47. Stackimplementbytwoqueues stack=New Stackimplementbytwoqueues ();
    48. int tmp=0;
    49. Stack.push (1);
    50. Stack.push (2);
    51. Stack.push (3);
    52. Tmp=stack.pop ();
    53. SYSTEM.OUT.PRINTLN (TMP); //3
    54. Stack.push (4);
    55. Tmp=stack.pop ();
    56. SYSTEM.OUT.PRINTLN (TMP); //4
    57. Tmp=stack.pop ();
    58. SYSTEM.OUT.PRINTLN (TMP); //2
    59. Stack.push (5);
    60. Stack.push (6);
    61. Tmp=stack.pop ();
    62. SYSTEM.OUT.PRINTLN (TMP); //6
    63. Tmp=stack.pop ();
    64. SYSTEM.OUT.PRINTLN (TMP); //5
    65. Tmp=stack.pop ();
    66. SYSTEM.OUT.PRINTLN (TMP); //1
    67. }
    68. }

java-57-Queue && Implement a stack with two queues with two stacks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.