-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day18_QueuesAndStacks.java
executable file
·68 lines (55 loc) · 1.63 KB
/
Day18_QueuesAndStacks.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
/*
* Sapayth Hossain
*/
package ThirtyDaysOfCode;
import java.util.*;
/*
* @author sapaythhossain
*/
public class Day18_QueuesAndStacks {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
scan.close();
// Convert input String to an array of characters:
char[] s = input.toCharArray();
// Create a Solution object:
Day18_QueuesAndStacks p = new Day18_QueuesAndStacks();
// Enqueue/Push all chars to their respective data structures:
for (char c : s) {
p.pushCharacter(c);
p.enqueueCharacter(c);
}
// Pop/Dequeue the chars at the head of both data structures and compare them:
boolean isPalindrome = true;
for (int i = 0; i < s.length / 2; i++) {
if (p.popCharacter() != p.dequeueCharacter()) {
isPalindrome = false;
break;
}
}
//Finally, print whether string s is palindrome or not.
System.out.println("The word, " + input + ", is "
+ ((!isPalindrome) ? "not a palindrome." : "a palindrome."));
}
// Write your code here.
LinkedList q;
Stack st;
// constructor
public Day18_QueuesAndStacks() {
q = new LinkedList();
st = new Stack();
}
public void pushCharacter(char c) {
st.push(c);
}
public void enqueueCharacter(char c) {
q.addLast(c);
}
public char popCharacter() {
return (char) st.pop();
}
public char dequeueCharacter() {
return (char) q.remove(0);
}
}