-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathds.java
More file actions
103 lines (96 loc) · 1.31 KB
/
ds.java
File metadata and controls
103 lines (96 loc) · 1.31 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
interface ds
{
void ins(int x);
int del();
}
class stack implements ds
{
int a[],top;
stack(int size)
{
a=new int[size];
top=-1;
}
@Override
public void ins(int x)
{
if(top==a.length-1)
{
System.out.println("Stack Overflow !");
return;
}
a[++top]=x;
}
@Override
public int del()
{
if(top==-1)
{
System.out.println("Stack Underflow !");
return -99;
}
return a[top--];
}
}
class queue implements ds
{
int a[],front,rear,n;
queue(int size)
{
a=new int[size];
front=rear=-1;
}
@Override
public void ins(int x)
{
if(n==a.length)
{
System.out.println("Queue full !");
return;
}
rear=(rear+1)%a.length;
a[rear]=x;
n++;
}
@Override
public int del()
{
if(n==0)
{
System.out.println("Queue empty!");
return -99;
}
front=(front+1)%a.length;
n--;
return a[front];
}
}
class Main
{
public static void main(String []args)
{
int n;
stack st=new stack(5);
queue q=new queue(10);
for(int i=10;i<=60;i+=10)
{
st.ins(i);
System.out.println("Item to push - "+i);
}
do
{
System.out.println("Item popped - "+(n=st.del()));
}
while(n!=-99);
for(int i=0;i<=100;i+=10)
{
q.ins(i);
System.out.println("Item to insert - "+i);
}
do
{
System.out.println("Item deleted - "+(n=q.del()));
}
while(n!=-99);
}
}