ADT stack

ADT stack

Design a Java interface for ADT Stack. Develop two different classes that implement this interface, one using array and the other using linked-list. Provide necessary exception handling in both the implementations.
View Answers

August 12, 2010 at 10:29 AM

Hi Friend,

Try the following codes:

ADT Stack:

import java.lang.*;
import java.io.*;
import java.util.*;

interface MyInterface {
int n = 20;

public void pop();

public void push();

public void peek();

public void display();
}

class StackImplementation implements MyInterface {
int arr[] = new int[n];
int top = -1;

public void push() {
try {
DataInputStream dis = new DataInputStream(System.in);
System.out.println("Enter Element");
int ele = Integer.parseInt(dis.readLine());
arr[++top] = ele;
} catch (Exception e) {
System.out.println("e");
}
}

public void pop() {
int popper = arr[top];
top--;
System.out.println("popped element " + popper);
}

public void peek() {
int popper = arr[top];
System.out.println("popped element " + popper);
}

public void display() {
if (top < 0) {
System.out.println("Stack is empty");
return;
} else {
String str = " ";
for (int i = 0; i <= top; i++)
str = str + " " + arr[i];
System.out.println("Elements are " + str);
}
}
}

class StackADT {
public static void main(String arg[]) throws IOException {
DataInputStream dis = new DataInputStream(System.in);
StackImplementation stk = new StackImplementation();
int menu = 0;
do {
System.out.println("1.push \n2.pop \n3.peek \n4.display \n5.Exit");
System.out.println();
System.out.print("Enter your choice: ");
menu = Integer.parseInt(dis.readLine());
switch (menu) {
case 1:
stk.push();
break;
case 2:
stk.pop();
break;
case 3:
stk.peek();
break;
case 4:
stk.display();
break;
case 5:
System.exit(0);
}
} while (menu <= 5);
System.out.println();
}
}

Thanks









