KEMBAR78
Copy Stack | PDF
0% found this document useful (0 votes)
11 views3 pages

Copy Stack

STACK EXAMPLE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Copy Stack

STACK EXAMPLE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

Scanner;

class copyStack{

int stackArray[], top, maxsize;

public copyStack(int size){

maxsize = size;

stackArray = new int[size];

top = -1;

public boolean isFull(){

return top == maxsize - 1;

public boolean isEmpty(){

return top == -1;

public void push(int num){

if (isFull())

System.out.println("The stack is full no more elements can be added");

else

stackArray[++top] = num;

public int pop(){

int temp = 0;

if (isEmpty())

System.out.println("The stack is empty no elements to remove");

else{

temp = stackArray[top];

top--;

}
return temp;

public void displayStack(){

for(int i = 0; i <= top; i++){

System.out.print(stackArray[i] + "\t");

System.out.println();

public static void main(String args[]){

copyStack s1 = new copyStack(5);

copyStack temp = new copyStack(5);

copyStack s2 = new copyStack(5);

s1.push(10);

s1.push(15);

s1.push(9);

s1.push(4);

s1.push(1);

s1.push(190);

System.out.println("The elements of stack 1:\n");

s1.displayStack();

temp.push(s1.pop());

temp.push(s1.pop());

temp.push(s1.pop());

temp.push(s1.pop());

temp.push(s1.pop());

System.out.println("\nThe elements of stack temp:\n");

temp.displayStack();
s2.push(temp.pop());

s2.push(temp.pop());

s2.push(temp.pop());

s2.push(temp.pop());

s2.push(temp.pop());

System.out.println("\nThe elements of stack 2:\n");

s2.displayStack();

You might also like