0% found this document useful (0 votes)
39 views4 pages

Interface Stack

This document implements stack data structures using both arrays and linked lists in Java. It defines stack interfaces with push, pop, isEmpty, and isFull methods. The StackArray class implements stacks using arrays, while StackLinked uses linked lists. A menu-driven StackImp class tests the push and pop functionality of both implementations.

Uploaded by

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

Interface Stack

This document implements stack data structures using both arrays and linked lists in Java. It defines stack interfaces with push, pop, isEmpty, and isFull methods. The StackArray class implements stacks using arrays, while StackLinked uses linked lists. A menu-driven StackImp class tests the push and pop functionality of both implementations.

Uploaded by

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

PANIMALAR ENGINEERING COLLEGE

DEPARTMENT OF CSE
REGNO: 211412104304
EX.NO: 04
DATE: 08/08/2014
IMPLEMENTATION OF STACK ADT
PROGRAM:
Inter!"e #t!"$
package stackimp;
public interface Stack
{
public void push();
public void pop();
public boolean isEmpty();
public boolean isFull();
public void display();
}
St!"$ %&'(e&ent# )#%n* !rr!+
package stackimp;
import javautil!;
public class stackarray implements Stack
{
int top;
int s"#$ne% int"&'#;
final int ma(;
Scanner in$ne% Scanner(Systemin);
stackarray()
{
top$)&; ma($*;
}
public void push()
{
if(isFull())
Systemoutprintln(+Stack is full,n+);
else
s"--top#$inne(t.nt();
}
public boolean isFull()
{
return(top$$(ma()&));
}
public void pop()
{
if(isEmpty())
Systemoutprintln(+,nStack Empty,n+);
else
Systemoutprint(+,n Element removed from stack is / + - s"top))#- +,n+);
}
public boolean isEmpty()
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REGNO: 211412104304
{
return(top$$)&);
}
public void display()
{
if(isEmpty())
Systemoutprintln(+,n Stack Empty,n+);
else
{
for(int i$top;i0$';i)))
Systemoutprint(+,t+-s"i#);
}
}
}
St!"$ I&'(e&ent!t%,n )#%n* L%n$e- L%#t
package stackimp;
import javautil!;
public class stacklinked implements Stack
{
1inked1ist top$ne% 1inked1ist();
Scanner in$ne% Scanner(Systemin);
public boolean isFull(){return true;}
public void push()
{
topaddFirst(inne(t.nt());
}
public void pop()
{
if (isEmpty())
Systemoutprintln(+,n Stack Empty,n+);
else
{
Systemoutprintln(+the element poped is + - topgetFirst());
topremoveFirst();
}
}
public boolean isEmpty()
{
return topisEmpty();
}
public void display()
{
if(isEmpty())
Systemoutprintln(+Stack Empty,n+);
else
Systemoutprintln(top);
}
}
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REGNO: 211412104304

M!%n 'r,*r!&
package stackimp;
import javautil!;
public class Stackimp
{
Scanner in $ ne% Scanner(Systemin);

static void menu(Stack stk)
{Scanner in $ ne% Scanner(Systemin);
int ch;
do
{
Systemoutprint(+,n S2345 67E832.69 +);
Systemoutprint(+,n &7:S; +);
Systemoutprint(+,n <767 +);
Systemoutprint(+,n =>.S713? +);
Systemoutprint(+,n,t Enter the choice /+);
ch$inne(t.nt();
s%itch(ch)
{
case &/ stkpush();
break;
case </stkpop();
break;
case =/ stkdisplay();
}
}%hile(ch@A);
}

public static void main(String"# args)
{Scanner in $ ne% Scanner(Systemin);
Stack stk;
int c;
do
{
Systemoutprint(+,n S2345 .B71EBE9232.69 +);
Systemoutprint(+,n &:S.9C 3883? +);
Systemoutprint(+,n <:S.9C 1.95E> 1.S2 +);
Systemoutprint(+,nSelect the option /+);
c$inne(t.nt();
s%itch(c)
{
case &/ stk$ne% stackarray();
menu(stk);
break;
case </ stk$ne% stacklinked();
menu(stk);
}
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REGNO: 211412104304
}%hile(c@=);
}
}
O)t')t
S2345 .B71EBE9232.69
&:S.9C 3883?
<:S.9C 1.95E> 1.S2
Select the option /&
S2345 67E832.69
&7:S;
<767
=>.S713?
Enter the choice /&
A
S2345 67E832.69
&7:S;
<767
=>.S713?
Enter the choice /=
A
S2345 .B71EBE9232.69
&:S.9C 3883?
<:S.9C 1.95E> 1.S2
Select the option /<
S2345 67E832.69
&7:S;
<767
=>.S713?
Enter the choice /&
A
S2345 67E832.69
&7:S;
<767
=>.S713?
Enter the choice /=
"A#
RES.LT:

You might also like