-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Section 1.3, end of Linked-List Exercises (Ex. 1.3.31).
- Loading branch information
Showing
13 changed files
with
1,752 additions
and
0 deletions.
There are no files selected for viewing
406 changes: 406 additions & 0 deletions
406
1-Fundamentals/1-3-BagsQueuesStacks/DoublyLinkedList.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
/************************************************************************* | ||
* Compilation: javac Evaluate.java | ||
* Execution: java Evaluate | ||
* Dependencies: Stack.java | ||
* | ||
* Evaluates (fully parenthesized) arithmetic expressions using | ||
* Dijkstra's two-stack algorithm. | ||
* | ||
* % java Evaluate | ||
* ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) ) | ||
* 101.0 | ||
* | ||
* % java Evaulate | ||
* ( ( 1 + sqrt ( 5 ) ) / 2.0 ) | ||
* 1.618033988749895 | ||
* | ||
* | ||
* | ||
* Remarkably, Dijkstra's algorithm computes the same | ||
* answer if we put each operator *after* its two operands | ||
* instead of *between* them. | ||
* | ||
* % java Evaluate | ||
* ( 1 ( ( 2 3 + ) ( 4 5 * ) * ) + ) | ||
* 101.0 | ||
* | ||
* Moreover, in such expressions, all parentheses are redundant! | ||
* Removing them yields an expression known as a postfix expression. | ||
* 1 2 3 + 4 5 * * + | ||
* | ||
* | ||
*************************************************************************/ | ||
|
||
public class Evaluate { | ||
public static void main(String[] args) { | ||
Stack<String> ops = new Stack<String>(); | ||
Stack<Double> vals = new Stack<Double>(); | ||
|
||
while (!StdIn.isEmpty()) { | ||
String s = StdIn.readString(); | ||
if (s.equals("(")) ; | ||
else if (s.equals("+")) ops.push(s); | ||
else if (s.equals("-")) ops.push(s); | ||
else if (s.equals("*")) ops.push(s); | ||
else if (s.equals("/")) ops.push(s); | ||
else if (s.equals("sqrt")) ops.push(s); | ||
else if (s.equals(")")) { | ||
String op = ops.pop(); | ||
double v = vals.pop(); | ||
if (op.equals("+")) v = vals.pop() + v; | ||
else if (op.equals("-")) v = vals.pop() - v; | ||
else if (op.equals("*")) v = vals.pop() * v; | ||
else if (op.equals("/")) v = vals.pop() / v; | ||
else if (op.equals("sqrt")) v = Math.sqrt(v); | ||
vals.push(v); | ||
} | ||
else vals.push(Double.parseDouble(s)); | ||
} | ||
StdOut.println(vals.pop()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
/************************************************************************* | ||
* Exercise 1.3.11 | ||
* | ||
* % java EvaluatePostfix | ||
* 1 2 3 + 4 5 * * + | ||
* 101.0 | ||
* | ||
* % java EvaluatePostfix | ||
* 1 5 sqrt + 2.0 / | ||
* 1.618033988749895 | ||
* | ||
* % java EvaluatePostfix | ||
* 12 9 - 105 7 / * | ||
* 45.0 | ||
* | ||
* % java InfixToPostfix | java EvaluatePostfix | ||
* ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) ) | ||
* 101.0 | ||
* | ||
* % java InfixToPostfix | java EvaluatePostfix | ||
* ( ( 1 + sqrt ( 5 ) ) / 2.0 ) | ||
* 1.618033988749895 | ||
* | ||
*************************************************************************/ | ||
|
||
public class EvaluatePostfix | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
Stack<Double> vals = new Stack<Double>(); | ||
|
||
while (!StdIn.isEmpty()) | ||
{ | ||
String s = StdIn.readString(); | ||
|
||
if (s.equals("(") || | ||
s.equals(")")) ; | ||
else if (s.equals("+") || | ||
s.equals("-") || | ||
s.equals("*") || | ||
s.equals("/") || | ||
s.equals("sqrt")) | ||
{ | ||
double v = vals.pop(); | ||
|
||
if (s.equals("+")) v = vals.pop() + v; | ||
else if (s.equals("-")) v = vals.pop() - v; | ||
else if (s.equals("*")) v = vals.pop() * v; | ||
else if (s.equals("/")) v = vals.pop() / v; | ||
else if (s.equals("sqrt")) v = Math.sqrt(v); | ||
|
||
vals.push(v); | ||
} | ||
else | ||
vals.push(Double.parseDouble(s)); | ||
} | ||
|
||
StdOut.println(vals.pop()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
|
||
/************************************************************************* | ||
* Simulate a sequence of push and pop operations to find out | ||
* whether a certain sequence of pop's can occur | ||
* | ||
* % java Ex_1_3_03 | ||
* 9 8 7 6 5 4 3 2 1 0 | ||
* true (Unprinted: 0; Stack: [ ]) | ||
* | ||
* 4 3 2 1 0 9 8 7 6 5 | ||
* true (Unprinted: 0; Stack: [ ]) | ||
* | ||
* 4 6 8 7 5 3 2 9 0 1 | ||
* false (Unprinted: 2; Stack: [ 1 0 ]) | ||
* | ||
* 2 5 6 7 4 8 9 3 1 0 | ||
* true (Unprinted: 0; Stack: [ ]) | ||
* | ||
* 4 3 2 1 0 5 6 7 8 9 | ||
* true (Unprinted: 0; Stack: [ ]) | ||
* | ||
* 1 2 3 4 5 6 9 8 7 0 | ||
* true (Unprinted: 0; Stack: [ ]) | ||
* | ||
* 0 4 6 5 3 8 1 7 2 9 | ||
* false (Unprinted: 4; Stack: [ 9 7 2 1 ]) | ||
* | ||
* 1 4 7 9 8 6 5 3 0 2 | ||
* false (Unprinted: 2; Stack: [ 2 0 ]) | ||
* | ||
* 2 1 4 3 6 5 8 7 9 0 | ||
* true (Unprinted: 0; Stack: [ ]) | ||
* | ||
*************************************************************************/ | ||
|
||
public class Ex_1_3_03 | ||
{ | ||
public static void checkSequence(int[] v) | ||
{ | ||
Stack<Integer> s = new Stack<Integer>(); | ||
int n = v.length; | ||
|
||
int i = 0, j = 0; | ||
while (i < n && j <= n) | ||
{ | ||
if (!s.isEmpty() && s.peek() == v[i]) | ||
{ | ||
StdOut.print(s.pop() + " "); | ||
i++; | ||
} | ||
else | ||
{ | ||
if (j < n) | ||
s.push(j); | ||
j++; | ||
} | ||
} | ||
StdOut.println(); | ||
|
||
StdOut.printf("%s (Unprinted: %d; Stack: [ %s])\n", | ||
i == n && s.isEmpty(), n - i, s); | ||
} | ||
|
||
public static void main(String[] args) | ||
{ | ||
String[] a = StdIn.readAll().split("\\s+"); | ||
|
||
int[] v = new int[a.length]; | ||
for (int i = 0; i < a.length; i++) | ||
v[i] = Integer.parseInt(a[i]); | ||
|
||
checkSequence(v); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
|
||
/************************************************************************* | ||
* | ||
* % java Ex_1_3_04 | ||
* [()]{}{[()()]()} | ||
* true | ||
* | ||
* % java Ex_1_3_04 | ||
* [(]) | ||
* false | ||
* | ||
*************************************************************************/ | ||
|
||
public class Ex_1_3_04 | ||
{ | ||
public static boolean isBalanced(String s) | ||
{ | ||
Stack<Character> open = new Stack<Character>(); | ||
int n = s.length(); | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
char c = s.charAt(i); | ||
|
||
if (c == '(' || c == '[' || c == '{') | ||
open.push(c); | ||
else if ((c == ')' && (open.isEmpty() || open.pop() != '(')) || | ||
(c == ']' && (open.isEmpty() || open.pop() != '[')) || | ||
(c == '}' && (open.isEmpty() || open.pop() != '{'))) | ||
return false; | ||
} | ||
|
||
return open.isEmpty(); | ||
} | ||
|
||
public static void main(String[] args) | ||
{ | ||
String s = StdIn.readAll().trim(); | ||
|
||
StdOut.println(isBalanced(s)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
/************************************************************************* | ||
* | ||
* % java Ex_1_3_09 | ||
* 1 + 2 ) * 3 - 4 ) * 5 - 6 ) ) ) | ||
* ( ( 1 + 2 ) * ( ( 3 - 4 ) * ( 5 - 6 ) ) ) | ||
* | ||
* % java Ex_1_3_09 | ||
* sqrt 1 + 2 ) ) | ||
* ( sqrt ( 1 + 2 ) ) | ||
* | ||
*************************************************************************/ | ||
|
||
public class Ex_1_3_09 | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
Stack<String> ops = new Stack<String>(); | ||
Stack<String> vals = new Stack<String>(); | ||
|
||
while (!StdIn.isEmpty()) | ||
{ | ||
String s = StdIn.readString(); | ||
|
||
if (s.equals("(")) ; | ||
else if (s.equals("+") || | ||
s.equals("-") || | ||
s.equals("*") || | ||
s.equals("/") || | ||
s.equals("sqrt")) ops.push(s); | ||
else if (s.equals(")")) | ||
{ | ||
String op = ops.pop(); | ||
String v = vals.pop(); | ||
|
||
if (op.equals("+") || | ||
op.equals("-") || | ||
op.equals("*") || | ||
op.equals("/")) | ||
v = String.format("( %s %s %s )", vals.pop(), op, v); | ||
else if (op.equals("sqrt")) | ||
v = String.format("( %s %s )", op, v); | ||
|
||
vals.push(v); | ||
} | ||
else vals.push(s); | ||
//else vals.push(((Double)Double.parseDouble(s)).toString()); | ||
} | ||
|
||
StdOut.println(vals.pop()); | ||
} | ||
} |
Oops, something went wrong.