-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_implementation_using_Linked_List.java
More file actions
129 lines (104 loc) · 2.92 KB
/
Stack_implementation_using_Linked_List.java
File metadata and controls
129 lines (104 loc) · 2.92 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.Scanner;
/**
* Stack_implementation_using_Linked_List
*/
public class Stack_implementation_using_Linked_List {
public static class Node {
int data;
Node next = null ;
Node(int value){
this.data = value;
}
@Override
public String toString() {
return "" + this.data;
}
}
public static class Stack{
int lenght =0;
Node top;
@Override
public String toString() {
String temp = "";
Node current = this.top;
while (current != null) {
temp = temp + current.data+ " -- " ;
current =current.next;
}
return temp + "Stack End";
}
public void push(int i){
Node temp = new Node(i);
if (this.top==null) {
this.top=temp;
this.lenght++;
return ;
}
temp.next=this.top;
top = temp;
this.lenght++;
}
public int pop(){
if(this.top == null){
System.out.println("Stack Underflow");
return -1;
}
Node temp = this.top;
top = top.next;
this.lenght--;
return temp.data;
}
public void peek(){
System.out.println("top = "+this.top);
}
}
public static void main(String[] args) {
Stack a = new Stack();
Scanner sc = new Scanner(System.in);
boolean l = true;
System.out.println("Empty stack initailized perform your operations :");
while (l) {
System.out.println();
System.out.print("1) Push ");
System.out.print("2) Pop ");
System.out.print("3) Peek ");
System.out.print("4) Is Stack empty ");
System.out.print("5) Display ");
System.out.print("6) Exit \n");
System.out.print("Enter your choice : ");
int ch = sc.nextInt();
System.out.println("OUTPUT:");
switch (ch) {
case 1:
System.out.print("Enter int to push: ");
int i = sc.nextInt();
a.push(i);
break;
case 2:
int poped = a.pop();
System.out.println(poped + " got removed");
break;
case 3:
a.peek();
break;
case 4:
if (a.lenght == 0) {
System.out.println("Stack is empty");
} else {
System.out.println("Stack is not empty");
}
break;
case 6:
System.out.println("Bye!");
l=false;
break;
case 5:
System.out.println(a);
break;
default:
System.out.println("Invalid choice");
}
}
sc.close();
}
}