-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
137 lines (125 loc) · 2.61 KB
/
Stack.java
File metadata and controls
137 lines (125 loc) · 2.61 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
127
128
129
130
131
132
133
134
135
136
137
//CLASS STACK USING LINKED LIST
class Node<T>{
public T data;
public Node next;
}
class Stack<T>{
private Node top;
public void push(T val){
System.out.println("Element pushed is " + val);
if(top==null)
{
top=new Node();
top.data=val;
return;
}
Node temp=new Node();
temp.data=val;
temp.next=top;
top=temp;
}
public void pop(){
if(top!=null)
{
if(this.top==null)
{
System.out.println("STACK IS EMPTY");
return;
}
System.out.println("The popped item is " + top.data);
top=top.next;
}
}
public void top(){
if(!top){
System.out.println("Stack is EMPTY");
return;
}
System.out.println("The top element is "top.data);
}
public void print(){
if(top!=null)
{
Node temp=this.top;
while(temp!=null)
{
System.out.print(temp.data+" ");
temp=temp.next;
}
System.out.print("\n");
}
}
public Stack ShallowCopy(){
return this;
}
public Stack DeepCopy(Stack){
Stack temp = this.clone();
return temp;
}
}
public class StackImplement{
public static void main(String[] args){
Stack obj=new Stack();
System.out.println("Character Stack");
obj.push('E');
obj.push('D');
obj.push('C');
obj.push('B');
obj.push('A');
obj.print();
obj.pop();
obj.pop();
obj.print();
obj=new Stack();
System.out.println("Integer Stack");
obj.push(5);
obj.push(4);
obj.push(3);
obj.print();
obj.push(2);
obj.push(1);
obj.print();
obj.pop();
obj.pop();
obj.print();
obj=new Stack();
System.out.println("Floating Point Stack");
obj.push(0.5f);
obj.push(0.4f);
obj.push(0.3f);
obj.print();
obj.push(0.2f);
obj.push(0.1f);
obj.print();
obj.pop();
obj.pop();
obj.print();
obj=new Stack();
System.out.println("String Stack");
obj.push("Wats");
obj.push("Kumar");
obj.push("Aman");
obj.print();
obj.push("NAME");
obj.push("MY");
obj.print();
obj.pop();
obj.pop();
obj.print();
//this is for checking shallowcopy
System.out.println("This checks for Shallow Copy");
Stack temp1=new Stack();
temp1=obj.ShallowCopy();
temp1.print();
temp1.push("qwerty");
obj.print();
//this is for checking Deepcopy;
System.out.println("This Checks for Deep Copy");
Stack temp2=new Stack();
temp2=obj.DeepCopy();
temp2.print();
temp2.push("qwerty");
temp2.print();
obj.print();
}
}