forked from krishna14kant/Data-Structures-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkstacktest.java
More file actions
92 lines (91 loc) · 1.57 KB
/
linkstacktest.java
File metadata and controls
92 lines (91 loc) · 1.57 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
class Node
{
protected int data;
protected Node link;
public Node()
{
link=null;
data=0;
}
public Node(int d,Node n)
{
data=d;
link=n;
}
public void setlink(Node n)
{
link=n;
}
public void setdata(int d)
{
data=d;
}
public Node getlink()
{
return link;
}
public int getdata()
{
return data;
}
}
class linkedstack
{
protected Node top;
protected int number;
public linkedstack()
{
top=null;
number=0;
}
public boolean isempty()
{
return top==null;
}
public int size()
{
return number;
}
public void push(int obj)
{
top=new Node(obj,top);
number++;
}
public int pop()
{
if(isempty())
return 0;
Node tmp=top;
top=tmp.getlink();
number--;
return tmp.getdata();
}
public int peep()
{
if(isempty())
return 0;
return top.getdata();
}
}
public class linkstacktest
{
protected static linkedstack s;
public static void main(String args[])
{
int i;
s=new linkedstack();
for(int a=0;a<5;a++)
{
i=(int)(Math.random()*100);
s.push(i);
System.out.println("Pushed :"+i);
}
int pp=s.peep();
System.out.println("At top "+ pp);
System.out.println();
while(!s.isempty())
{
System.out.println("Popped :"+s.pop());
}
}
}