Re: Error in stack code

Originally Posted by
CodeX Pro
Problem solved so I have removed the codes
This is a forum, where we share problems and their possible solutions.
For the record, this is the original post:

Originally Posted by
CodeX Pro
hello all,
I want to implement stack data structure using interfaces and in such a way that it works for all kinds of data types. Which we will specify by JStack<Integer> or JStack<String> etc.
I have done the following but during the execution it shows some logical error in JStack class please help me out.
StackInterface.java
Java Code:
package DataStructures;
/**
*
* @author CodeX Pro
*/
public interface StackInterface<ObjectDataType> {
/**
*
* @param item This method push the item passed into the stack
*/
public abstract void push(ObjectDataType item);
/**
*
* @return This method removes the top element from the stack
*/
public abstract ObjectDataType pop();
/**
*
* @return This method returns whether the stack is empty or not
*/
public abstract boolean isEmpty();
/**
*
* @return This Methods returns the top element of the stack
*/
public abstract ObjectDataType peek();
/**
*
* @return This method returns the size of the stack
*/
public abstract int size();
}
JStack.java (This contains the implementations of the methods)
Java Code:
package DataStructures;
import java.util.LinkedList;
/**
*
* @author Codex Pro
*/
public class JStack<E> implements StackInterface<E>{
private LinkedList<E> list = new LinkedList<>();
public JStack(){
list=null;
}
@Override
public void push(E item) {
list.addFirst(item);
}
@Override
public E pop() {
return list.removeFirst();
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}
@Override
public E peek() {
return list.getFirst();
}
@Override
public int size() {
return list.size();
}
}
StackDriver.java : This is a test class for checking the working of the JStack
Java Code:
package DataStructures;
import java.util.Scanner;
/**
*
* @author CodeX Pro
*/
public class StackDriver {
private JStack<Integer> stack;
public StackDriver() {
this.stack = new JStack<>();
}
public static void main(String[] args) {
StackDriver obj = new StackDriver();
int item, choice;
boolean proceed = true;
Scanner sc = new Scanner(System.in);
while (proceed) {
System.out.println("Menu\n");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Peek");
System.out.println("4. Size");
System.out.println("\nEnter your choice :-");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Enter an item to be entered into the stack :");
item = sc.nextInt();
obj.stack.push(item);
System.out.println("Element " + item + " pushed into stack");
break;
case 2:
if (!obj.stack.isEmpty()) {
item = obj.stack.pop();
System.out.println("Element " + item + " poped from stack");
} else {
System.out.println("Stack Underflow. No items found in stack");
}
break;
case 3:
if (!obj.stack.isEmpty()) {
item = obj.stack.peek();
System.out.println("Top Element in the stack is " + item);
} else {
System.out.println("Stack Underflow. No items found in stack");
}
break;
case 4:item=obj.stack.size();
System.out.println("Size of Stack is :- "+item);
break;
default:
System.out.println("Wrong Choice!");
}
System.out.println("\nDo you want to continue[true/false] :-");
proceed = sc.nextBoolean();
}
}
}
db
THREAD CLOSED
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
Bookmarks