Implement a queue class that uses two stacks

Source: Internet
Author: User
1/* 2 * implement a queue class. This class uses two stacks to implement 3 * the main difference between queues and stacks is that queues are first-in-first-out, just like in the canteen [for party meals, first-come, first-served 4 * stacks are first-in-first-out, just like bullets in the gun, the first step is to minimize the pressure to 5 * so we only need to modify the peek () and POP () to execute the operation in reverse order, we can use the Second stack to reverse the order of elements 6 *. This approach is feasible, but it is not the most efficient. The elements are removed and moved repeatedly, which is unnecessary. 7 * Here we can delay the movement of elements so that the elements remain in the Second stack, only elements 8*9*10 */11 12 Import Java are moved when the order of elements must be reversed. util. stack; 13 14 public class stacktoqueue <t> {15 stack <t> stacknew, stackold; 16 public stacktoqueue () 17 {18 stacknew = new stack <t> (); 19 stackold = new stack <t> (); 20 21} 22 public int size () 23 {24 return stacknew. size () + stackold. size (); 25} 26 Public void add (T value) // Add elements in the first stack 27 {28 stacknew. push (value); 29} 30 Private void shiftstacks () // reverse the order of implemented elements. elements are extracted from the first stack and placed in the Second stack 31 {32 If (stackold. isempty () 33 {34 while (! Stacknew. isempty () 35 {36 stackold. push (stacknew. pop (); 37} 38} 39} 40 public t PEEK () 41 {42 shiftstacks (); 43 return stackold. peek (); 44} 45 public t remove () 46 {47 shiftstacks (); 48 return stackold. pop (); 49} 50 51 public static void main (string [] ARGs) {52 // todo auto-generated method stub53 stacktoqueue queue = new stacktoqueue (); 54 queue. add (1); 55 queue. add (2); 56 queue. add (3); 57 queue. add (4); 58 queue. add (5); 59 int temp = queue. size (); 60 for (INT I = 0; I <temp; I ++) 61 {62 system. out. println ("Header element:" + queue. remove (); 63} 64 65 66} 67 68}

 

Implement a queue class that uses 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.