Data structures and algorithms-exercises

Source: Internet
Author: User

1. Implement a stack structure with special functions: Based on the implementation of the basic functions of the stack, the operation of the smallest element in the return stack is realized getmin>
Requirements:
1) The event complexity of the pop/push/getmin operation is O (1)
2) Design of stack type can use ready-made stack structure

 Public classSolutionone {PrivateLinkedlist<integer> Stackdata =NewLinkedlist<>(); PrivateLinkedlist<integer> stackmin =NewLinkedlist<>();  Public voidPushintnode)         {Stackdata.push (node); //when the value of the stack is pressed as the stack top element is more than the hour, the current into the stack element is pressed like the stackmin stack, otherwise, the Stackmin//stackmin pressure to the top element of the current stack (i.e. the smallest element) Stackmin         if(stackmin.size () = = 0 | | Node <Stackmin.peek ()) {            //pressure to stackmin as current valueStackmin.push (node); }Else{            //Otherwise, push the stackmin to the top of the stackStackmin.push (Stackmin.peek ()); }    }        //Stack, both stacks pop up at the same time     Public intpop () {stackmin.pop (); returnStackdata.pop (); }        //returns the top element of the stack     Public intTop () {returnStackdata.peek (); }        //gets the smallest element in the stack     Public intmin () {returnStackmin.peek (); }}

2. Write a class that can only implement queues with two stack structures, supporting the basic operation of the queue (Push,pop).
Given an action sequence ope and its length n, where the element is positive for the push operation, 0 for the pop operation, to ensure that the operation sequence is valid and must contain a pop operation, please return the result sequence of the pop.
Test examples:
[1,2,3,0,4,0],6
return: [+]

 Public classSolutiontwo { Public Static voidMain (string[] args) {//[1,2,3,0,4,0],6                int[] Ope = {1,2,3,0,4,0}; int[] result = Twostack (Ope, 6); //arrays.aslist (ope). ForEach (action);         for(inti = 0;i < Result.length;i + +) {System.out.println (result[i]); }    }         Public Static int[] Twostack (int[] Ope,intN) {Stack<Integer> Stack1 =NewStack<integer>(); Stack<Integer> Stack2 =NewStack<integer>(); //        intCount = 0;//number of log stacks        int[] result; //traversing a given array         for(inti = 0;i < N;i + +){            if(ope[i]! = 0) {//Press the stack to the Stack1Stack1.push (Ope[i]); }Else{Count++; }        }//Press Stack Completeresult =New int[Count]; //import data from Stack1 to Stack2         while(!Stack1.isempty ())        {Stack2.push (Stack1.pop ()); }                //get the results from the Stack2 shot stack         for(inti = 0;i < count &&!stack2.isempty (); i + +) {Result[i]=Stack2.pop (); }        returnresult; }}

3. To implement the reverse of a stack, but only with the recursive function and the stack itself to implement the pop operation, and can not apply for additional data structure.

 Public classSolutionthree { Public Static voidMain (string[] args) {Stack<Integer> s =NewStack<integer>(); /*S.push (1);        S.push (2); S.push (3);*/                        //[230,272,224,399,126]S.addall (Arrays.aslist (230,272,224,399,126)); Solutionthree St=NewSolutionthree ();                St.reverse (s); S.foreach (i-{System.out.print (i+ " ");                    }); }        //use the following get to implement the reverse order of stack elements     Public voidReverse (stack<integer>stack) {        if(Stack.isempty ()) {return; }                inti =get (stack);        reverse (stack);    Stack.push (i); }            //Using recursion to remove the stack-bottom element and return it, only use the stack's own method,//cannot use additional data structures     Public intGet (stack<integer>stack) {        intresult =Stack.pop (); if(Stack.isempty ()) {returnresult; }Else{            intLast =get (stack);            Stack.push (result); returnLast ; }            }}

4. Write a program that sorts the stacks in ascending order (that is, the largest element is at the top of the stack).
Only one additional stack is required to hold temporary data, but the elements must not be copied into other data structures.
Given a int[] numbers (vector<int> in C + +), where the first element is the top of the stack,
Please return to the sorted stack. Note that this is a stack, meaning that you can only access the first element during the sorting process.

 Public classSolutionfour { Public Static voidMain (string[] args) {Solutionfour F=NewSolutionfour (); int[] in = { 54695,46580,6418,52304,5595,5149,51943,11454,23596,6444,61037,94146,50220,98642,97292,57898,11745,7286,31224,5160,41550,2 5277,59350,53353,68663,9642,30406,5396,3222,67194,7124,54247,15077,97688,36939,62888,80307,65467,6882,97071,39652,38268,8 8226,89088,92198,39003,9858,73803,83078,24648,49891,34551,57649,24443,30685,68740,55407,53155,87465,89282,41856,96218,372 92,24551,67663,31715,46363,25573,61921,56333,69576,55919,19818,26409,21590,70392,67648,36909,89175,74443,41856,11755,2478 8,25975,25116,57360,80998,62093,40691,91189,29337,68914,57653,64272,53653,5975,27967,59600,25803,13937,93725,26457,16603 , 18360,79926,63243,94958,45131};    F.twostackssort (in); }     PublicArraylist<integer> Twostackssort (int[] numbers) {ArrayList<Integer> result =NewArraylist<integer>(); Stack<Integer> Stackdata =NewStack<integer>(); Stack<Integer> Stackhelp =NewStack<integer>();  for(inti = Numbers.length-1;i >= 0;i--) {Stackdata.push (numbers[i]); }                         while(!Stackdata.isempty ()) {            if(Stackhelp.isempty ()) {Stackhelp.push (Stackdata.pop ()); }Else{                intCurrent =Stackdata.pop (); if(Current <Stackhelp.peek ()) {                     while(!Stackhelp.isempty ())                        {Stackdata.push (Stackhelp.pop ()); if(!stackhelp.isempty () && current >=Stackhelp.peek ())                            {Stackhelp.push (current);  Break; }Else if(Stackhelp.isempty ()) {Stackhelp.push (current);  Break; }                                                                    }                }Else{Stackhelp.push (current); }}} Stackhelp.foreach (I-{System.out.print (i+ "  ");        Result.add (i);                }); returnresult; }    }

Data structures and algorithms-exercises

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.