-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingLinkedList.java
More file actions
96 lines (62 loc) · 1.71 KB
/
StackUsingLinkedList.java
File metadata and controls
96 lines (62 loc) · 1.71 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Dell
*/
import java.io.*;
import java.util.*;
class Node{
Node next;
int data;
}
public class StackUsingLinkedList {
public static Node head;
public static Node tail;
public static void push(int data){
if(head == null){
Node latest = new Node();
latest.data = data;
latest.next = head;
head = latest;
tail = latest;
}
else{
Node late = new Node();
Node temp = head;
late.data = data;
late.next = temp;
head = late;
}
}
public static int pop(){
Node temp = head;
head = temp.next;
return temp.data;
}
public static void print(){
Node ne = head;
while(ne != null){
System.out.print(ne.data + "->");
ne = ne.next;
}
}
public static void main(String[] args){
head = null;
tail = null;
push(10);
push(20);
push(30);
push(40);
push(50);
push(60);
push(100);
print();
System.out.println("\npopped element is" +pop());
System.out.println("New list is ");
print();
}
}