0

I'm trying to create a stack for characters in java but there's a bug which doesn't push the characters into stack. I input a String and extract each character to place in Stack. Weirdly, the stack only stores one character of string. Can someone help me out?

int top, capacity;
char[] stack;

stack7() {
    top = -1;
    capacity = 6;
    stack = new char[capacity];
}

public boolean isempty() {
    return top == -1;
}

public boolean isfull() {
    return top == capacity - 1;
}

public void push(char data) {
    if (isfull()) {
        System.out.println("Stack is full");
    } else {
        stack[++top] = data;
    }
}

public char pop() {
    if (isempty()) {
        System.out.println("Stack is empty");
    }
    return stack[top--];
}

public char peek() {
    return stack[top];
}

public void display() {
    int temp = top;
    if (temp >= 0) {
        System.out.print(stack[temp] + " ");
        temp--;
    }
    System.out.println();
}

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("enter string");
    String str = sc.nextLine();
    stack7 st = new stack7();
    for (int i = 0; i < str.length(); i++) {
        st.push(str.charAt(i));
    }
    st.display();
}
trincot
  • 317,000
  • 35
  • 244
  • 286
theastros
  • 3
  • 4
  • 3
    Try replacing if(temp >= 0) with while(temp>=0) in display method – Rocco Apr 19 '21 at 13:16
  • 1
    *there are many alternatives to a homebrewed stack in* Java Vector stack = new Vector<>(); // push and pop must be implemented Stack stack = new Stack<>(); Deque stack = new ArrayDeque<>(); stack.forEach(e -> System.out.println(e)); // print the stack You can run out of memory, but a stack is never *full* – Kaplan Apr 19 '21 at 16:05
  • 1
    See [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Stepping through the code would have let you figure out the problem yourself, plus using a debugger is an essential skill to learn. – Bohemian Apr 19 '21 at 16:22
  • There are 2 mistakes in Your implementation: isfull `return top >= capacity;` and push `stack[top++] = data;`. – Kaplan Apr 19 '21 at 16:27
  • By the way, the `char` type in Java is obsolete, unable to represent even half of the characters defined in Unicode. Use [code point](https://en.wikipedia.org/wiki/Code_point) integer numbers instead: `int[] stack;`. – Basil Bourque Apr 19 '21 at 16:31
  • @Kaplan changing the top to postfix is creating error out of bound exception – theastros Apr 19 '21 at 16:51
  • @Kaplan I'm not sure of generics, I need to practice on them. I can't find a perfect tutorial to implement stacks using generics from scratch – theastros Apr 19 '21 at 17:41
  • Normally a stack is growing as capacity is exceeded. If You want to limit the stack-size then You must check when `str.length()` is greater than `capacity` and stop filling the stack if the `capacity` is exceeded. You must not use generics. Write eg. `Stack stack = new Stack();` and ignore the compiler warning. – Kaplan Apr 19 '21 at 18:16

0 Answers0