-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStacksAndQueues.java
178 lines (119 loc) · 3.33 KB
/
StacksAndQueues.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import java.util.*;
import java.io.*;
/*
class A
{
void bark()
{
System.out.println("eating");
}
}
class B
{
void bark()
{
System.out.println("barking");
}
}
class C extends B, A
{
void sleep()
{
System.out.println("sleeping");
}
}
c.bark();
class D extends B
{
void dance()
{
System.out.println("dancing");
}
}
*/
public class StacksAndQueues
{
public static void main(String[] args)
{
//B b = new B();
// A a = new A();
//b.eat();
///b.bark();/////
//
//C c = new C();
//c.eat();
//c.bark();
//c.sleep();
/*
static int sum_ofNumbers(int a, int b)
{
return a+b;
}
static int sum_ofNumbers(int a, int b, int c)
{
return a+b+c;
}
static int sum_ofNumbers(long a, int b, int c)
{
return (int)a+b+c;
}
long a = 5;
int b = 2;
int c = 5;
int sum = sum_ofNumbers(a,b,c);
System.out.println(sum);
// Create a new stack
Stack<Integer> s = new Stack<Integer>();
// Check if the s is empty
System.out.println("Is s empty? " + s.empty()); // true
// Push elements to the stack
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
// Print the s
System.out.println("s: " + s);
// [5 4 3 2 1]: MEMORY, Top: 5, In representation: [1 2 3 4 5]
System.out.println("Size: " + s.size()); //5
// Pop elements from the s
System.out.println("Popped element: " + s.pop()); // [4 3 2 1]
System.out.println("Popped element: " + s.pop()); // [3 2 1]
System.out.println("Popped element: " + s.pop()); // [2 1]
System.out.println("s: " + s);// [2 1]: memory, representation: [1 2]
System.out.println("Size: " + s.size()); //2
// Peek at the top element
System.out.println("Top element: " + s.peek()); // 2
// Check if the s is empty
System.out.println("Is s empty? " + s.empty()); // false
// Clear the s
//s.clear();
*/
// Create a new queue
Queue<Integer> queue = new LinkedList<Integer>();
//Size of Queue
System.out.println("Size: " + queue.size());//0
System.out.println("Is queue empty? " + queue.isEmpty()); // true
// Enqueue elements to the queue
queue.add(1);
queue.add(2);
queue.add(3);
queue.add(4);
queue.add(5);
// Print the queue
System.out.println("Queue: " + queue); //[1 2 3 4 5]
// Dequeue elements from the queue
System.out.println("Dequeued element: " + queue.remove()); //[2 3 4 5]
System.out.println("Dequeued element: " + queue.remove()); //[3 4 5]
System.out.println("Dequeued element: " + queue.remove()); //[4 5]
System.out.println("Queue: " + queue); //[4 5]
// Check front element
System.out.println("Front element: " + queue.element()); //4
// Size of Queue
System.out.println("Size: " + queue.size()); // 2
// Check if the queue is empty
System.out.println("Is queue empty? " + queue.isEmpty()); // false
// Clear the queue
// queue.clear();
}
}