Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7762979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:32:57+00:00 2026-06-01T14:32:57+00:00

#include <cstdlib> #include<iostream> using namespace std; template <typename T> class stack { public: T

  • 0
#include <cstdlib>
#include<iostream>
using namespace std;
template <typename T>
class stack
{
  public:
  T arr[100];
  int top;
  stack()
  {
    top=-1;
  }
  public:
  void push(T x)
  {
    if(top==99)
        cout<<"overflow";
    else
        arr[++top]=x;
  }
  public :
  T pop(void)
  {
    if(top==-1)
        return 0;
    else
        return arr[top--];
  }

};
int main(int argc, char** argv) 
{
  stack <int> s;
  int x;
  cout<<"\nEnter no : ";
  cin>>x;
  s.push(x);
  x=s.pop();
  cout<<"Element popped : "<<x;
  return 0;
 }

this is my C++ code for a stack class of variable data type. Its does basic function of pop,push. I want to convert this code to java generics . As i am not very much familiar with java generics . please help me

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:32:58+00:00Added an answer on June 1, 2026 at 2:32 pm

    
    import java.util.EmptyStackException;
    import java.util.Vector;

    public class Stack extends Vector{

     /**
     * Creates an empty Stack.
     */
    public Stack() {
    
    }
    
    
    public E pop() {
        E   obj;
        int len = size();
    
        if (len == 0)
            throw new EmptyStackException();
        obj = elementAt(len - 1);
        removeElementAt(len - 1);
        return obj;
    }
    
    
    public void push(E e) {
    
        if(e==null)
            throw new NullPointerException();
        addElement(e);
    
    }
    
    
    public int size() {
        return elementCount;
    }
    
    
    public static void main(String args[]){
    
    
    Stack<Integer>  integerStack= new Stack<Integer>();
    
    //(7,1,3,3,5,1,2,4,3)
    integerStack.push(7);
    integerStack.push(1);
    integerStack.push(3);
    integerStack.push(3);
    integerStack.push(5);
    integerStack.push(1);
    integerStack.push(2);
    integerStack.push(4);
    integerStack.push(3);
    
    
    System.out.println("STACK WITH INTEGER ELEMENTS");
    //Size
    System.out.println("Size of the Stack is : "+integerStack.size());
    
    
    
    // String Stack
    Stack<String>  stringStack= new Stack<String>();
    
    //("abc","def","acd","fgi","fth","lmn","zxy","cde","adr")
    stringStack.push("abc");
    stringStack.push("def");
    stringStack.push("acd");
    stringStack.push("fgi");
    stringStack.push("fth");
    stringStack.push("lmn");
    stringStack.push("zxy");
    stringStack.push("cde");
    stringStack.push("adr");
    
    
    System.out.println("STACK WITH STRING ELEMENTS");
    //Size
    System.out.println("Size of the Stack is : "+stringStack.size());
    
    
    
    //pop
    stringStack.pop();
    
    //Size after pop
    System.out.println("Size of the Stack After pop is : "+stringStack.size());
    

    }

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example code: #include <cstdlib> #include <iostream> using namespace std; class A { public: A(int
Consider the following program #include <iostream> #include<cstdlib> using namespace std; class E { public:
#include <cstdlib> #include <iostream> #include <string> #include <string.h> using namespace std; class Portfolio{ public
#include<iostream> #include<fstream> #include<cstdlib> #include<iomanip> using namespace std; int main() { ifstream in_stream; // reads
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; const int FILENAME_MAX=20; int main()
#include <iostream> #include <math.h> #include <cstdlib> using namespace std; void circle(int x, int y,
Here is my code: #include <cstdlib> #include <iostream> #include <cstring> using namespace std; int
The following code: #include <cstdlib> #include <iostream> using namespace std; int function(void) { static
This is a small program: #include <iostream> #include <cstdlib> using namespace std; int main()
I have convex hull algorithm: #include <cstdlib> #include <iostream> using namespace std; typedef int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.