I have a stack in Java populated with integers Im trying to

I have a stack in Java populated with integers. I\'m trying to compare the elements in the stack to determine which is the largest. Basically, I enter the element at the top of the stack into e then pop each element off the stack and check to see if it is greater than the value stored in e. How can I check to see if the next element in the stack is greater than the element stored in e?

E e = stk1.peek();
while(!stk1.empty())
{
if(stk1.peek() > e)
{
e=stk1.peek();
}

Solution

Using recursive approach we can sort the stack and the top element of stack would be the largest integer.

Algorithm:

Sorting stack elements:

Inserting elements in sorted order:

Illustration:

First pop all the elements from the stack and store poped element in variable \'temp\'. After poping all the elements function\'s stack frame will look like:

Now stack is empty and \'insert_in_sorted_order()\' function is called and it inserts 30 (from stack frame #5) at the bottom of the stack. Now stack looks like below:

Now next element i.e. -5 (from stack frame #4) is picked. Since -5 < 30, -5 is inserted at the bottom of stack. Now stack becomes:

Next 18 (from stack frame #3) is picked. Since 18 < 30, 18 is inserted below 30. Now stack becomes:

Next 14 (from stack frame #2) is picked. Since 14 < 30 and 14 < 18, it is inserted below 18. Now stack becomes:

Now -3 (from stack frame #1) is picked, as -3 < 30 and -3 < 18 and -3 < 14, it is inserted below 14. Now stack becomes:

Implementation:

/**
* Stack is represented using linked list
*/

public class Stack extends NativeClass {
   private final IntContainer data = new IntContainer(this, 1);

   private final Container<Container<Stack>> next = new Container<Container<Stack>>(this, 1){};

   public int getData() {
       return data.get();
   }

   public int setData(int newData) {
       return data.set(newData);
   }

   public Container<Stack> getNext() {
       return next.get();
   }

   public Container<Stack> setNext(Container<Stack> newNext) {
       return next.set(newNext);
   }

   public static Container<Stack> nnc(Container<Stack> orig) {
       return Container.nnc(new TypeInfo<Container<Stack>>(){}, orig);
   }

   /**
   * Utility function to initialize stack
   */

   public static void initStack(Container<Stack>[] s) {
       s[0] = null;
   }

   /**
   * Utility function to chcek if stack is empty
   */

   public static int isEmpty(Stack[] s) {
       if(s == null) {
           return 1;
       }
       return 0;
   }

   /**
   * Utility function to push an item to stack
   */

   public static void push(Container<Stack>[] s, int x) {
       Container<Stack> p = new Container<Stack>(true, 1){};


       p.get().setData(x);
       p.get().setNext(s[0]);
       s[0] = p;
   }

   /**
   * Utility function to remove an item from stack
   */

   public static int pop(Container<Stack>[] s) {
       int x;
       Container<Stack> temp;

       x = s[0].get().getData();
       temp = s[0];
       s[0] = s[0].get().getNext();
       nnc(temp).setNativeControlled(false);

       return x;
   }

   /**
   * Function to find top item
   */

   public static int top(Stack[] s) {
       return s[0].getData();
   }
}

// Recursive function to insert an item x in sorted way

   public static void sortedInsert(Container<Container<Stack>> s, int x) {
       // Base case: Either stack is empty or newly inserted
       // item is greater than top (more than all existing)

       if(isEmpty(s.get()) != 0 || x > top(s.get())) {
           push(s, x);
           return;
       }

       // If top is greater, remove the top item and recur
       int temp = pop(s);
       sortedInsert(s, x);

       // Put back the top item removed earlier
       push(s, temp);
   }

   /**
   * Function to sort stack
   */

   public static void sortStack(Container<Container<Stack>> s) {
       // If stack is not empty
       if(isEmpty(s.get()) == 0) {
           // Remove the top item
           int x = pop(s);

           // Sort remaining stack
           sortStack(s);

           // Push the top item back in sorted stack
           sortedInsert(s, x);
       }
   }

// Utility function to print contents of stack

public static printStack(Container<Stack> s) {
       while(s != null) {
           System.out.print(s.get().getData() + \" \");
           s = s.get().getNext();
       }
       System.out.println();
   }

   /**
   * Driver Program
   */

   public static void main(String[] args) {
       Container<Container<Stack>> top = new Container<Container<Stack>>(1){};

       initStack(top);
       push(top, 30);
       push(top, -5);
       push(top, 18);
       push(top, 14);
       push(top, -3);

       System.out.println(\"Stack elements before sorting:\");
       printStack(top.get());

       sortStack(top);
       System.out.println(\"\ \");

       System.out.println(\"Stack elements after sorting:\");
       printStack(top.get());
   }
}

I have a stack in Java populated with integers. I\'m trying to compare the elements in the stack to determine which is the largest. Basically, I enter the eleme
I have a stack in Java populated with integers. I\'m trying to compare the elements in the stack to determine which is the largest. Basically, I enter the eleme
I have a stack in Java populated with integers. I\'m trying to compare the elements in the stack to determine which is the largest. Basically, I enter the eleme

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site