-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackusinglinkedlist.java
85 lines (72 loc) · 2.03 KB
/
stackusinglinkedlist.java
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
public class stackusinglinkedlist {
static int size = 0;
public static class node {
int data;
node next;
node(int data) {
this.data = data;
this.next = null;
size++;
}
}
public static class stack {
int max = 5;
node top = null;
public boolean isempty() {
return top == null;
}
public boolean isfull() {
return size == max;
}
public void push(int data) {
if (isfull()) {
System.out.println("The stack is full");
return;
}
node newnode = new node(data);
System.out.println(data + " is pushed");
newnode.next = top;
top = newnode;
}
public void pop() {
if (isempty()) {
System.out.println("The stack is empty");
} else {
System.out.println(top.data + " is poped out");
top = top.next;
}
}
public void printstack() {
if (isempty()) {
System.out.println("The stack is empty");
return;
} else {
node curr = top;
while (curr != null) {
System.out.println(curr.data);
curr = curr.next;
}
}
}
public void peek() {
if (isempty()) {
System.out.println("The stack is empty");
} else {
System.out.println("The peek element is " + top.data);
}
}
}
public static void main(String[] args) {
System.out.println("subodh narayan sah");
stack s = new stack();
s.printstack();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.printstack();
s.push(6);
s.peek();
}
}