Program:
import java.io.*;
interface stackoperation
{
public void push(int i);
public void pop();
}
class Astack implements stackoperation
{
int stack[];
int top;
Astack()
{
stack=new int[10];
top=0;
}
public void push(int item)
{
if(stack[top]==10)
System.out.println("overflow");
else
{
stack[++top]=item;
System.out.println("item pushed");
}
}
public void pop()
{
if(stack[top]<=0)
System.out.println("underflow");
else
{
stack[top]=top--;
System.out.println("item popped");
}
}
public void display()
{
for(int i=1;i<=top;i++)
System.out.println("element:"+stack[i]);
}
}
class liststack implements stackoperation
{
node top,q;
int count;
public void push(int i)
{
node n=new node(i);
n.link=top;
top=n;
count++;
}
public void pop()
{
if(top==null)
System.out.println("under flow");
else
{
int p=top.data;
top=top.link;
count--;
System.out.println("popped element:"+p);
}
}
void display()
{
for(q=top;q!=null;q=q.link)
{
System.out.println("the elements are:"+q.data);
}
}
class node
{
int data;
node link;
node(int i)
{
data=i;
link=null;
}
}
}
class sample
{
public static void main(String args[])throws IOException
{
int ch,x=1,p=0,t=0;
DataInputStream in=new DataInputStream(System.in);
do
{
try
{
System.out.println("----------------------------------");
System.out.println("1.Arraystack 2.liststack 3.exit");
System.out.println("-----------------------------------");
System.out.println("enter ur choice:");
int c=Integer.parseInt(in.readLine());
Astack s=new Astack();
switch(c)
{
case 1:
do
{
if(p==1)
break;
System.out.println("ARRAY STACK");
System.out.println("1.push 2.pop 3.display 4.exit");
System.out.println("enter ur choice:");
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:System.out.println("enter the value to push:");
int i=Integer.parseInt(in.readLine());
s.push(i);
break;
case 2:
s.pop();
break;
case 3:
System.out.println("the elements are:");
s.display();
break;
case 4:
p=1;
continue;
}
}while(x!=0);
break;
case 2:
liststack l=new liststack();
do
{
if(t==1)
break;
System.out.println("LIST STACK:");
System.out.println("1.push 2.pop 3.display 4.exit");
System.out.println("enter your choice:");
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
System.out.println("enter the value for push:");
int a=Integer.parseInt(in.readLine());
l.push(a);
break;
case 2:
l.pop();
break;
case 3:
l.display();
break;
case 4:
t=1;
continue;
}
}
while(x!=0);
break;
case 3:
System.exit(0);
}
}
catch(IOException e)
{
System.out.println("io error");
}
}
while(x!=0);
}
}
Output:
C:\j2sdk1.4.0\bin>javac sample.java
Note: sample.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\j2sdk1.4.0\bin>java sample
----------------------------------
1.Arraystack 2.liststack 3.exit
-----------------------------------
enter ur choice:
1
ARRAY STACK
1.push 2.pop 3.display 4.exit
enter ur choice:
1
enter the value to push:
1
item pushed
ARRAY STACK
1.push 2.pop 3.display 4.exit
enter ur choice:
1
enter the value to push:
2
item pushed
ARRAY STACK
1.push 2.pop 3.display 4.exit
enter ur choice:
1
enter the value to push:
3
item pushed
ARRAY STACK
1.push 2.pop 3.display 4.exit
enter ur choice:
3
the elements are:
element:1
element:2
element:3
ARRAY STACK
1.push 2.pop 3.display 4.exit
enter ur choice:
2
item popped
ARRAY STACK
1.push 2.pop 3.display 4.exit
enter ur choice:
3
the elements are:
element:1
element:2
ARRAY STACK
1.push 2.pop 3.display 4.exit
enter ur choice:
4
----------------------------------
1.Arraystack 2.liststack 3.exit
-----------------------------------
enter ur choice:
2
LIST STACK:
1.push 2.pop 3.display 4.exit
enter your choice:
1
enter the value for push:
1
LIST STACK:
1.push 2.pop 3.display 4.exit
enter your choice:
1
enter the value for push:
2
LIST STACK:
1.push 2.pop 3.display 4.exit
enter your choice:
1
enter the value for push:
3
LIST STACK:
1.push 2.pop 3.display 4.exit
enter your choice:
3
the elements are:3
the elements are:2
the elements are:1
LIST STACK:
1.push 2.pop 3.display 4.exit
enter your choice:
2
popped element:3
LIST STACK:
1.push 2.pop 3.display 4.exit
enter your choice:
3
the elements are:2
the elements are:1
LIST STACK:
1.push 2.pop 3.display 4.exit
enter your choice:
4
----------------------------------
1.Arraystack 2.liststack 3.exit
-----------------------------------
enter ur choice:
3
No comments:
Post a Comment