Filip_Markoski

[ADSx] - StackAndQueueFun

Jan 29th, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.40 KB | None | 0 0
  1. import java.util.ListIterator;
  2. import java.util.Stack;
  3.  
  4. public class StackAndQueueFun {
  5.     public static void main(String args[]) {
  6.     }
  7. }
  8.  
  9. /**
  10.  * https://stackoverflow.com/questions/5134127/sorting-a-queue-using-same-queue
  11.  * */
  12.  
  13. class SortStack {
  14.     /**
  15.      * https://www.geeksforgeeks.org/sort-stack-using-temporary-stack/
  16.      */
  17.  
  18.     // This function return the sorted stack
  19.     public static Stack<Integer> sortStack(Stack<Integer> input) {
  20.         Stack<Integer> tmpStack = new Stack<>();
  21.         while (!input.isEmpty()) {
  22.             // pop out the first element
  23.             int tmp = input.pop();
  24.  
  25.             System.out.printf("tmp: %d | input: %s | tmpStack: %s\n", tmp, input, tmpStack);
  26.  
  27.             // while temporary stack is not empty and
  28.             // top of stack is greater than temp
  29.             while (!tmpStack.isEmpty() && tmpStack.peek() < tmp) {
  30.                 // pop from temporary stack and
  31.                 // push it to the input stack
  32.                 input.push(tmpStack.pop());
  33.             }
  34.  
  35.  
  36.             // push temp in temporary stack
  37.             tmpStack.push(tmp);
  38.             System.out.printf("tmp: %d | input: %s | tmpStack: %s\n", tmp, input, tmpStack);
  39.         }
  40.         return tmpStack;
  41.     }
  42.  
  43.     // Driver Code
  44.     public static void main(String args[]) {
  45.         Stack<Integer> input = new Stack<>();
  46.         input.add(34);
  47.         input.add(3);
  48.         input.add(31);
  49.         input.add(98);
  50.         input.add(92);
  51.         input.add(23);
  52.  
  53.         // This is the temporary stack
  54.         Stack<Integer> tmpStack = sortStack(input);
  55.         System.out.println("Sorted numbers are:");
  56.  
  57.         while (!tmpStack.empty()) {
  58.             System.out.print(tmpStack.pop() + " ");
  59.         }
  60.     }
  61. }
  62.  
  63. // Java program to sort a Stack using recursion
  64. // Note that here predefined Stack class is used
  65. // for stack operation
  66. class SortStackRecursion {
  67.     /**
  68.      * https://www.geeksforgeeks.org/sort-a-stack-using-recursion/
  69.      */
  70.  
  71.     // Recursive Method to insert an item x in sorted way
  72.     static void sortedInsert(Stack<Integer> s, int x) {
  73.         // Base case: Either stack is empty or newly inserted
  74.         // item is greater than top (more than all existing)
  75.         if (s.isEmpty() || x > s.peek()) {
  76.             s.push(x);
  77.             return;
  78.         }
  79.  
  80.         // If top is greater, remove the top item and recur
  81.         int temp = s.pop();
  82.         sortedInsert(s, x);
  83.  
  84.         // Put back the top item removed earlier
  85.         s.push(temp);
  86.     }
  87.  
  88.     // Method to sort stack
  89.     static void sortStack(Stack<Integer> s) {
  90.         // If stack is not empty
  91.         if (!s.isEmpty()) {
  92.             // Remove the top item
  93.             int x = s.pop();
  94.  
  95.             // Sort remaining stack
  96.             sortStack(s);
  97.  
  98.             // Push the top item back in sorted stack
  99.             sortedInsert(s, x);
  100.         }
  101.     }
  102.  
  103.     // Utility Method to print contents of stack
  104.     static void printStack(Stack<Integer> s) {
  105.         ListIterator<Integer> lt = s.listIterator();
  106.  
  107.         // forwarding
  108.         while (lt.hasNext())
  109.             lt.next();
  110.  
  111.         // printing from top to bottom
  112.         while (lt.hasPrevious())
  113.             System.out.print(lt.previous() + " ");
  114.     }
  115.  
  116.     // Driver method
  117.     public static void main(String[] args) {
  118.         Stack<Integer> s = new Stack<>();
  119.         s.push(30);
  120.         s.push(-5);
  121.         s.push(18);
  122.         s.push(14);
  123.         s.push(-3);
  124.  
  125.         System.out.println("Stack elements before sorting: ");
  126.         printStack(s);
  127.  
  128.         sortStack(s);
  129.  
  130.         System.out.println(" \n\nStack elements after sorting:");
  131.         printStack(s);
  132.  
  133.     }
  134. }
  135.  
  136.  
  137. class ReverseStackRecursion {
  138.     /**
  139.      * https://www.geeksforgeeks.org/reverse-a-stack-using-recursion/
  140.      * */
  141.  
  142.     //using Stack class for stack implementation
  143.     static Stack<Character> st = new Stack<>();
  144.  
  145.     // Below is a recursive function that inserts an element
  146.     // at the bottom of a stack.
  147.     static void insert_at_bottom(char x) {
  148.  
  149.         if (st.isEmpty())
  150.             st.push(x);
  151.  
  152.         else {
  153.             /* All items are held in Function Call Stack until we
  154.                reach end of the stack. When the stack becomes
  155.                empty, the st.size() becomes 0, the
  156.                above if part is executed and the item is inserted
  157.                at the bottom */
  158.  
  159.             char a = st.peek();
  160.             st.pop();
  161.             insert_at_bottom(x);
  162.  
  163.             //push allthe items held in Function Call Stack
  164.             //once the item is inserted at the bottom
  165.             st.push(a);
  166.         }
  167.     }
  168.  
  169.     // Below is the function that reverses the given stack using
  170.     // insert_at_bottom()
  171.     static void reverse() {
  172.         if (st.size() > 0) {
  173.             /* Hold all items in Function Call Stack until we
  174.                reach end of the stack */
  175.             char x = st.peek();
  176.             st.pop();
  177.             reverse();
  178.             /* Insert all the items held in Function Call Stack
  179.                one by one from the bottom to top. Every item is
  180.                inserted at the bottom */
  181.             insert_at_bottom(x);
  182.         }
  183.     }
  184.  
  185.  
  186.     // Driver method
  187.     public static void main(String[] args) {
  188.         //push elements into the stack
  189.         st.push('1');
  190.         st.push('2');
  191.         st.push('3');
  192.         st.push('4');
  193.  
  194.         System.out.println("Original Stack");
  195.  
  196.         System.out.println(st);
  197.  
  198.         //function to reverse the stack
  199.         reverse();
  200.  
  201.         System.out.println("Reversed Stack");
  202.  
  203.         System.out.println(st);
  204.     }
  205. }
  206.  
  207.  
  208. // Java program to implement HisStack using linked
  209. // list so that reverse can be done with O(1)
  210. // extra space.
  211. class HisStackNode {
  212.     int data;
  213.     HisStackNode next;
  214.  
  215.     public HisStackNode(int data) {
  216.         this.data = data;
  217.         this.next = null;
  218.     }
  219. }
  220.  
  221. class HisStack {
  222.     HisStackNode top;
  223.  
  224.     // Push and pop operations
  225.     public void push(int data) {
  226.         if (this.top == null) {
  227.             top = new HisStackNode(data);
  228.             return;
  229.         }
  230.         HisStackNode s = new HisStackNode(data);
  231.         s.next = this.top;
  232.         this.top = s;
  233.     }
  234.  
  235.     public HisStackNode pop() {
  236.         HisStackNode s = this.top;
  237.         this.top = this.top.next;
  238.         return s;
  239.     }
  240.  
  241.     // prints contents of HisStack
  242.     public void display() {
  243.         HisStackNode s = this.top;
  244.         while (s != null) {
  245.             System.out.print(" " + s.data);
  246.             s = s.next;
  247.         }
  248.         System.out.println();
  249.     }
  250.  
  251.     // Reverses the HisStack using simple
  252.     // linked list reversal logic.
  253.     public void reverse() {
  254.         HisStackNode prev, cur, succ;
  255.         cur = prev = this.top;
  256.         cur = cur.next;
  257.         prev.next = null;
  258.         while (cur != null) {
  259.  
  260.             succ = cur.next;
  261.             cur.next = prev;
  262.             prev = cur;
  263.             cur = succ;
  264.         }
  265.         this.top = prev;
  266.     }
  267. }
  268.  
  269. class reverseHisStackWithoutSpace {
  270.     public static void main(String[] args) {
  271.         HisStack s = new HisStack();
  272.         s.push(1);
  273.         s.push(2);
  274.         s.push(3);
  275.         s.push(4);
  276.         System.out.println("Original HisStack");
  277.         s.display();
  278.  
  279.         // reverse
  280.         s.reverse();
  281.  
  282.         System.out.println("Reversed HisStack");
  283.         s.display();
  284.     }
  285. }
Advertisement
Add Comment
Please, Sign In to add comment