remove element from stack

remove element from stack

Hi there

I'm looking around on the internet how to remove names from stack by asking user how many names they want to remove and if the number they enter is bigger than the stack number then show error message and if not then remove the names from stack until the stack is empty the exit.

asking for your idea and here is my code

can you please show me how to do it..look forward for your reply

import java.util.*;

public class Stack extends ArrayList<String> {

public Object peek()
{
    return get(size() - 1);
}
public Object pop(String s)
{
    return remove(size() - 1);
}
public String push(Object obj)
{
    add(obj.toString());
    return obj.toString();
}
public  int search(Object obj)
{
    return lastIndexOf(obj);
}

public static void main(String[] args)
{
    int d = 5;
    String name;

    Stack stringSS = new Stack();
    Scanner scan = new Scanner(System.in);
    for(int i = 1; i <= 5; ++i)
    {
        System.out.println("Stack contains: " + stringSS.size() + " elements");
        System.out.print("Enter string #" + i + ": ");
        stringSS.push(scan.nextLine());
    }
    System.out.println("\nDisplay Stack:  " + "\n" + stringSS);

    //System.out.println("\nEnter number of employees to be dismissed: ");
    Scanner scan2 = new Scanner(System.in);
    for(int i = 1; i <= 5; ++i)
    {
        System.out.println("Stack contains: " + stringSS.size() + " elements");
        System.out.print("Enter string #" + i + ": ");
        stringSS.pop(scan2.nextLine());
        System.out.println("\nDisplay Stack:  " + "\n" + stringSS);
    }

} }

View Answers

April 2, 2012 at 5:39 PM

import java.util.*;

public class Stack extends ArrayList<String> {

public Object peek()
{
    return get(size() - 1);
}
public Object pop()
{
    return remove(size() - 1);
}
public String push(Object obj)
{
    add(obj.toString());
    return obj.toString();
}
public  int search(Object obj)
{
    return lastIndexOf(obj);
}

public static void main(String[] args){
    int d = 5;
    String name;

    Stack stringSS = new Stack();
    Scanner scan = new Scanner(System.in);
    for(int i = 1; i <= 5; ++i){
        System.out.println("Stack contains: " + stringSS.size() + " elements");
        System.out.print("Enter string #" + i + ": ");
        stringSS.push(scan.nextLine());
    }
    System.out.println("\nDisplay Stack:  " + "\n" + stringSS);

    System.out.println("\nEnter number of employees to be dismissed: ");
    int num=scan.nextInt();
    if(num>d) {
        System.out.println("Number you have entered is bigger than the stack size!");
    }
    else{
    for(int i = 1; i <= num; ++i){
        System.out.println("Stack contains: " + stringSS.size() + " elements");
        stringSS.pop();
        System.out.println("\nDisplay Stack:  " + "\n" + stringSS);
      }
    }
  }
}









