Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Interview-Questions remove element from stack
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


mavic
remove element from stack
1 Answer(s)      3 years ago
Posted in : Java Interview Questions


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);
    }

} }


Advertisement
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... code, we can remove a child of given element. The DOMBuilder class is used
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
will demonstrate you about how to delete an element from array. To remove the array...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
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.
) The removeAttribute(...) method remove attribute from existing element. Code... 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 w1.w3 is a subset
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 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
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
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
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
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
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
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
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 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
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... Tool (S key) and adjust the setting to remove glass. Remove : Hold Alt key
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
 

 

 

DMCA.com