-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_using_linkList.java
More file actions
68 lines (56 loc) · 1.14 KB
/
Stack_using_linkList.java
File metadata and controls
68 lines (56 loc) · 1.14 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
import java.util.*;
public class Stack_using_linkList {
public static void main(String args[]){
stack s=new stack();
s.peak();
s.push(1);
s.push(2);
s.pop();
System.out.println(s.peak());
}
}
class stack{
class Node{
int data;
Node next;
public Node(int data){
this.data=data;
this.next=null;
}
}
public static Node head;
//check empty list
public int isEmpty(){
if(head==null){
return -1;
}
return 1;
}
//push
public void push(int data){
Node newNode=new Node(data);
if(head==null){
head=newNode;
return;
}
newNode.next=head;
head=newNode;
}
//pop
public int pop(){
if(head==null){
// System.out.println("empty");
return -1;
}
head=head.next;
return 1;
}
//peak
public int peak(){
if(head==null){
System.out.println("null");
return -1;
}
return head.data;
}
}