-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInterfaceTest.java
More file actions
75 lines (73 loc) · 2.09 KB
/
InterfaceTest.java
File metadata and controls
75 lines (73 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
public interface InterfaceTest{
int pop();
void push(int s);
void print();
};
class StackImp implements InterfaceTest{
int a[]=new int[10];
int st_top;
StackImp(int x){
st_top=-1;
a=new int[x];
}
public void push(int num){
System.out.println("running StackImp push \n");
if(st_top==9) System.out.println("stack full");
else a[++st_top]=num;
}
public int pop(){
System.out.println("running StackImp pop \n");
if(st_top<0){
System.out.println("StackImp stack is empty");
return 0;
}
else return a[st_top--];
}
public void print(){
System.out.println("this is print function for StackImp..\n");
for(int i=0;i<10;i++) System.out.println(a[i]);
}
}
class GrowStack implements InterfaceTest{
int a[];
int top;
GrowStack(int x){
top=-1;
a=new int[x];
}
public int pop(){
System.out.println("running GrowStack pop \n");
if(top<0){
System.out.println("GrowStack stack is empty");
return 0;
}
else return a[top--];
}
public void push(int num){
System.out.println("running GrowStack push \n");
if(top==a.length-1){
int temp[]=new int[a.length*2];
for(int i=0;i<a.length;i++) temp[i]=a[i];
a=temp;
a[++top]=num;
}
else a[++top]=num;
}
public void print(){
System.out.println("this is print function for GrowStack..\n");
for(int i=0;i<10;i++) System.out.println(a[i]);
}
}
class MyStack{
public static void main(String args[]){
InterfaceTest Test;
StackImp si=new StackImp(5);
GrowStack gs= new GrowStack(5);
Test=gs;
for(int i=0;i<10;i++) Test.push(i);
for(int i=0;i<10;i++) System.out.println(Test.pop());
Test=si;
for(int i=0;i<5;i++) Test.push(i);
for(int i=0;i<5;i++) System.out.println(Test.pop());
}
}