Related Tutorials/Questions & Answers:
remove element from stack
remove element from stack  Hi there I'm looking around on the internet how to remove names from stack by asking user how many names they want... error message and if not then remove the names from stack until the stack is empty
How to remove specific element from Hashset.
How to remove specific element from Hashset. In this  exmple, you will see how to remove specific element from hashset. The HashSet class provides a method called remove. It removes a elements from set. Code: 
Advertisements
How to remove all element from Hashset.
How to remove all element from Hashset. HashSet is set type Collection...; it .In this example, you will see the use of clear method of HashSet. It is used to remove all element of hashset. Code:  HashSetRemoveElement.java
Remove Element from XML Document
Remove Element from XML Document       In this section, you will learn to remove any element from a given  XML document. Whenever you remove the xml element from the xml document
write a program to remove the element from hashset using keyboard.
write a program to remove the element from hashset using keyboard.  write a program to remove the element from hashset using keyboard
remove parent element JavaScript
remove parent element JavaScript  How to remove parent element in JavaScript
Push and Pop operation of stack using linked list.
;,&n); } while(n == 1); printf("Enter 1 to pop an element from stack... operation of stack using linked list. In the previous tutorial the stack...) { struct node *temp; if(p==NULL) { printf(" The stack is empty and cannot
JDOM Element Example, How to remove child of an element from xml file in java.
JDOM Element Example, How to remove child of an element from xml file in java. In this tutorial, we will see how to remove child of an element from xml file...(...) method remove child    element of given name from existing
JavaScript Remove Element
JavaScript Remove Element...; In this section, you will learn how to remove HTML element using JavaScript. JavaScript provides several methods which allow the user to remove particular HTML element
JDOM Element Example, How to add and remove child from xml file.
JDOM Element Example, How to add and remove child from xml file. In this tutorial, we will see how to add and remove child node in an xml Document tree... a child Element. The addContent(Content child) method of Element class adds
How To Remove Array Element In Java
How To Remove Array Element In Java In this section we will discuss about how to remove a given array element. In this section we will discuss about removing an integer array element. We will discuss about to remove a specific position
JavaScript Array Remove Element
JavaScript Array Remove Element In this example we will describe JavaScript Array Remove Element. First of all we have created an array list and store... Function. In this example we have used remove() method to remove element
JDOM Element Example, How to remove attribute of xml element in java.
name) The removeAttribute(...) method remove attribute from existing element... JDOM Element Example, How to remove attribute of xml element in java. In this tutorial, we will see how to remove attribute of xml element in java
remove a substring from a string
remove a substring from a string  Please I jave an arraylist composed as follwoing [w1, w2, w3, w4, w1.w2.w3, w2.w3.w4, w3.w4, w2.w4, w1.w3, w1.w2] w2.w4 is a subset of w2.w3.w4 ?how I recognize it also ADS_TO_REPLACE_1 w1.w3
remove comma from string php
remove comma from string php  How to remove the last comma from the string in PHP
remove comma from string php
remove comma from string php  How to remove the last comma from the string in PHP?   remove last comma from string names = names.replaceAll(",$", ""); //Or a simple substring: if (names.endsWith(",")) { names
remove from superview iphone sdk
remove from superview iphone sdk  How to remove the old view from super-view and add the new view safely? My main concern is memory leak.. so, please suggest how can i do it without facing a memory leak. Thanks
remove punctuation from string in java
remove punctuation from string in java  Hi, I want to remove punctuation in text from Java program. How to remove punctuation from string in java? Thanks   Hi, You can use the regular expression "\p{Punct
remove all objects from NSMutablearray
remove all objects from NSMutablearray  Hi, How to remove all objects from nsmutablearray? Thanks   HI, Here is the code for creating NSMutablearray, adding objects and then removing all the objects: NSMutableArray
Find a missing element from array
Find a missing element from array We have two arrays A and B where B consists of all the elements that are present in A but one element is missing from the array B. From these two arrays, we have to find the missing element from
Remove duplicates from ArrayList
Remove duplicates from ArrayList Sometimes we added large amount of data to ArrayList  where some values will get added twice or thrice which may conflict. So It is better to remove them. The easiest way to remove repeated elements
Pick random element from Set
Pick random element from Set A Set is a collection that contains no duplicate... the mathematical set abstraction. Here, we are going to pick an element randomly from... to the collection. An object of Random class is created to retrieve any element from the Set
Stack
Stack  How to implement a stack using classes in java?   Here is an example that implements a Stack class based on arrays. public class Stack { private int top; private int[] storage; Stack(int
Java Stack Example
the element to stack, pop to remove the element from stack, peek to look... the item in the stack and how far it is from top of stack Example : A Java code.... System.out.println(st.pop());//Remove the item on top of the stack
How to Remove audio from video file with FFmpeg?
How to Remove audio from video file with FFmpeg?  I have a video test.mp4 and I want to remove the audion from the video. So, that there is no audion in video. How to remove audio from video file with FFmpeg? thanks   
How to Remove Repeated Characters from the String?
How to Remove Repeated Characters from the String?  Hi, I trying to develop an application which will remove the repeated characters from... the example of how to remove repeated characters from the string. Please visit
Need to Remove Duplicate Records from Excel Sheet
Need to Remove Duplicate Records from Excel Sheet  Need to Remove Duplicate Records from Excel Sheet. I have one excel sheet having two fields... empnum rating (without using sql query have to remove records from excel using java
Java Remove a character from string
Java Remove a character from string In this tutorial, you will learn how to remove a character from the string. There are several methods for examining... to remove a particular character from the string. In the given example, we have
Remove duplicate characters from the string
Remove duplicate characters from the string In this tutorial, you will learn how to remove duplicate characters from the string. Java has provide several.... Here we are going to remove duplicate characters from the string
Remove Repeated Characters from the String
Remove Repeated Characters from the String In this we are going to remove repeated characters from the string. For this, we have allowed the user to enter the string. The class BufferedReader reads the string from the console. Then we
how to remove the column from a java web page
how to remove the column from a java web page  i have a web page with account#, qtr, year if i want to remove the year column which is a drop down list from my jsp what should i do and what is the process please give a brief view
How to remove paragraph from ms word
How to remove paragraph from ms word  Hi! I am using MS Word to edit my text but somehow i get paragraph sign on every line of my text. So, can any one please tell me how to remove paragraph from ms word?   CTRL+SHIFT+8
Neo4j - How to to remove all data from neo4j?
Neo4j - How to to remove all data from neo4j?  Hi, I am learning... to delete all nodes, links and properties from a Neo4j database? Thanks  ...]-() DELETE n,r This query will delete all the data (nodes, links, properties) from
remove duplicates from list in java using set
remove duplicates from list in java using set  Hi, I have a list... example: Remove duplicates from ArrayList Thanks... this? Tell me program for the removal of duplicates from list in java using set
How to remove virtual keypad from apps java
How to remove virtual keypad from apps java  I have an galaxy smart s6830 touch Chinese Mobile (240x320) (myriad java platform) (esmertec jbed) and I need to remove keypad from apps . I have put the following into the jar file
How to remove virtual keypad from apps java
How to remove virtual keypad from apps java  I have an galaxy smart s6830 touch Chinese Mobile (240x320) (myriad java platform) (esmertec jbed) and I need to remove keypad from apps . I have put the following into the jar file
How to remove virtual keypad from apps java
How to remove virtual keypad from apps java  I have an galaxy smart s6830 touch Chinese Mobile (240x320) (myriad java platform) (esmertec jbed) and I need to remove keypad from apps . I have put the following into the jar file
How to remove glass from the face
How to remove glass from the face By this example you will be able to remove any object from every place but. I have tried to remove a goggle from... : Select Clone Stamp Tool (S key) and adjust the setting to remove glass. Remove
Java Stack Example
is the operation in which the top element is retrieved without removing it from stack...")); // remove top item of the stack stack.pop();//remove 'Example' from... and there is no more element is to be removed i.e. the stack is empty. In Java Stack
Photoshop Tutorial : How to remove an object from a picture
How to remove an object from a picture       We are going to learn the method to remove an object from a picture. If you want to remove an object, which you don't want to like
Stack
operation is done at one end and whatever element we keep on stack is shown...Stack Overview Stack follows the rule of last in first out rule. Mainly two action are performed by stack one is push and other is pop. The last thing which
Find Array element from index using GUI
Find Array element from index using GUI In this tutorial, you will learn how to create an GUI application to find an array element from its index... to display the element at that specific index. A button 'Show Element'
Java collection Stack example
the element at the top of the Stack, and pop(): The pop() method remove...()); System.out.println("The element poped out of the stack :- " + stack.pop()); System.out.println("The element in a stack after pop out an element- : " +stack
Collection : Stack Example
element from stack : " + stack.pop()); System.out.println("Pop element... : 3 Elements of Stack : [50, hello, world] Pop element from stack : world Pop element from stack : hello Size of stack : 1
Need to remove duplicated rows from excel using apache POI API
Need to remove duplicated rows from excel using apache POI API  Hi, Need help from you. I am able to generate excel sheet with data also... = window.adsbygoogle || []).push({}); So can any one suggest me how to remove
remove item from list box using java script - Java Beginners
remove item from list box using java script  remove item from list box using java script  Hi friend, Code to remove list box item using java script : Add or Remove Options in Javascript function addItem
Remove checked checkboxes from arraylist and dipaly remaining arraylist
Remove checked checkboxes from arraylist and dipaly remaining arraylist  I want to delete checked checkboxes after submitting button. And i want... facing proble in deleting them from arraylist and display arrylist. Please
Value Stack / OGNL
. Every element is queried for the value till it is found or stack ends...Value Stack / OGNL Object Graph Navigational Language (OGNL) is a open source framework used to get properties from Java Beans. In Struts 2 framework, OGNL
What is the flaw with the Stack class?
method to remove a specified element from a Stack object without regard...There is a flaw in the current stack class and it has become a matter... inheritance rather than composition and does not completely follow the Stack ADT
JavaScript array remove
that will remove the specified element according to the user's choice. So we can use the pop() method to remove the element of the array. We can create different methods that will remove the specified element by using the pop() method. Here

Ads