Related Tutorials/Questions & Answers:
ADT stack - Java Beginners
ADT stack  Design a Java interface for ADT Stack. Develop two....  Hi Friend, Try the following codes: ADT Stack: import...("Stack is empty"); return; } else { String str = " "; for (int i = 0; i <= top
ADT stack - Java Beginners
]. 2)Design a Java interface for ADT Stack. Develop two different classes...ADT stack  I want a algorithm for the below programs.... 1)Implement Lisp-like list in Java. Write basic operations such as 'car', 'cdr', and 'cons
Advertisements
Java Stack ADT
Java Stack ADT You all are familiar with the data structure 'Stack'. The Stack ADT is same as Stack. It is a LIFO data structure where insertions and deletions always occur at the same end. This end is called the top of the stack
array ADT
array ADT  Write a program using array_ADT to retrieve a list of elements from a file
array ADT
array ADT  Write a program using array_ADT to retrieve a list of elements from a file
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
array ADT
array ADT  Write a program using array_ADT to retrieve a list of URLs from a file. In your program take an array index as the input and delete the entry corresponding to that index
ModuleNotFoundError: No module named 'adt'
ModuleNotFoundError: No module named 'adt'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'adt' How to remove the ModuleNotFoundError: No module named 'adt' error
ModuleNotFoundError: No module named 'adt-extension'
ModuleNotFoundError: No module named 'adt-extension'  Hi, My... 'adt-extension' How to remove the ModuleNotFoundError: No module named 'adt-extension' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'adt-package'
ModuleNotFoundError: No module named 'adt-package'  Hi, My Python... 'adt-package' How to remove the ModuleNotFoundError: No module named 'adt-package' error? Thanks   Hi, In your python environment
Stack
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 we placed or push on stack is the first thing we can get when we pop. A stack
Define stack
Define stack  hii, Explain stack
stack in java/
stack in java/  java code for stack push pop and display operation? thanks
ADT ,data structure (ArrayList), sorting
ADT ,data structure (ArrayList), sorting  Write a program to calculate a bonus for 10 employees of WAFA Supermarket. The program consists of an abstract class Employee. Each employee contains IC number, name, basicSalary
Define stack
Define stack  hii, Explain stack?   hello,ADS_TO_REPLACE_1 Stack is an ordered list in which all the insertions and deletions made at one end called top of the stack. It is referred as LIFO list. Last inserted item
ARRAY and STACK
and STACK?   hii,ADS_TO_REPLACE_1 STACK follows LIFO. Thus the item... or removed in any order.   a stack is simply a special case of an array. You could say that a stack uses user defined methods to remove and add contents
Array stack
Array stack  Write a stack class ArrayStack.java implements PureStack interface that reads in strings from standard input and prints them in reverse order
Array stack
Array stack  Write a stack class ArrayStack.java implements PureStack interface that reads in strings from standard input and prints them in reverse order
Java Stack
Java Stack       The Stack class works on the principle last-in-first-out (LIFO) stack of objects... a vector to be treated as a stack. Methods push() and pop() are used to pick
Stack and Queue
Stack and Queue  Consider a class called Configuration. This class ââ?¬Å?encapsulatesââ?¬Â? the configuration management for any software system... or a Stack. Your App class is given and you should not change this class at all. Your
What is a stack overflow error?
What is a stack overflow error?  What is a stack overflow error
Stack Overflow - Java Tutorials
Stack Overflow in Java A stack is the part of the memory. The local automatic variable is created on this stack and method arguments are passed. When a process starts, it get a default stack size which is fixed for each process. 
What is the flaw with the Stack class?
inheritance rather than composition and does not completely follow the Stack ADT (abstract data type). Stack defines methods such as push(), pop(), and peek(). Being...There is a flaw in the current stack class and it has become a matter
java technologies stack
java technologies stack  java technologies stack
stack using linked list
stack using linked list  how to implement stack using linked list
ModuleNotFoundError: No module named 'stack'
ModuleNotFoundError: No module named 'stack'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'stack' How to remove the ModuleNotFoundError: No module named 'stack'
heap and stack memory
heap and stack memory   hi i am jane pls explain the difference between heap memory and stack memory with example programs
Java collection Stack example
Java collection Stack example  How to use Stack class in java collection?   The Stack class uses the First In Last Out(FILO) mechanism. Here is an example of Stack class. import java.util.Stack; public class
can Stack accept imageIcon ..??
can Stack accept imageIcon ..??  how can write program like album ..by using Stack as an ideas datastracture and using imageIconb ?? please help me ..thanks alot
remove element from stack
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
Web service protocol stack
Web service protocol stack  hii, What is the Web service protocol stack?   hello,ADS_TO_REPLACE_1 The Web service protocol stack is an evolving set of protocols used to define, discover, and implement Web services
stack and queue - Java Beginners
stack and queue  write two different program in java 1.) stack 2.) queue   Hi Friend, Please visit the following links: http://www.roseindia.net/java/example/java/util/StackImplement.shtml http
Java stack help
Java stack help  How would I use stacks in java to do a calculation like: 4*((3+3)/(5-3
Java Stack - Java Beginners
Java Stack  Can you give me a code using Java String STACK using the parenthesis symbol ( ) the user will be the one to input parenthesis......) if Open parenthesis push it to stack otherwise Pop the stack Thank you
stack - Java Beginners
that inputs a line of text and uses a stack object to print the words of the line... : import java.io.*; import java.util.*; public class StackExample{ Stack stack...(){ try{ stack = new Stack(); InputStreamReader ir = new InputStreamReader
heap and stack memory
heap and stack memory   hi i am jane pls explain the difference between heap memory and stack memory   STACK memory is referred as temporary memory,if you come out of the program the memory
Java Stack Example
Java Stack Example In this section you will learn about Stack class  in java, how to use Stack in java. Before going into details we should know what is stack class, stack  class represent Last In First Out(LIFO) of object
Collection : Stack Example
Collection : Stack Example In this tutorial we will describe implementation of stack with example. Stack : Stack is based on concept of LIFO(Last In First... one access point that is top of stack. There are two main methods of stack
Generic Java Stack
- package jp.co.worksap.intern; /** *The Stack class represents a last-in-first-out(LIFO) stack of objects *And this class can look at the object which has the highest (lowest or so) value. *So every object onto the stack
Is Python full stack?
Is Python full stack?  Hi, I am beginner in Data Science and machine... stack? Try to provide me good examples or tutorials links so that I can learn the topic "Is Python full stack?". Also tell me which is the good
{create stack with queue}
{create stack with queue}  how can create queue that accept all data type ?? meanse that this array is used accept all type for example(int ,char,String,float).?? please help me ....which output is : 1- to create queue 2-Equeue
ModuleNotFoundError: No module named 'alchemist-stack'
ModuleNotFoundError: No module named 'alchemist-stack'  Hi, My... named 'alchemist-stack' How to remove the ModuleNotFoundError: No module named 'alchemist-stack' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'alchemist-stack'
ModuleNotFoundError: No module named 'alchemist-stack'  Hi, My... named 'alchemist-stack' How to remove the ModuleNotFoundError: No module named 'alchemist-stack' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'alchemist-stack'
ModuleNotFoundError: No module named 'alchemist-stack'  Hi, My... named 'alchemist-stack' How to remove the ModuleNotFoundError: No module named 'alchemist-stack' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'beanstalk-stack'
ModuleNotFoundError: No module named 'beanstalk-stack'  Hi, My... named 'beanstalk-stack' How to remove the ModuleNotFoundError: No module named 'beanstalk-stack' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-stack-it'
ModuleNotFoundError: No module named 'django-stack-it'  Hi, My... named 'django-stack-it' How to remove the ModuleNotFoundError: No module named 'django-stack-it' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'focus-stack'
ModuleNotFoundError: No module named 'focus-stack'  Hi, My Python... 'focus-stack' How to remove the ModuleNotFoundError: No module named 'focus-stack' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'hmsdbmitc-stack'
ModuleNotFoundError: No module named 'hmsdbmitc-stack'  Hi, My... named 'hmsdbmitc-stack' How to remove the ModuleNotFoundError: No module named 'hmsdbmitc-stack' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'hmsdbmitc-stack'
ModuleNotFoundError: No module named 'hmsdbmitc-stack'  Hi, My... named 'hmsdbmitc-stack' How to remove the ModuleNotFoundError: No module named 'hmsdbmitc-stack' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'py_stack'
ModuleNotFoundError: No module named 'py_stack'  Hi, My Python..._stack' How to remove the ModuleNotFoundError: No module named 'py_stack... to install padas library. You can install py_stack python with following

Ads