import java.io.*;
class stack
{
int st[]=new int[50];
int top,item,max,i;
DataInputStream d=new DataInputStream(System.in);
stack()
{
top=0;
}
void maxsize()throws IOException
{
System.out.println("Stack Operation");
System.out.println("**********");
System.out.println("Enter the Maximum Size");
max=Integer.parseInt(d.readLine());
}
void push()throws IOException
{
if(top>=max)
{
System.out.println("Stack is Full");
}
else
{
System.out.println("Enter the item");
item=Integer.parseInt(d.readLine());
top=top+1;
st[top]=item;
}
}
void pop()
{
if(top<=0)
{
System.out.println("Stack is empty");
}
else
{
System.out.println("Pop Element:"+item);
to=top-1;
item=st[top];
}
}
void pri()
{
System.out.println("Stack Elements");
System.out.println("-------------");
for(int i=top;i>0;i--)
{
System.out.println(st[i]);
}
}
}
class stack1
{
public static void main(String args[])throws IOException
{
int i,c,ch;
DataInputStream d=new DataInputStream(System.in);
stack s=new stack();
s.maxsize();
do
{
System.out.println("Menu");
System.out.println("-----");
System.out.println("1.Push");
System.out.println("2.Pop");
System.out.println("3.Print");
System.out.println("4.Exit");
System.out.println("Enter your choice");
c=Integer.parseInt(d.readLine());
switch(c)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.pri();
break;
case 4:
System.out.println("Exit");
break;
}
}
while(c!=4)
}
}
-------------
OUTPUT:
*********
Stack Operation
*************
Enter the Maximum Size
3
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
1
Enter the item
4
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
1
Enter the item
5
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
1
Enter the item
3
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
3
Stack Elements
------------------
3
5
4
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
1
Stack is full
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
2
Pop Element:3
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
2
Pop Element:5
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
2
Pop Element:4
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
2
Stack is Empty
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
4
Exit
class stack
{
int st[]=new int[50];
int top,item,max,i;
DataInputStream d=new DataInputStream(System.in);
stack()
{
top=0;
}
void maxsize()throws IOException
{
System.out.println("Stack Operation");
System.out.println("**********");
System.out.println("Enter the Maximum Size");
max=Integer.parseInt(d.readLine());
}
void push()throws IOException
{
if(top>=max)
{
System.out.println("Stack is Full");
}
else
{
System.out.println("Enter the item");
item=Integer.parseInt(d.readLine());
top=top+1;
st[top]=item;
}
}
void pop()
{
if(top<=0)
{
System.out.println("Stack is empty");
}
else
{
System.out.println("Pop Element:"+item);
to=top-1;
item=st[top];
}
}
void pri()
{
System.out.println("Stack Elements");
System.out.println("-------------");
for(int i=top;i>0;i--)
{
System.out.println(st[i]);
}
}
}
class stack1
{
public static void main(String args[])throws IOException
{
int i,c,ch;
DataInputStream d=new DataInputStream(System.in);
stack s=new stack();
s.maxsize();
do
{
System.out.println("Menu");
System.out.println("-----");
System.out.println("1.Push");
System.out.println("2.Pop");
System.out.println("3.Print");
System.out.println("4.Exit");
System.out.println("Enter your choice");
c=Integer.parseInt(d.readLine());
switch(c)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.pri();
break;
case 4:
System.out.println("Exit");
break;
}
}
while(c!=4)
}
}
-------------
OUTPUT:
*********
Stack Operation
*************
Enter the Maximum Size
3
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
1
Enter the item
4
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
1
Enter the item
5
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
1
Enter the item
3
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
3
Stack Elements
------------------
3
5
4
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
1
Stack is full
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
2
Pop Element:3
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
2
Pop Element:5
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
2
Pop Element:4
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
2
Stack is Empty
Menu
-------
1.Push
2.Pop
3.Print
4.Exit
Enter your choice
4
Exit
0 comments:
Post a Comment