How to determine size, first STACK in java and delete data between STACK

I have 3 books from "A" to "C". How to display them using Stackand then display its size. Also, how can I delete book "B" and then see the size again?

This is my code:

import java.util.*;
public class test_stack {
    public static void main (String[] args){

        Stack<String> stack = new Stack<String>();
        stack.push("a");
        printStack(stack);
        stack.push("b");
        printStack(stack);
        stack.push("c");
        printStack(stack);

        stack.pop();
        printStack(stack);
        stack.pop();
        printStack(stack);
        stack.pop();
        printStack(stack);
    }

    private static void printStack(Stack<String> s){
        if(s.isEmpty())
            System.out.printIn("empty stack");
        else
            System.out.printf("% s TOP\n", s);
    }

}

The output I get is:

[a] TOP
[a, b] TOP
[a, b, c] TOP
[a, b] TOP
[a] TOP
empty stack

I want to achieve:

[a,b,c] TOP
[a,c] TOP
empty stack
+4
source share
6 answers

Although I would recommend you use a different data structure, such as linked listif you want to remove from the middle, because there is one for this linked lists. You should NOT use ALL stacks if you want to remove an item from the middle. But since the question asks the question, then you can introduce a method deleteFrom()as shown below.

: , , , .

[a] TOP
[a, b] TOP
[a, b, c] TOP

After calling deleteFrom(stack, "b")
[a, c] TOP

deleteFrom():

public static Stack<String> deleteFrom(Stack<String> stack, String obj) {
    if (stack.search(obj) == -1) {
        System.out.println("Element doesn't exists in stack.");
        return stack;
    }
    Stack<String> stack2 = new Stack<String>();
    while (stack.search(obj) != -1)
        stack2.push(stack.pop());
    stack2.pop();
    while (stack2.size() != 0)
        stack.push(stack2.pop());
    return stack;
}

:

import java.util.*;

public class HelloWorld {

    public static void main(String[] args) {

        Stack<String> stack = new Stack<String>();
        stack.push("a");
        printStack(stack);
        stack.push("b");
        printStack(stack);
        stack.push("c");
        printStack(stack);

        stack = deleteFrom(stack, "b");
        System.out.println("After calling deleteFrom(stack, \"b\")");
        printStack(stack);
    }

    private static void printStack(Stack<String> s) {
        if (s.isEmpty()) {
            System.out.println("empty stack");
        } else {
            System.out.printf("%s TOP\n", s);
        }
    }

    public static Stack<String> deleteFrom(Stack<String> stack, String obj) {
        if (stack.search(obj) == -1) {
            System.out.println("Element doesn't exists in stack.");
            return stack;
        }
        Stack<String> stack2 = new Stack<String>();
        while (stack.search(obj) != -1)
            stack2.push(stack.pop());
        stack2.pop();
        while (stack2.size() != 0)
            stack.push(stack2.pop());
        return stack;
    }

}
+2

, push pop. , - LIFO. , (POP) b, (POP) c. c.

: , , . !!:)

0

Stack, Stack. , , , > > . .

. , , LinkedList Stack. , LinkedList .

package com.tutorialspoint;

import java.util.*;

public class LinkedListDemo {

   public static void main(String[] args) {
       // create a LinkedList
       LinkedList list = new LinkedList();

       // add some elements
       list.add("A");
       list.add("B");
       list.add("C");

       // print the list
       System.out.println("LinkedList:" + list);
       System.out.println("size:" + list.size());

       // remove "B" Element from the list
       list.remove("B");

       // print the list
       System.out.println("LinkedList:" + list);
       System.out.println("size:" + list.size());
   }
}

LinkedList:[A, B, C]
size: 3
LinkedList:[A, C]
size: 2
0

b , , b, .

:

import java.util.*;
public class test_stack {
    public static void main (String[] args){

        // put a, b and c on the stack
        Stack<String> stack = new Stack<String>();
        stack.push("a");
        stack.push("b");
        stack.push("c");
        printStack(stack);

        // pop items off the stack until we reach b (and push non b items onto temporary stack)
        Stack<String> temporaryStack = new Stack<String>();
        String currentItem;
        do {
            currentItem = stack.pop();
            if(!currentItem.equals("b")) {
                temporaryStack.push(currentItem);
        } while(!currentItem.equals("b"));

        // push temporary stack back onto main stack
        while(!temporaryStack.isEmpty()) {
            stack.push(temporaryStack.pop());
        }
        printStack(stack);
    }

    private static void printStack(Stack<String> s){
        if(s.isEmpty())
            System.out.printIn("empty stack");
        else
            System.out.printf("% s TOP\n", s);
    }

}
0

Stack LinkedList , / , Stack

public class StackLL<E> {

    private LinkedList<E> list;

    public StackLL() {
        list = new LinkedList<>();
    }

    public void push(E e) {
        list.addFirst(e);
    }

    public E pop(E e) {
        Iterator<E> it = list.descendingIterator(); // list.iterator()
        while (it.hasNext()) {
            E ele = it.next();
            if (ele == e) {
                it.remove();
                break;
            }
        }
        return null;
    }

    public E pop() {
        return list.removeFirst();
    }

    public int size() {
        return list.size();
    }

    public void print() {
        System.out.println(list);
    }
}

- , , , pop , , else, null.

public static void main(String[] args) {
    StackLL<String> stack = new StackLL<>();
    stack.push("a");
    stack.push("b");
    stack.push("c");
    stack.print();
    stack.pop("b");
    stack.print();
    stack.pop("b");
}

[c, b, a]
[c, a]

mid, -, 2 O (1 ) .

0

, :

Stack<String> stack = new Stack<String>();
stack.push("a");
stack.push("b");
stack.push("c");

// Pop the elements from the original stack onto
// the temporary stack.
Stack<String> temp = new Stack<String>();
while (!stack.isEmpty()) {
    String top = stack.pop();
    if (top.equals("b")) {
        break;
    }
    temp.push(top);
}

// Pop the elements from the temporary stack
// back into the original stack. This preserves
// the original order.
while (!temp.isEmpty()) {
    stack.push(temp.pop());
}
-1
source

Source: https://habr.com/ru/post/1650609/


All Articles