-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathStack_LL.java
More file actions
50 lines (49 loc) · 712 Bytes
/
Stack_LL.java
File metadata and controls
50 lines (49 loc) · 712 Bytes
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
public class Stack_LL
{
class Node
{
Node next;
int data;
public Node(int d)
{
next=null;
data=d;
}
}
Node head;
public void push(int d)
{
Node newNode=new Node(d);
newNode.next=head;
head=newNode;
}
public void display()
{
Node n=head;
while(n!=null)
{
System.out.println(n.data);
n=n.next;
}
}
public int pop()
{
Node temp=head;
head=head.next;
return temp.data;
}
public static void main(String args[])
{
Stack_LL s = new Stack_LL();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
int x= s.pop();
System.out.println("Element popped is "+x);
int y = s.pop();
System.out.println("Element popped is "+y);
s.display();
}
}