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> next = new Container>(this, 1){};
public int getData() {
return data.get();
}
public int setData(int newData) {
return data.set(newData);
}
public Container getNext() {
return next.get();
}
public Container setNext(Container newNext) {
return next.set(newNext);
}
public static Container nnc(Container orig) {
return Container.nnc(new TypeInfo>(){}, orig);
}
/**
* Utility function to initialize stack
*/
public static void initStack(Container[] 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[] s, int x) {
Container p = new Container(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[] s) {
int x;
Container 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> 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> 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 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> top = new Container>(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. Im trying to compa.pdf

  • 1.
    I have astack 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
  • 2.
    */ public class Stackextends NativeClass { private final IntContainer data = new IntContainer(this, 1); private final Container> next = new Container>(this, 1){}; public int getData() { return data.get(); } public int setData(int newData) { return data.set(newData); } public Container getNext() { return next.get(); } public Container setNext(Container newNext) { return next.set(newNext); } public static Container nnc(Container orig) { return Container.nnc(new TypeInfo>(){}, orig); } /** * Utility function to initialize stack */ public static void initStack(Container[] 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
  • 3.
    */ public static voidpush(Container[] s, int x) { Container p = new Container(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[] s) { int x; Container 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> 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
  • 4.
    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> 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 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> top = new Container>(1){}; initStack(top); push(top, 30); push(top, -5); push(top, 18);
  • 5.
    push(top, 14); push(top, -3); System.out.println("Stackelements before sorting:"); printStack(top.get()); sortStack(top); System.out.println(" "); System.out.println("Stack elements after sorting:"); printStack(top.get()); } }