-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacklink.java
More file actions
54 lines (48 loc) · 1.02 KB
/
stacklink.java
File metadata and controls
54 lines (48 loc) · 1.02 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
public class stacklink {
class Node{
Node next;
int data;
Node(int data){
this.data=data;
this.next=null;
}
}
Node top=null;
void push(int data){
Node newnode=new Node(data);
newnode.next=top;
top=newnode;
}
void pop(){
if(top==null){
System.out.print("empty");
return;
}
top=top.next;
}
void traverse(){
if(top==null){
System.out.print("empty");
return;
}
Node temp=top;
while(temp!=null){
System.out.println(temp.data);
temp=temp.next;
}
}
public static void main(String[]args){
stacklink st=new stacklink();
st.push(10);
st.push(20);
st.push(30);
st.traverse();
st.pop();
st.traverse();
st.pop();
st.push(30);
st.traverse();
st.pop();
st.traverse();
}
}