Вы находитесь на странице: 1из 3

import java.io.

*;
import java.util.Scanner;
interface stackimpl
{
int maxn=100;
void push(int i);
int pop();
void display();
}
class stackarray implements stackimpl
{
int stack[]=new int[100];
int top=-1,i,j,n;
Scanner s=new Scanner(System.in);
public void push(int item)
{
if(top<maxn)
{
top++;
stack[top]=item;
}
else
{
System.out.println("Stack is full");
}
}
public int pop()
{
if(top>=0)
{
int item=stack[top];
top--;
return item;
}
else
{
System.out.println("Stack is empty");
return 0;
}
}
public void display()
{
if(top>=0)
{
for(i=top;i>=0;i--)
{
System.out.println("The elements are:"+stack[i])
;
}
}
else
{
System.out.println("The stack is empty");
}
}
}
class instack implements stackimpl
{
private node head;
private class node
{
int item;
node next;
node(int item,node next)
{
this.item=item;
this.next=next;
}
public int getdata()
{
return(item);
}
}
void stack(int maxn)
{
head=null;
}
boolean isempty()
{
return(head==null);
}
public void push(int item)
{
head=new node(item,head);
}
public int pop()
{
int v=head.item;
node t=head.next;
head=t;
return v;
}
public void display()
{
node current=head;
while(current!=null)
{
System.out.println(current.getdata());
current=current.next;
}
System.out.println(" ");
}
}
public class mainstack
{
public static void main(String args[])
{
stackarray s1=new stackarray();
instack s2=new instack();
int top,ch,item,ch1,ch2;
int i,n;
Scanner s=new Scanner(System.in);
System.out.println("1.Array 2.Linked List");
ch1=s.nextInt();
switch(ch1)
{
case 1:
do
{
System.out.println("1.push 2.pop 4.display");
ch=s.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter the element to
insert");
item=s.nextInt();
s1.push(item);
s1.display();
break;
case 2:
s1.pop();
s1.display();
break;
case 3:
System.out.println("The stack display");
s1.display();
break;
}
}while(true);
case 2:
do
{
System.out.println("1.push 2.pop 3.display");
ch2=s.nextInt();
switch(ch2)
{
case 1:
System.out.println("Enter the item to in
sert");
item=s.nextInt();
s2.push(item);
break;
case 2:
System.out.println("The deleted element
"+s2.pop());
break;
case 3:
s2.display();
break;
}
}while(true);
}
}
}

Вам также может понравиться