-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstackusingLinkedlist.java
100 lines (79 loc) · 1.71 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.util.*;
class stackLL{
public static void main(String args[]){
Scanner ip = new Scanner(System.in);
fn obj = new fn();
System.out.println("[1].Enter 1 to push in the stack.");
System.out.println("[2].Enter 2 to pop from the stack.");
System.out.println("[3].Enter 3 to peep in the stack.");
System.out.println("[4].Enter 4 to display elements in the stack.");
System.out.println("[5].Enter 5 to exit.");
int v,res;
while(true){
res = ip.nextInt();
switch(res){
case 1:
obj.push(new Integer(v = ip.nextInt()));
break;
case 2:
obj.pop();
break;
case 3:
obj.peek();
break;
case 4:
obj.display();
break;
case 5:
break;
default:
System.out.println("You entered a wrong input !");
// break;
}
if(res==5)
break;
}
}
}
class fn{
Node head;
int top = -1;
static class Node{
Object data;
Node next;
int top = -1;
Node(Object ele){
data = ele;
next = null;
}
}
public void push(Object ele){
Node newNode = new Node(ele);
newNode.next = head;
head = newNode;
top++;
}
public void pop(){
if(top==-1)
{
System.out.println("Underflow!");
}
head = head.next;
top--;
}
public void peek(){
if(top==-1)
{
System.out.println("Underflow!");
}
System.out.println(head.data);
}
public void display(){
Node temp = head;
//System.out.println(head.data);
while(temp!=null){
System.out.println(temp.data);
temp = temp.next;
}
}
}