Results 1 to 4 of 4
  1. #1
    CodeX Pro is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Error in stack code

    Problem solved so I have removed the codes
    Last edited by CodeX Pro; 07-25-2013 at 11:29 AM.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Error in stack code

    "during the execution it shows some logical error" - post it.

    BTW, if this isn't homework... just use a Deque (Java Platform SE 6).
    Get in the habit of using standard Java naming conventions!

  3. #3
    CodeX Pro is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: Error in stack code

    Quote Originally Posted by kjkrum View Post
    "during the execution it shows some logical error" - post it.

    BTW, if this isn't homework... just use a Deque (Java Platform SE 6).
    No its not home work and I have figured out the error and have solved it

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Error in stack code

    Quote Originally Posted by CodeX Pro View Post
    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:
    Quote Originally Posted by CodeX Pro View Post
    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.

Similar Threads

  1. Code not executing (stack implementation)
    By mafro in forum New To Java
    Replies: 5
    Last Post: 03-22-2012, 04:42 PM
  2. Replies: 6
    Last Post: 08-20-2010, 02:13 PM
  3. Error stack trace
    By Arnold in forum Android
    Replies: 1
    Last Post: 05-13-2010, 05:54 PM
  4. stack over flow error
    By sara12345 in forum New To Java
    Replies: 3
    Last Post: 04-14-2010, 09:50 PM
  5. error stack when I try to execute my code
    By paty in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •