// 4.3 bracket.java p90 2021年04月05日 星期一 21时28分50秒
String input;
while(true){
System.out.print("Enter string containing delimiters: ");
System.out.flush();
input = getString();
if(input.equals(""))
break;
BracketChecker theChecker = new BracketChecker(input);
theChecker.check();
}
}
public static String getString() throws IOException{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
public static class StackX{
private int maxSize;
private char[] stackArray;
private int top;
public StackX(int s){
maxSize = s;
stackArray = new char[maxSize];
top = -1;
}
public void push(char j){
stackArray[++top] = j;
}
public char pop(){
return stackArray[top--];
}
public char peek(){
return stackArray[top];
}
public boolean isEmpty(){
return (top == -1);
}
}
public static class BracketChecker{
private String input;
public BracketChecker(String in){
input = in;
}
public void check(){
int stackSize = input.length();
StackX theStack = new StackX(stackSize);
for(int j = 0; j < input.length(); j++){
char ch = input.charAt(j);
switch(ch){
case '{':
case '[':
case '(':
theStack.push(ch);
break;
case '}':
case ']':
case ')':
if(!theStack.isEmpty()){
char chx = theStack.pop();
if((ch == '}' && chx != '{') ||
(ch == ']' && chx != '[') ||
(ch == ')' && chx != '('))
System.out.println("Error: " +ch+" at "+ j);
}
else
System.out.println("Error: " +ch+" at "+ j);
break;
default:
break;
}
}
if(!theStack.isEmpty())
System.out.println("Error: missing right delimiter" );
}
}
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Enter string containing delimiters: a{b(c}d]e
Error: } at 5
*/
//4.4 queue.java p99 2021年04月05日 星期一 21时48分38秒
Queue theQueue = new Queue(5);
theQueue.insert(10);
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);
theQueue.remove();
theQueue.remove();
theQueue.remove();
theQueue.insert(50);
theQueue.insert(60);
theQueue.insert(70);
theQueue.insert(80);
while(!theQueue.isEmpty()){
long n = theQueue.remove();
System.out.print(n);
System.out.print(" ");
}
System.out.println(" ");
}
public static class Queue{
private int maxSize;
private long[] queArray;
private int front;
private int rear;
private int nItems;
public Queue(int s){
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
public void insert(long j){
if(rear == maxSize -1)
rear = -1;
queArray[++rear] = j;
nItems++;
}
public long remove(){
long temp = queArray[front++];
if(front == maxSize)
front = 0;
nItems--;
return temp;
}
public long peekFront(){
return queArray[front];
}
public boolean isEmpty(){
return (nItems == 0);
}
public boolean isFull(){
return(nItems == maxSize);
}
public int size(){
return nItems;
}
}
}
//p158 2021年04月06日 星期二 21时28分54秒
final int NUMBER_OF_ELEMENTS = 100;
double[] numbers = new double[NUMBER_OF_ELEMENTS];
double sum = 0;
java.util.Scanner input = new java.util.Scanner(System.in);
for(int i = 0; i < NUMBER_OF_ELEMENTS; i++){
System.out.print("Enter a new number: ");
numbers[i] = input.nextDouble();
sum += numbers[i];
}
double average = sum / NUMBER_OF_ELEMENTS;
int count = 0;
for(int i = 0; i < NUMBER_OF_ELEMENTS; i++)
if(numbers[i] > average)
count++;
System.out.println("Average is " + average);
System.out.println("Number of elements above the average " + count);
//p163 6-1 2021年04月06日 星期二 21时29分27秒
Scanner input = new Scanner(System.in);
boolean[] isCovered = new boolean[99];
int number = input.nextInt();
while(number != 0){
isCovered[number - 1] = true;
number = input.nextInt();
}
boolean allCovered = true;
for(int i = 0; i < 99; i++)
if(!isCovered[i]){
allCovered = false;
break;
}
if(allCovered)
System.out.println("The tickets cover all numbers");
else
System.out.println("The tickets don't cover all numbers");
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
2 5 6 5 4 3 23 43 2 0
The tickets don't cover all numbers
//DeckOfCards.java p165 2021年04月06日 星期二 21时35分44秒
int[] deck = new int[52];
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King"};
for(int i = 0; i < deck.length; i++)
deck[i] = i;
for(int i = 0; i < deck.length; i++){
int index = (int)(Math.random()*deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
for(int i = 0; i < 4; i++){
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] / 13];
System.out.println("Card number " + deck[i] + ": "
+ rank + " of " + suit);
}
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Card number 48: 4 of Clubs
Card number 25: 2 of Hearts
Card number 14: 2 of Hearts
Card number 28: 3 of Diamonds
int x = 1;
int[] y = new int[10];
m(x, y);
System.out.println("x is " + x);
System.out.println("y[0] is " + y[0]);
}
public static void m(int number, int[] numbers){
number = 1001;
numbers[0] = 5555;
}
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
x is 1
y[0] is 5555
//TestPassArray.java 6-3 2021年04月06日 星期二 21时47分44秒
int[] a = {1, 2};
System.out.println("Before invoking swap");
System.out.println("array is {" + a[0] + ", " + a[1] + "}");
swap(a[0], a[1]);
System.out.println("After invoking swap");
System.out.println("array is {" + a[0] + ", " + a[1] + "}");
System.out.println("Before invoking swapFirstTwoInArray");
System.out.println("array is {" + a[0] + ", " + a[1] + "}");
swapFirstTwoInArray(a);
System.out.println("After invoking swapFirstTwoInArray");
System.out.println("array is {" + a[0] + ", " + a[1] + "}");
}
public static void swap(int n1, int n2){
int temp = n1;
n1 = n2;
n2 = temp;
}
public static void swapFirstTwoInArray(int[] array){
int temp = array[0];
array[0] = array[1];
array[1] = temp;
}
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Before invoking swap
array is {1, 2}
After invoking swap
array is {1, 2}
Before invoking swapFirstTwoInArray
array is {1, 2}
After invoking swapFirstTwoInArray
array is {2, 1}
*/
//p170 6-4 CountLettersInArray.java 2021年04月06日 星期二 21时55分04秒
char[] chars = createArray();
System.out.println("The lowercase letters are: ");
displayArray(chars);
int[] counts = countLetters(chars);
System.out.println();
System.out.println("The occurrences of each letter are: ");
displayCounts(counts);
}
//https://blog.csdn.net/ajxst82848/article/details/102046566
//public static class RandomCharacter {
//生成一个介于ch1 和 ch2 的随机字母
// public static char getRandomCharacter(char ch1, char ch2) {
// return (char) (ch1 + Math.random() * (ch2 - ch1 + 1));
// }
//生成一个随机的小写字母
// public static char getRandomLowerCaseLetter() {
// return getRandomCharacter('a', 'z');
// }
//}
//https://blog.csdn.net/wx1217386409/article/details/78242923?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-1.baidujs&dist_request_id=1328769.72925.16177180156488559&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-1.baidujs
public static class RandomCharacter {
public static char getRandomCharacter(char ch1,char ch2)
{
//随机生成一个介于ch1 和 ch2之间的字符。
return (char)(ch1 + Math.random()*(ch2 - ch1 + 1));
}
//生成一个随机的小写字母。
public static char getRandomLowerCaseLetter(){
return getRandomCharacter('a','z');
}
//生成一个随机的大写字母。
public static char getRandomUpperCaseLetter(){
return getRandomCharacter('A','Z');
}
//生成一个随机的数字。
public static char getRandomDigitCharacter(){
return getRandomCharacter('0','9');
}
//生成一个随机的字符。
public static char getRandomCharacter(){
return getRandomCharacter('\u0000','\uFFFF');
}
public static void main(String[] args)
{
char ch = RandomCharacter.getRandomCharacter();
System.out.println(ch);
}
}
public static char[] createArray(){
char[] chars = new char[100];
for(int i = 0; i < chars.length; i++)
chars[i] = RandomCharacter.getRandomLowerCaseLetter();
return chars;
}
public static void displayArray(char[] chars){
for(int i = 0; i < chars.length; i++){
if((i + 1)% 20 == 0)
System.out.println(chars[i]);
else
System.out.println(chars[i] + " ");
}
}
public static int[] countLetters(char[] chars){
int[] counts = new int[26];
for(int i = 0; i < chars.length; i++)
counts[chars[i] - 'a']++;
return counts;
}
public static void displayCounts(int[] counts){
for(int i = 0; i < counts.length; i++){
if((i+ 1) % 10 == 0)
System.out.println(counts[i] + " " + (char)(i + 'a'));
else
System.out.println(counts[i] + " " + (char)(i + 'a') + " ");
}
//4.6 priorityQ.java p106 2021年04月08日 星期四 21时53分29秒
PriorityQ thePQ = new PriorityQ(5);
thePQ.insert(30);
thePQ.insert(50);
thePQ.insert(10);
thePQ.insert(40);
thePQ.insert(20);
while(!thePQ.isEmpty()){
long item = thePQ.remove();
System.out.print(item + " ");
}
System.out.println("");
}
public static class PriorityQ{
private int maxSize;
private long[] queArray;
private int nItems;
public PriorityQ(int s){
maxSize = s;
queArray = new long[maxSize];
nItems = 0;
}
public void insert(long item){
int j;
if(nItems == 0)
queArray[nItems++] = item;
else{
for(j = nItems-1; j >= 0; j--){
if(item > queArray[j])
queArray[j+1] = queArray[j];
else
break;
}
queArray[j+1] = item;
nItems++;
}
}
public long remove(){
return queArray[--nItems];
}
public long peekMin(){
return queArray[nItems - 1];
}
public boolean isEmpty(){
return (nItems == 0);
}
public boolean isFull(){
return (nItems == maxSize);
}
}wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
10 20 30 40 50
//4.7 infix.java 2021年04月10日 星期日 21时28分40秒
String input, output;
while(true){
System.out.print("Enter infix: ");
System.out.flush();
input = getString();
if(input.equals(""))
break;
InToPost theTrans = new InToPost(input);
output = theTrans.doTrans();
System.out.println("Postfix is " + output + '\n');
}
}
public static String getString() throws IOException{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
public static class StackX{
private int maxSize;
private char[] stackArray;
private int top;
public StackX(int s){
maxSize = s;
stackArray = new char[maxSize];
top = -1;
}
public void push(char j){
stackArray[++top] = j;
}
public char pop(){
return stackArray[top--];
}
public char peek(){
return stackArray[top];
}
public boolean isEmpty(){
return (top == -1);
}
public int size(){
return top+1;
}
public char peekN(int n){
return stackArray[n];
}
public void displayStack(String s){
System.out.print(s);
System.out.print("Stack (bootom-->top): ");
for(int j = 0; j < size(); j++){
System.out.print(peekN(j));
System.out.print(' ');
}
System.out.println("");
}
}
public static class InToPost{
private StackX theStack;
private String input;
private String output = "";
public InToPost(String in){
input = in;
int stackSize = input.length();
theStack = new StackX(stackSize);
}
public String doTrans(){
for(int j = 0; j < input.length(); j++){
char ch = input.charAt(j);
theStack.displayStack("For " + ch + " ");
switch(ch){
case '+':
case '-':
gotOper(ch, 1);
break;
case '*':
case '/':
gotOper(ch, 2);
break;
case '(':
theStack.push(ch);
break;
case ')':
gotParen(ch);
break;
default:
output = output + ch;
break;
}
}while(!theStack.isEmpty()){
theStack.displayStack("while ");
output = output + theStack.pop();
}
theStack.displayStack("End ");
return output;
}
public void gotOper(char opThis, int prec1){
while(!theStack.isEmpty()){
char opTop = theStack.pop();
if(opTop == '('){
theStack.push(opTop);
break;
}else{
int prec2;
if(opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if(prec2 < prec1){
theStack.push(opTop);
break;
}else
output = output + opTop;
}
}
theStack.push(opThis);
}
public void gotParen(char ch){
while(!theStack.isEmpty()){
char chx = theStack.pop();
if(chx == '(')
break;
else
output = output + chx;
}
}
}wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Enter infix: A*(B + c) - D/(E + F)
For A Stack (bootom-->top):
For * Stack (bootom-->top):
For ( Stack (bootom-->top): *
For B Stack (bootom-->top): * (
For Stack (bootom-->top): * (
For + Stack (bootom-->top): * (
For Stack (bootom-->top): * ( +
For c Stack (bootom-->top): * ( +
For ) Stack (bootom-->top): * ( +
For Stack (bootom-->top): *
For - Stack (bootom-->top): *
For Stack (bootom-->top): -
For D Stack (bootom-->top): -
For / Stack (bootom-->top): -
For ( Stack (bootom-->top): - /
For E Stack (bootom-->top): - / (
For Stack (bootom-->top): - / (
For + Stack (bootom-->top): - / (
For Stack (bootom-->top): - / ( +
For F Stack (bootom-->top): - / ( +
For ) Stack (bootom-->top): - / ( +
while Stack (bootom-->top): - /
while Stack (bootom-->top): -
End Stack (bootom-->top):
Postfix is AB c+ * DE F+/-
// 4.8 postfix.java 2021年04月11日 星期日 07时33分25秒 p124
String input;
int output;
while(true){
System.out.print("Enter postfix: ");
System.out.flush();
input = getString();
if(input.equals(""))
break;
ParsePost aParser = new ParsePost(input);
output = aParser.doParse();
System.out.println("Evaluates is " + output + '\n');
}
}
public static String getString() throws IOException{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
public static class StackX{
private int maxSize;
private int[] stackArray;
private int top;
public StackX(int size){
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}
public void push(int j){
stackArray[++top] = j;
}
public int pop(){
return stackArray[top--];
}
public int peek(){
return stackArray[top];
}
public boolean isEmpty(){
return (top == -1);
}
public boolean isFull(){
return (top == maxSize - 1);
}
public int size(){
return top+1;
}
public int peek(int n){
return stackArray[n];
}
public void displayStack(String s){
System.out.print(s);
System.out.print("Stack (bootom-->top): ");
for(int j = 0; j < size(); j++){
System.out.print(peek(j));
System.out.print(' ');
}
System.out.println("");
}
}
public static class ParsePost{
private StackX theStack;
private String input;
public ParsePost(String s){
input = s;
}
public int doParse(){
theStack = new StackX(20);
char ch;
int j;
int num1, num2, interAns;
for( j = 0; j < input.length(); j++){
ch = input.charAt(j);
theStack.displayStack(" " + ch + " ");
if(ch >= '0' && ch <= '9')
theStack.push((int)(ch-'0'));//是"-" ,不是"."
else{
num2 = theStack.pop();
num1 = theStack.pop();
switch(ch){
case '+':
interAns = num1 + num2;
break;
case '-':
interAns = num1 - num2;
break;
case '*':
interAns = num1 * num2;
break;
case '/':
interAns = num1 / num2;
break;
default:
interAns = 0;
}//end switch
theStack.push(interAns);
}//end else
}//end for
interAns = theStack.pop();
return interAns;
}//end doParse()
}//end class ParsePost
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Enter postfix: 345+*612+/-
3 Stack (bootom-->top):
4 Stack (bootom-->top): 3
5 Stack (bootom-->top): 3 4
+ Stack (bootom-->top): 3 4 5
* Stack (bootom-->top): 3 9
6 Stack (bootom-->top): 27
1 Stack (bootom-->top): 27 6
2 Stack (bootom-->top): 27 6 1
+ Stack (bootom-->top): 27 6 1 2
/ Stack (bootom-->top): 27 6 3
- Stack (bootom-->top): 27 2
Evaluates is 25
//5.1 linkList.java p140 2021年04月11日 星期日 08时15分12秒
LinkList theList = new LinkList();
theList.insertFirst(22, 2.99);
theList.insertFirst(44, 4.99);
theList.insertFirst(66, 6.99);
theList.insertFirst(88, 8.99);
theList.displayList();
while(!theList.isEmpty()){
Link aLink = theList.deleteFirst();
System.out.print("Deleted ");
aLink.displayLink();
System.out.println("");
}
theList.displayList();
}
public static class Link{
public int iData;
public double dData;
public Link next;
public Link(int id, double dd){
iData = id;
dData = dd;
}
public void displayLink(){
System.out.print("{" + iData + ", " + dData + "}");
}
}
public static class LinkList{
private Link first;
public LinkList(){
first = null;
}
public boolean isEmpty(){
return(first == null);
}
public void insertFirst(int id, double dd){
Link newLink = new Link(id, dd);
newLink.next = first;
first = newLink;
}
public Link deleteFirst(){
Link temp = first;
first =first.next;
return temp;
}
public void displayList(){
System.out.print("List (first-->last): ");
Link current = first;
while(current != null){
current.displayLink();
current = current.next;
}
System.out.println(" ");
}
}wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
List (first-->last): {88, 8.99}{66, 6.99}{44, 4.99}{22, 2.99}
Deleted {88, 8.99}
Deleted {66, 6.99}
Deleted {44, 4.99}
Deleted {22, 2.99}
List (first-->last):
//5.2 linkList2.java 2021年04月12日 星期一 21时18分17秒
LinkList theList = new LinkList();
theList.insertFirst(22, 2.99);
theList.insertFirst(44, 4.99);
theList.insertFirst(66, 6.99);
theList.insertFirst(88, 8.99);
theList.displayList();
Link f = theList.find(44);
if(f != null){
System.out.print("Found link with key " + f.iData);
}else
System.out.println("Can't delete link");
theList.displayList();
}
public static class Link{
public int iData;
public double dData;
public Link next;
public Link(int id, double dd){
iData = id;
dData = dd;
}
public void displayLink(){
System.out.print("{" + iData + ", " + dData + "}");
}
}//漏掉了
public static class LinkList{
private Link first;
public LinkList(){
first = null;
}
public void insertFirst(int id, double dd){
Link newLink = new Link(id, dd);
newLink.next = first;
first = newLink;
}
public Link find(int key){
Link current = first;
while(current.iData != key){
if(current.next == null)
return null;
else
current = current.next;
}
return current;
}
public Link delete(int key){
Link current = first;
Link previous = first;
while(current.iData != key){
if(current.next == null)
return null;
else{
previous = current;
current = current.next;
}
}
if(current == first)
first = first.next;
else
previous.next = current.next;
return current;
}
public void displayList(){
System.out.print(" List (first-->last): ");
Link current = first;
while(current != null){
current.displayLink();
current = current.next;
}
System.out.println(" ");
}
}wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
List (first-->last): {88, 8.99}{66, 6.99}{44, 4.99}{22, 2.99}
Found link with key 44 List (first-->last): {88, 8.99}{66, 6.99}{44, 4.99}{22, 2.99}
//5.3 firstLastList.java p146 2021年04月12日 星期一 21时43分49秒
FirstLastList theList = new FirstLastList();
theList.insertFirst(22);
theList.insertFirst(44);
theList.insertFirst(66);
theList.insertFirst(11);
theList.insertFirst(33);
theList.insertFirst(55);
theList.displayList();
theList.deleteFirst();
theList.deleteFirst();
theList.deleteFirst();
}
public static class Link{
public long dData;
public Link next;
public Link(long d){ dData = d; }
public void displayLink(){
System.out.print(dData + " "); }
}
public static class FirstLastList{
private Link first;
private Link last;
public FirstLastList(){
first = null;
last = null;
}
public boolean isEmpty(){
return first == null; }
public void insertFirst(long dd){
Link newLink = new Link(dd);
if(isEmpty())
last = newLink;
newLink.next = first;
first = newLink;
}
public void insertLast(long dd){
Link newLink = new Link(dd);
if(isEmpty())
first = newLink;
else
last.next = newLink;
last = newLink;
}
public long deleteFirst(){
long temp = first.dData;
if(first.next == null)
last = null;
first = first.next;
return temp;
}
public void displayList(){
System.out.print("List (first-->last): ");
Link current = first;
while(current != null){
current.displayLink();
current = current.next;
}
System.out.println(" ");
}
}wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
List (first-->last): 55 33 11 66 44 22
//5.4 linkStack.java p152 2021年04月13日 星期二 17时13分53秒
LinkStack theStack = new LinkStack();
theStack.push(20);
theStack.push(40);
theStack.displayStack();
theStack.push(60);
theStack.push(80);
theStack.displayStack();
theStack.pop();
theStack.pop();
theStack.displayStack();
}
public static class Link{
public long dData;
public Link next;
public Link(long dd){
dData = dd;
}
public void displayLink(){
System.out.print(dData + " ");
}
}
public static class LinkList{
private Link first;
public LinkList(){
first = null;
}
public boolean isEmpty(){
return (first == null);
}
public void insertFirst(long dd){
Link newLink = new Link(dd);
newLink.next = first;
first = newLink;
}
public long deleteFirst(){
Link temp = first;
first = first.next;
return temp.dData;
}
public void displayList(){
Link current = first;
while(current != null){
current.displayLink();
current = current.next;
}
System.out.println(" ");
}
}
public static class LinkStack{
private LinkList theList;
public LinkStack(){
theList = new LinkList();
}
public void push(long j){
theList.insertFirst(j);
}
public long pop(){
return theList.deleteFirst();
}
public boolean isEmpty(){
return (theList.isEmpty());
}
public void displayStack(){
System.out.print("Stack(top-->bottom): ");
theList.displayList();
}
}wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Stack(top-->bottom): 40 20
Stack(top-->bottom): 80 60 40 20
Stack(top-->bottom): 40 20
//5.5 linkQueue.java 2021年04月13日 星期二 17时32分55秒
LinkQueue theQueue = new LinkQueue();
theQueue.insert(20);
theQueue.insert(40);
theQueue.displayQueue();
theQueue.insert(60);
theQueue.insert(80);
theQueue.displayQueue();
theQueue.remove();
theQueue.remove();
theQueue.displayQueue();
}
public static class Link{
public long dData;
public Link next;
public Link(long d){
dData = d;
}
public void displayLink(){
System.out.print(dData + " ");
}
}
public static class FirstLastList{
private Link first;
private Link last;
public FirstLastList(){
first = null;
last = null;
}
public boolean isEmpty(){
return first == null;
}
public void insertLast(long dd){
Link newLink = new Link(dd);
if(isEmpty())
first = newLink;
else
last.next = newLink;
last = newLink;
}
public long deleteFirst(){
long temp = first.dData;
if(first.next == null)
last = null;
first = first.next;
return temp;
}
public void displayList(){
Link current = first;
while(current != null){
current.displayLink();
current = current.next;
}
System.out.println("");
}
}
public static class LinkQueue{
private FirstLastList theList;
//构造函数未写, 出问题 2021年04月13日 星期二 19时45分34秒
//Exception in thread "main" java.lang.NullPointerException
//at hello$LinkQueue.insert(hello.java:3131)
//at hello.main(hello.java:3071)
public LinkQueue(){
theList = new FirstLastList();
}
public boolean isEmpty(){
return theList.isEmpty();
}
public void insert(long j){
theList.insertLast(j);
}
public long remove(){
return theList.deleteFirst();
}
public void displayQueue(){
System.out.print("Queue(front-->rear): ");
theList.displayList();
}
}wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Queue(front-->rear): 20 40
Queue(front-->rear): 20 40 60 80
Queue(front-->rear): 60 80
//5.6 sortedList.java p160 2021年04月14日 星期三 21时21分51秒
SortedList theSortedList = new SortedList();
theSortedList.insert(20);
theSortedList.insert(40);
theSortedList.displayList();
theSortedList.insert(10);
theSortedList.insert(30);
theSortedList.insert(50);
theSortedList.displayList();
theSortedList.displayList();
}
public static class Link{
public long dData;
public Link next;
public Link (long dd){
dData = dd;
}
public void displayLink(){
System.out.print(dData + " ");
}
}
public static class SortedList{
private Link first;
public SortedList(){
first = null;
}
public boolean isEmpty(){
return(first == null);
}
public void insert(long key){
Link newLink = new Link(key);
Link previous = null;
Link current = first;
while(current != null && key > current.dData){
previous = current;
current = current.next;
}
if(previous == null)
first = newLink;
else
previous.next = newLink;
newLink.next = current;
}
public Link remove(){
Link temp = first;
first = first.next;
return temp;
}
public void displayList(){
System.out.print("List(first-->last): ");
Link current = first;
while(current != null){
current.displayLink();
current = current.next;
}
System.out.println("");
}
}wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
List(first-->last): 20 40
List(first-->last): 10 20 30 40 50
List(first-->last): 10 20 30 40 50
//5.7 listInsertionSort.java p164 2021年04月14日 星期三 21时22分43秒
int size = 10;
Link[] linkArray = new Link[size];
for(int j = 0; j < size; j++){
int n = (int)(java.lang.Math.random()*99);
Link newLink = new Link(n);
linkArray[j] = newLink;
}
System.out.print("Unsorted array: ");
for(int j = 0; j < size; j++)
System.out.print(linkArray[j].dData + " ");
System.out.println(" ");
SortedList theSortedList = new SortedList(linkArray);
for(int j = 0; j < size; j++)
linkArray[j] = theSortedList.remove();
System.out.print("Sorted Array: ");
for(int j = 0; j < size; j++)
System.out.print(linkArray[j].dData + " ");
System.out.println("");
}
public static class Link{
public long dData;
public Link next;
public Link(long dd){
dData = dd;
}
}
public static class SortedList{
private Link first;
public SortedList(){
first = null;
}
public SortedList(Link[] linkArr){
first = null;
for(int j = 0; j < linkArr.length; j++)
insert(linkArr[j]);
}
public void insert(Link k){
Link previous = null;
Link current = first;
while(current != null && k.dData > current.dData){
previous = current;
current = current.next;
}
if(previous == null)
first = k;
else
previous.next = k;
k.next = current;
}
public Link remove(){
Link temp = first;
first = first.next;
return temp;
}
}wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Unsorted array: 63 79 46 57 92 56 33 45 35 96
Sorted Array: 33 35 45 46 56 57 63 79 92 96
//5.8 doublyLinked.java p170 2021年04月14日 星期三 21时37分06秒
DoublyLinkedList theList = new DoublyLinkedList();
theList.insertFirst(22);
theList.insertFirst(44);
theList.insertFirst(66);
theList.insertFirst(11);
theList.insertFirst(33);
theList.insertFirst(55);
theList.displayForward();
theList.displayBackward();
theList.deleteFirst();
theList.deleteLast();
theList.deleteKey(11);
theList.displayForward();
theList.insertAfter(22, 77);
theList.insertAfter(33, 88);
theList.displayForward();
}
public static class Link{
public long dData;
public Link next;
public Link previous;
public Link(long d){
dData = d;
}
public void displayLink(){
System.out.print(dData + " ");
}
}
public static class DoublyLinkedList{
private Link first;
private Link last;
public DoublyLinkedList(){
first = null;
last = null;
}
public boolean isEmpty(){
return first == null;// == 打错了 =
}
public void insertFirst(long dd){
Link newLink = new Link(dd);
if(isEmpty())
last = newLink;
else
first.previous = newLink;
newLink.next = first;
first = newLink; //fist
}
public void insertLast(long dd){ //void 打错了 viod
Link newLink = new Link(dd);
if(isEmpty())
first = newLink;
else{
last.next = newLink;
newLink.previous = last;
}
last = newLink;
}
public Link deleteFirst(){
Link temp = first;
if(first.next == null)
last = null;
else
first.next.previous = null;
first = first.next;
return temp;
}
public Link deleteLast(){
Link temp = last;
if(first.next == null)
first = null;
else
last.previous.next = null;
last = last.previous;
return temp;
}
public boolean insertAfter(long key, long dd){
Link current = first;
while(current.dData != key){
current = current.next;
if(current == null)
return false;
}
Link newLink = new Link(dd);
if(current == last){
newLink.next = null;
last = newLink;
}else{
newLink.next = current.next;
current.next.previous = newLink;
}
newLink.previous = current;
current.next = newLink;
return true;
}
public Link deleteKey(long key){
Link current = first;
while(current.dData != key){
current = current.next;
if(current == null)
return null;
}
if(current == first)
first = current.next;
else
current.previous.next = current.next;
if(current == last)
last = current.previous;
else
current.next.previous = current.previous;
return current;
}
public void displayForward(){
System.out.print("List(first-->last): ");
Link current = first;
while(current != null){
current.displayLink();//currnet
current = current.next;
}
System.out.println(" ");
}
public void displayBackward(){
System.out.print("List(last-->first): ");
Link current = last;
while(current != null){
current.displayLink(); // currnet
current = current.previous;
}
System.out.println(" ");
}
}wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
List(first-->last): 55 33 11 66 44 22
List(last-->first): 22 44 66 11 33 55
List(first-->last): 33 66 44
List(first-->last): 33 88 66 44
//interIterator.java p178 2021年04月15日 星期四 22时01分03秒
LinkList theList = new LinkList();
ListIterator iter1 = theList.getIterator();
long value;
iter1.insertAfter(20);
iter1.insertAfter(40);
iter1.insertAfter(80);
iter1.insertAfter(60);
while(true){
System.out.print("Enter first letter of show, reset, ");
System.out.print("next, get, before, after, delete: ");
System.out.flush();
int choice = getChar();
switch(choice){
case 's':
if(!theList.isEmpty())
theList.displayList();
else
System.out.println("List is empty");
break;
case 'r':
iter1.reset();
break;
case 'n':
if(!theList.isEmpty() && !iter1.atEnd())
iter1.nextLink();
else
System.out.println("Can't go to next link");
break;
case 'g':
if(!theList.isEmpty()){
value = iter1.getCurrent().dData;
System.out.println("Returned " + value);
}else
System.out.println("List is empty");
break;
case 'b':
System.out.print("Enter value to insert: ");
System.out.flush();
value = getInt();
iter1.insertBefore(value);
break;
case 'a':
System.out.print("Enter value to insert: ");
System.out.flush();
value = getInt();
iter1.insertAfter(value);//iter1.insertBefore(value);
break;
case 'd':
if(!theList.isEmpty()){
value = iter1.deleteCurrent();
System.out.println("Deleted " + value);
}else
System.out.println("Can't delete");
break;
default:
System.out.println("Invalid entry");
}
}
}
public static String getString() throws IOException{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
public static char getChar() throws IOException{
String s = getString();
return s.charAt(0);
}
public static int getInt() throws IOException{
String s = getString();
return Integer.parseInt(s);
}
public static class Link{
public long dData;
public Link next;
public Link (long dd){
dData = dd;
}
public void displayLink(){
System.out.print(dData + " ");
}
}
public static class LinkList{
private Link first;
public LinkList(){
first = null;
}
public Link getFirst(){
return first;
}
public void setFirst(Link f){
first = f;
}
public boolean isEmpty(){
return first == null;
}
public ListIterator getIterator(){
return new ListIterator(this);
}
public void displayList(){
Link current = first;
while(current != null){
current.displayLink();
current = current.next;
}
System.out.println(" ");
}
}
public static class ListIterator{
private Link current;
private Link previous;
private LinkList ourList;
public ListIterator(LinkList list){
ourList = list;
reset();
}
public void reset(){
current = ourList.getFirst();
previous = null;
}
public boolean atEnd(){
return (current.next == null);
}
public void nextLink(){
previous = current;
current = current.next;
}
public Link getCurrent(){
return current;
}
public void insertAfter(long dd){
Link newLink = new Link(dd);
if(ourList.isEmpty()){
ourList.setFirst(newLink);
current = newLink;
}else{
newLink.next = current.next;
current.next = newLink;
nextLink();
}
}
public void insertBefore(long dd){
Link newLink = new Link(dd);
if(previous == null){
newLink.next = ourList.getFirst();
ourList.setFirst(newLink);
reset();
}else{
newLink.next = previous.next;
previous.next = newLink;
current = newLink;
}
}
public long deleteCurrent(){
long value = current.dData;
if(previous == null){
ourList.setFirst(current.next);
reset();
}else{
previous.next = current.next;
if(atEnd())
reset();
else
current = current.next;
}
return value;
}
}wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Enter first letter of show, reset, next, get, before, after, delete: s
20 40 80 60
Enter first letter of show, reset, next, get, before, after, delete: r
Enter first letter of show, reset, next, get, before, after, delete: n
Enter first letter of show, reset, next, get, before, after, delete: n
Enter first letter of show, reset, next, get, before, after, delete: g
Returned 80
Enter first letter of show, reset, next, get, before, after, delete: b
Enter value to insert: 100
Enter first letter of show, reset, next, get, before, after, delete: a
Enter value to insert: 7
Enter first letter of show, reset, next, get, before, after, delete: s
20 40 100 7 80 60
//6.1 triangle.java 2021年04月19日 星期一 21时18分41秒 p192
System.out.print("Enter a number: ");
theNumber = getInt();
int theAnswer = triangle(theNumber);
System.out.println("Triangle = " + theAnswer);
}
static int theNumber;
public static int triangle(int n){
if(n == 1)
return 1;
else
return (n + triangle(n-1));
}
public static String getString() throws IOException{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
public static int getInt() throws IOException{
String s = getString();
return Integer.parseInt(s);
}wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Enter a number: 100
Triangle = 5050
// anagram.java p199 2021年04月19日 星期一 21时32分53秒
System.out.print("Enter a word: ");
String input = getString();
size = input.length();
count = 0;
for(int j = 0; j < size; j++)
arrChar[j] = input.charAt(j);
doAnagram(size);
}
static int size;
static int count;
static char[] arrChar = new char[100];
public static void doAnagram(int newSize){
if(newSize == 1)
return;
for(int j = 0; j < newSize; j++){
doAnagram(newSize - 1);
if(newSize == 2)
displayWord();
rotate(newSize);
}
}
public static void rotate(int newSize){
int j ;
int position = size - newSize;
char temp = arrChar[position];
for(j = position + 1; j < size; j++)
arrChar[j-1] = arrChar[j];
arrChar[j-1] = temp;
}
public static void displayWord(){
if(count < 99)
System.out.print(" ");
if(count < 9)
System.out.print(" ");
System.out.print(++count + " ");
for(int j = 0; j < size; j++)
System.out.print(arrChar[j]);
System.out.print(" ");
System.out.flush();
if(count % 6 == 0)
System.out.print(" ");
}
public static String getString() throws IOException{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Enter a word: cats
1 cats 2 cast 3 ctsa 4 ctas 5 csat 6 csta
7 atsc 8 atcs 9 asct 10 astc 11 acts 12 acst
13 tsca 14 tsac 15 tcas 16 tcsa 17 tasc 18 tacs
19 scat 20 scta 21 satc 22 sact 23 stca 24 stac
//binarySearch.java 2021年04月19日 星期一 22时09分01秒
int maxSize = 100;
ordArray arr;
arr = new ordArray(maxSize);
arr.insert(72);
arr.insert(90);
arr.insert(45);
arr.insert(126);
arr.insert(54);
arr.insert(99);
arr.insert(144);
arr.insert(27);
arr.insert(135);
arr.insert(81);
arr.insert(18);
arr.insert(108);
arr.insert(9);
arr.insert(117);
arr.insert(63);
arr.insert(36);
arr.display();
int searchKey = 27;
if(arr.find(searchKey) != arr.size())
System.out.println("Found " + searchKey);
else
System.out.println("Can't find " + searchKey);
}
public static class ordArray{
private long[] a;
private int nElems;
public ordArray(int max){
a = new long[max];
nElems = 0;
}
public int size(){
return nElems;
}
public int find(long searchKey){
return recFind(searchKey, 0, nElems - 1);
}
private int recFind(long searchKey, int lowerBound, int upperBound){
int curIn;
curIn = (lowerBound + upperBound) / 2;
if(a[curIn] == searchKey)
return curIn;
else if(lowerBound > upperBound)
return nElems;
else{
if(a[curIn] < searchKey)
return recFind(searchKey, curIn+1, upperBound);
else
return recFind(searchKey, lowerBound, curIn -1);
}
}
public void insert(long value){
int j;
for(j = 0; j < nElems; j++)
if(a[j] > value)
break;
for(int k = nElems; k > j; k--)
a[k] = a[k-1];
a[j] = value;
nElems++;
}
public void display(){
for(int j = 0; j < nElems; j++)
System.out.print(a[j] + " ");
System.out.println("");
}
} wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 144
Found 27
*/
}