java学习第十天到第二十天

java学习第十一天: 顺序表(一)

学习:

在《数据结构》中, 使用“抽象数据类型”来描述不同的数据结构. 在《面向对象程序设计》中, 用对象来存储数据及其上的操作. 我认为, 它们的本质都是相同的.
11.1 对象: 数据及其上操作的总和. 例如, 我是一个对象, 具有身高、体重、年龄、跑步速度等数据; 同时,我具有吃饭、睡觉、送快递等功能. 从计算机的发展来看, 第一阶段以操作 (函数) 为中心, 一个计算导弹轨迹的函数, 根据不同输入获得不同输出. 第二阶段以数据为中心, 即数据存放于数据库, 使用不同的算法来处理它. 第三阶段认为数据及其上的操作是统一不可分的, 这就到了面向对象.
11.2 类. 前面已经使用过 int i; 这类代码, int 就是类型, i 是一个具体的整数变量. 同理, 对象就是属于某种类的变量. 也可以用集合的方式来理解: 类是集合, 对象是其中的元素; int 是指所有整数的集合, i 是其中的一个元素.
11.3 包. 包并非程序设计必须的东西, 其作用仅仅是将类进行合理的组织. 但是, 在计算机界, 往往这种可有可无的东西才是最重要的. 如文档、注释、编码规范. 可有可无是针对程序的运行而言, 其核心是计算机; 而重要是针对程序的易读性、可维护性而言, 其核心是程序员.
11.4 常量用 final 修饰. 这里故意把 MAX_LENGTH 设置得比较少, 方便调拭后面的越界检查代码.
11.5 用 new 生成新的对象.
11.6 有一个成员变量叫做 length. 程序里还有用 length 表示一个整数数组的长度. 实际上, 同一个变量名可以被不同的类所使用, 例如: 人有体重, 西瓜也有重量. 由于限定了不同的类、不同的对象, 它们之间就不会有冲突. 张三的体重、李四的体重,有关联才奇怪了. 这段描述写出来怪怪的, 明明现实生活中就是如此. 但这也正是体现了面向对象的特点: 比面向过程的程序设计更贴合我们的人类认知, 也就更远离机器底层.
11.7 toString 这个方法很特殊, 它覆盖了 Object 类的相应方法. 可以看到, 在 println 里面使用 tempFirstList 里, 由于是用另一个字符串与其相加, 系统会自动调用 tempFirstList.toString().

通过观看别人的博客得知:顺序表是线性表的一种。线性表是一个可以在任意位置进行插入和删除数据元素操作的、由n(n>=0)个相同类型数据元素a0, a1, ···, ai, ai+1, ···, an-1组成的一个有限序列。顺序表就是按照顺序存储数据的线性表,用内存中一块地址连续的存储单元依次存储表中的数据元素。
 

代码输入:

package datastructure;


public class SequentialList {


	public static final int MAX_LENGTH = 10;

	int length;

	int[] data;

	public SequentialList() {
		length = 0;
		data = new int[MAX_LENGTH];
	}// Of the first constructor


	public SequentialList(int[] paraArray) {
		data = new int[MAX_LENGTH];
		length = paraArray.length;

		// Copy data.
		for (int i = 0; i < paraArray.length; i++) {
			data[i] = paraArray[i];
		} // Of for i
	}// Of the second constructor

	public String toString() {
		String resultString = "";

		if (length == 0) {
			return "empty";
		} // Of if

		for (int i = 0; i < length - 1; i++) {
			resultString += data[i] + ", ";
		} // Of for i

		resultString += data[length - 1];

		return resultString;
	}// Of toString

	public void reset() {
		length = 0;
	}// Of reset

	public static void main(String args[]) {
		int[] tempArray = { 1, 4, 6, 9 };
		SequentialList tempFirstList = new SequentialList(tempArray);
		System.out.println("Initialized, the list is: " + tempFirstList.toString());
		System.out.println("Again, the list is: " + tempFirstList);

		tempFirstList.reset();
		System.out.println("After reset, the list is: " + tempFirstList);
	}// Of main

}// Of class SequentialList

输出结果:

Initialized, the list is: 1, 4, 6, 9
Again, the list is: 1, 4, 6, 9
After reset, the list is: empty

java学习第十二天: 顺序表(二)

学习:

今天的代码直接在昨天的基础上增加. 只贴出新的部分.
12.1 查找给定元素所处的位置. 找不到就返回 -1.
12.2 在给定位置增加元素. 如果线性表已满, 或位置不在已有位置范围之内, 就拒绝增加. 该位置可以是在最后一个元素之后一个.
12.3 删除定定位置的元素. 要处理给定位置不合法的情况. 该位置必须是已经有数据的.
12.4 函数 要求同样的输入参数获得同样的输出结果, 但 方法 所依赖的数据既包括参数列表中给出的,也依赖于对象的成员变量. 因此, 面向对象所涉及的参数列表要短些. 例如, locate 方法就有效利用了 length 和 data 这两个成员变量.
 

代码输入:

package datastructure;


public class SequentialList {


	public static final int MAX_LENGTH = 10;

	int length;

	int[] data;

	public SequentialList() {
		length = 0;
		data = new int[MAX_LENGTH];
	}// Of the first constructor


	public SequentialList(int[] paraArray) {
		data = new int[MAX_LENGTH];
		length = paraArray.length;

		// Copy data.
		for (int i = 0; i < paraArray.length; i++) {
			data[i] = paraArray[i];
		} // Of for i
	}// Of the second constructor

	public String toString() {
		String resultString = "";

		if (length == 0) {
			return "empty";
		} // Of if

		for (int i = 0; i < length - 1; i++) {
			resultString += data[i] + ", ";
		} // Of for i

		resultString += data[length - 1];

		return resultString;
	}// Of toString

	public void reset() {
		length = 0;
	}// Of reset
	   public int locate(int paraValue) {
	   	int tempPosition = -1;

	   	for (int i = 0; i < length; i++) {
	   		if (data[i] == paraValue) {
	   			tempPosition = i;
	   			break;
	   		} // Of if
	   	} // Of for i

	   	return tempPosition;
	   }// Of locate

	   public boolean insert(int paraPosition, int paraValue) {
	   	if (length == MAX_LENGTH) {
	   		System.out.println("List full.");
	   		return false;
	   	}//Of if
	   	
	   	if ((paraPosition < 0) || (paraPosition > length)) {
	   		System.out.println("The position " + paraPosition + " is out of bounds.");
	   		return false;
	   	} // Of if

	   	// From tail to head.
	   	for (int i = length; i > paraPosition; i--) {
	   		data[i] = data[i - 1];
	   	} // Of for

	   	data[paraPosition] = paraValue;
	   	length++;

	   	return true;
	   }// Of insert

	   public boolean delete(int paraPosition) {
	   	if ((paraPosition < 0) || (paraPosition >= length)) {
	   		System.out.println("The position " + paraPosition + " is out of bounds.");
	   		return false;
	   	} // Of if

	   	// From head to tail.
	   	for (int i = paraPosition; i < length - 1; i++) {
	   		data[i] = data[i + 1];
	   	} // Of for

	   	length--;

	   	return true;
	   }// Of insert

	   public static void main(String args[]) {
	   	int[] tempArray = { 1, 4, 6, 9 };
	   	SequentialList tempFirstList = new SequentialList(tempArray);
	   	System.out.println("Initialized, the list is: " + tempFirstList.toString());
	   	System.out.println("Again, the list is: " + tempFirstList);

	   	int tempValue = 4;
	   	int tempPosition = tempFirstList.locate(tempValue);
	   	System.out.println("The position of " + tempValue + " is " + tempPosition);

	   	tempValue = 5;
	   	tempPosition = tempFirstList.locate(tempValue);
	   	System.out.println("The position of " + tempValue + " is " + tempPosition);

	   	tempPosition = 2;
	   	tempValue = 5;
	   	tempFirstList.insert(tempPosition, tempValue);
	   	System.out.println("After inserting " + tempValue + " to position " + tempPosition
	   			+ ", the list is: " + tempFirstList);

	   	tempPosition = 8;
	   	tempValue = 10;
	   	tempFirstList.insert(tempPosition, tempValue);
	   	System.out.println("After inserting " + tempValue + " to position " + tempPosition
	   			+ ", the list is: " + tempFirstList);

	   	tempPosition = 3;
	   	tempFirstList.delete(tempPosition);
	   	System.out.println("After deleting data at position " + tempPosition + ", the list is: "
	   			+ tempFirstList);

	   	for(int i = 0; i < 8; i ++) {
	   		tempFirstList.insert(i, i);
	   		System.out.println("After inserting " + i + " to position " + i
	   				+ ", the list is: " + tempFirstList);
	   	}//Of for i
	   	
	   	tempFirstList.reset();
	   	System.out.println("After reset, the list is: " + tempFirstList);
	   }// Of main
}

输出结果:

Initialized, the list is: 1, 4, 6, 9
Again, the list is: 1, 4, 6, 9
The position of 4 is 1
The position of 5 is -1
After inserting 5 to position 2, the list is: 1, 4, 5, 6, 9
The position 8 is out of bounds.
After inserting 10 to position 8, the list is: 1, 4, 5, 6, 9
After deleting data at position 3, the list is: 1, 4, 5, 9
After inserting 0 to position 0, the list is: 0, 1, 4, 5, 9
After inserting 1 to position 1, the list is: 0, 1, 1, 4, 5, 9
After inserting 2 to position 2, the list is: 0, 1, 2, 1, 4, 5, 9
After inserting 3 to position 3, the list is: 0, 1, 2, 3, 1, 4, 5, 9
After inserting 4 to position 4, the list is: 0, 1, 2, 3, 4, 1, 4, 5, 9
After inserting 5 to position 5, the list is: 0, 1, 2, 3, 4, 5, 1, 4, 5, 9
List full.
After inserting 6 to position 6, the list is: 0, 1, 2, 3, 4, 5, 1, 4, 5, 9
List full.
After inserting 7 to position 7, the list is: 0, 1, 2, 3, 4, 5, 1, 4, 5, 9
After reset, the list is: empty

java学习第十三天: 链表

学习:

13.1 支持与顺序表相同的操作: 初始化、插入、删除等.
13.2 为节点建一个类.
13.3 引用与指针的异同. 前者只能使用; 后者可以支持 p ++ 危险操作.
13.4 引用数据类型的赋值, 都不会产生新的对象空间.
13.5 链表与线性表在插入、删除时的不同: 前者不移动元素, 只改变引用 (指针).
13.6 今天的代码稍微多一点, 不过有昨天的铺垫, 还好.
13.7 第一个版本中, 128 和 164 行的 if 语句中都少了一个 .next, 导致对最后一个位置的检查出错 (空指针异常). 本 bug 由许嘉欣同学找到并修复
学习心得:链表跟顺序表差不多就是要节点连接,需要指针来连接。

代码输入:

package datastructure;



public class LinkedList {


	class Node {

		int data;

		Node next;


		public Node(int paraValue) {
			data = paraValue;
			next = null;
		}// Of the constructor
	}// Of class Node


	Node header;


	public LinkedList() {
		header = new Node(0);
		//header.next = null; //Redundant
	}// Of the first constructor

	public String toString() {
		String resultString = "";

		if (header.next == null) {
			return "empty";
		} // Of if

		Node tempNode = header.next;
		while (tempNode != null) {
			resultString += tempNode.data + ", ";
			tempNode = tempNode.next;
		} // Of while

		return resultString;
	}// Of toString


	public void reset() {
		header.next = null;
	}// Of reset


	public int locate(int paraValue) {
		int tempPosition = -1;

		Node tempNode = header.next;
		int tempCurrentPosition = 0;
		while (tempNode != null) {
			if (tempNode.data == paraValue) {
				tempPosition = tempCurrentPosition;
				break;
			} // Of if

			tempNode = tempNode.next;
			tempCurrentPosition++;
		} // Of while

		return tempPosition;
	}// Of locate


	public boolean insert(int paraPosition, int paraValue) {
		Node tempNode = header;
		Node tempNewNode;

		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next == null) {
				System.out.println("The position " + paraPosition + " is illegal.");
				return false;
			} // Of if

			tempNode = tempNode.next;
		} // Of for i

		// Construct a new node.
		tempNewNode = new Node(paraValue);

		// Now link them.
		tempNewNode.next = tempNode.next;
		tempNode.next = tempNewNode;

		return true;
	}// Of insert


	public boolean delete(int paraPosition) {
		if (header.next == null) {
			System.out.println("Cannot delete element from an empty list.");
			return false;
		} // Of if

		Node tempNode = header;

		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next.next == null) {
				System.out.println("The position " + paraPosition + " is illegal.");
				return false;
			} // Of if

			tempNode = tempNode.next;
		} // Of for i

		tempNode.next = tempNode.next.next;

		return true;
	}// Of delete


	public static void main(String args[]) {
		LinkedList tempFirstList = new LinkedList();
		System.out.println("Initialized, the list is: " + tempFirstList.toString());

		for (int i = 0; i < 5; i++) {
			tempFirstList.insert(0, i);
		} // Of for i
		System.out.println("Inserted, the list is: " + tempFirstList.toString());

		tempFirstList.insert(6, 9);

		tempFirstList.delete(4);

		tempFirstList.delete(2);
		System.out.println("Deleted, the list is: " + tempFirstList.toString());

		tempFirstList.delete(0);
		System.out.println("Deleted, the list is: " + tempFirstList.toString());

		for (int i = 0; i < 5; i++) {
			tempFirstList.delete(0);
			System.out.println("Looped delete, the list is: " + tempFirstList.toString());
		} // Of for i
	}// Of main
}// Of class LinkedList

输出结果:

Deleted, the list is: 4, 3, 1, 
Deleted, the list is: 3, 1, 
Looped delete, the list is: 1, 
Looped delete, the list is: empty
Cannot delete element from an empty list.
Looped delete, the list is: empty
Cannot delete element from an empty list.
Looped delete, the list is: empty
Cannot delete element from an empty list.
Looped delete, the list is: empty

java学习第十四天: 栈

学习:

14.1 push 和 pop 均只能在栈顶操作.
14.2 没有循环, 时间复杂度为 O ( 1 ) O(1)O(1).
 

通过学习得知栈的性质为先进后出,好比把一本本书在桌面上摞,从上面往下面取后放的书先取出来先放的书后取出来,push就是进栈操作,pop就是出栈操作。

代码输入:

package datastructure;


public class CharStack {

	public static final int MAX_DEPTH = 10;

	int depth;

	char[] data;


	public CharStack() {
		depth = 0;
		data = new char[MAX_DEPTH];
	}// Of the first constructor

	public String toString() {
		String resultString = "";
		for (int i = 0; i < depth; i++) {
			resultString += data[i];
		} // Of for i

		return resultString;
	}// Of toString

	public boolean push(char paraChar) {
		if (depth == MAX_DEPTH) {
			System.out.println("Stack full.");
			return false;
		} // Of if

		data[depth] = paraChar;
		depth++;

		return true;
	}// Of push


	public char pop() {
		if (depth == 0) {
			System.out.println("Nothing to pop.");
			return '\0';
		} // of pop

		char resultChar = data[depth - 1];
		depth--;

		return resultChar;
	}// Of pop


	public static void main(String args[]) {
		CharStack tempStack = new CharStack();

		for (char ch = 'a'; ch < 'm'; ch++) {
			tempStack.push(ch);
			System.out.println("The current stack is: " + tempStack);
		} // Of for i

		char tempChar;
		for (int i = 0; i < 12; i++) {
			tempChar = tempStack.pop();
			System.out.println("Poped: " + tempChar);
			System.out.println("The current stack is: " + tempStack);
		} // Of for i
	}// Of main
}// Of CharStack

输出结果:

The current stack is: a
The current stack is: ab
The current stack is: abc
The current stack is: abcd
The current stack is: abcde
The current stack is: abcdef
The current stack is: abcdefg
The current stack is: abcdefgh
The current stack is: abcdefghi
The current stack is: abcdefghij
Stack full.
The current stack is: abcdefghij
Stack full.
The current stack is: abcdefghij
Poped: j
The current stack is: abcdefghi
Poped: i
The current stack is: abcdefgh
Poped: h
The current stack is: abcdefg
Poped: g
The current stack is: abcdef
Poped: f
The current stack is: abcde
Poped: e
The current stack is: abcd
Poped: d
The current stack is: abc
Poped: c
The current stack is: ab
Poped: b
The current stack is: a
Poped: a
The current stack is: 
Nothing to pop.
Poped: 

java学习第十五天: 栈的应用(括号匹配)

学习:

任务描述: 检查一个字符串的括号是否匹配. 所谓匹配, 是指每个左括号有相应的一个右括号与之对应, 且左括号不可以出现在右括号右边. 可以修改测试字符串, 检查不同情况下的运行.
15.1 仅在昨天的代码基础上增加了一个 bracketMatching 方法, 以及 main 中的相应调拭语句.
15.2 操作系统的核心数据结构. 对于计算机而言, 如何降低时间、空间复杂度才是王道.
15.3 除了关注的括号, 其它字符不起任何作用.
15.4 一旦发现不匹配, 就直接返回, 不用罗嗦.
 

代码的大致内容就是遇到左括号就进栈,遇到一个有括号栈里面的左括号就出栈。

代码输入:

package datastructure;


public class CharStack {

	public static final int MAX_DEPTH = 10;

	int depth;

	char[] data;


	public CharStack() {
		depth = 0;
		data = new char[MAX_DEPTH];
	}// Of the first constructor
	public static boolean bracketMatching(String paraString) {
		// Step 1. Initialize the stack through pushing a '#' at the bottom.
		CharStack tempStack = new CharStack();
		tempStack.push('#');
		char tempChar, tempPopedChar;

		// Step 2. Process the string. For a string, length() is a method
		// instead of a member variable.
		for (int i = 0; i < paraString.length(); i++) {
			tempChar = paraString.charAt(i);

			switch (tempChar) {
			case '(':
			case '[':
			case '{':
				tempStack.push(tempChar);
				break;
			case ')':
				tempPopedChar = tempStack.pop();
				if (tempPopedChar != '(') {
					return false;
				} // Of if
				break;
			case ']':
				tempPopedChar = tempStack.pop();
				if (tempPopedChar != '[') {
					return false;
				} // Of if
				break;
			case '}':
				tempPopedChar = tempStack.pop();
				if (tempPopedChar != '{') {
					return false;
				} // Of if
				break;
			default:
				// Do nothing.
			}// Of switch
		} // Of for

		tempPopedChar = tempStack.pop();
		if (tempPopedChar != '#') {
			return false;
		} // Of if

		return true;
	}// Of bracketMatching

	public String toString() {
		String resultString = "";
		for (int i = 0; i < depth; i++) {
			resultString += data[i];
		} // Of for i

		return resultString;
	}// Of toString

	public boolean push(char paraChar) {
		if (depth == MAX_DEPTH) {
			System.out.println("Stack full.");
			return false;
		} // Of if

		data[depth] = paraChar;
		depth++;

		return true;
	}// Of push


	public char pop() {
		if (depth == 0) {
			System.out.println("Nothing to pop.");
			return '\0';
		} // of pop

		char resultChar = data[depth - 1];
		depth--;

		return resultChar;
	}// Of pop


	public static void main(String args[]) {
		CharStack tempStack = new CharStack();

		for (char ch = 'a'; ch < 'm'; ch++) {
			tempStack.push(ch);
			System.out.println("The current stack is: " + tempStack);
		} // Of for i

		char tempChar;
		for (int i = 0; i < 12; i++) {
			tempChar = tempStack.pop();
			System.out.println("Poped: " + tempChar);
			System.out.println("The current stack is: " + tempStack);
		} // Of for i

		boolean tempMatch;
		String tempExpression = "[2 + (1 - 3)] * 4";
		tempMatch = bracketMatching(tempExpression);
		System.out
				.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);

		tempExpression = "( )  )";
		tempMatch = bracketMatching(tempExpression);
		System.out
				.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);

		tempExpression = "()()(())";
		tempMatch = bracketMatching(tempExpression);
		System.out
				.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);

		tempExpression = "({}[])";
		tempMatch = bracketMatching(tempExpression);
		System.out
				.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);

		tempExpression = ")(";
		tempMatch = bracketMatching(tempExpression);
		System.out
				.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);
	}// Of main
}// Of CharStack

输出结果:

The current stack is: a
The current stack is: ab
The current stack is: abc
The current stack is: abcd
The current stack is: abcde
The current stack is: abcdef
The current stack is: abcdefg
The current stack is: abcdefgh
The current stack is: abcdefghi
The current stack is: abcdefghij
Stack full.
The current stack is: abcdefghij
Stack full.
The current stack is: abcdefghij
Poped: j
The current stack is: abcdefghi
Poped: i
The current stack is: abcdefgh
Poped: h
The current stack is: abcdefg
Poped: g
The current stack is: abcdef
Poped: f
The current stack is: abcde
Poped: e
The current stack is: abcd
Poped: d
The current stack is: abc
Poped: c
The current stack is: ab
Poped: b
The current stack is: a
Poped: a
The current stack is: 
Nothing to pop.
Poped: 

java学习第十六天:递归

学习:

任务描述: 检查一个字符串的括号是否匹配. 所谓匹配, 是指每个左括号有相应的一个右括号与之对应, 且左括号不可以出现在右括号右边. 可以修改测试字符串, 检查不同情况下的运行.
15.1 仅在昨天的代码基础上增加了一个 bracketMatching 方法, 以及 main 中的相应调拭语句.
15.2 操作系统的核心数据结构. 对于计算机而言, 如何降低时间、空间复杂度才是王道.
15.3 除了关注的括号, 其它字符不起任何作用.
15.4 一旦发现不匹配, 就直接返回, 不用罗嗦.
 

递归的大致意思就是自己调用自己,斐波那契数列实现你需要先知道斐波那契是个什么数列之后再把它用java打入编辑器里面,描述:斐波那契数列(Fibonacci sequence),指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……在数学上,斐波那契数列以如下被以递推的方法定义:F(0)=0,F(1)=1, F(n)=F(n-1)+F(n-2)(n ≥ 2,n ∈ N*)

代码输入:

package datastructure;

public class Recursion {

	public static int sumToN(int paraN) {
		if (paraN <= 0) {
			//Basis.
			return 0;
		} // Of if

		return sumToN(paraN - 1) + paraN;
	}// Of sumToN


	public static int fibonacci(int paraN) {
		if (paraN <= 0) {
			//Negative values are invalid. Index 0 corresponds to the first element 0.
			return 0;
		} if (paraN == 1) {
			//Basis.
			return 1;
		}//Of if
		
		return fibonacci(paraN - 1) + fibonacci(paraN - 2);
	}//Of fibonacci
	

	public static void main(String args[]) {
		int tempValue = 5;
		System.out.println("0 sum to " + tempValue + " = " + sumToN(tempValue));
		tempValue = -1;
		System.out.println("0 sum to " + tempValue + " = " + sumToN(tempValue));
		
		for(int i = 0; i < 10; i ++) {
			System.out.println("Fibonacci " + i + ": " + fibonacci(i));
		}//Of for i
	}// Of main
}// Of class Recursion

输出结果:

Fibonacci 1: 1
Fibonacci 2: 1
Fibonacci 3: 2
Fibonacci 4: 3
Fibonacci 5: 5
Fibonacci 6: 8
Fibonacci 7: 13
Fibonacci 8: 21
Fibonacci 9: 34

java学习第十七天: 链队列

学习:

17.1 链队列比较容易写.
17.2 Node 类以前是 LinkdedList 的内嵌类, 这里重写了一遍. 访问控制的事情以后再说.
17.3 为方便操作, 空队列也需要一个节点. 这和以前的链表同理. 头节点的引用 (指针) 称为 header.
17.4 入队仅操作尾部, 出队仅操作头部.
 

首先我们要知道什么是队列,队列就是只是允许在一短进行插入操作,而在另一端进行删除操作的线性表。队列是一种先进先出的线性表,简称fifo。

用链表表示的队列就简称为链队列,一个链队列显然需要两个分别指示队头和队尾的指针(分别称为头指针和尾指针)才能惟一确定。链队列只能尾进头出。

代码输入:

package datastructure.queue;


public class LinkedQueue {


	class Node {

		int data;


		Node next;


		public Node(int paraValue) {
			data = paraValue;
			next = null;
		}// Of the constructor
	}// Of class Node


	Node header;

	Node tail;


	public LinkedQueue() {
		header = new Node(-1);
		header.next = null;

		tail = header;
	}// Of the first constructor


	public void enqueue(int paraValue) {
		Node tempNode = new Node(paraValue);
		tail.next = tempNode;
		tail = tempNode;
	}// Of enqueue


	public int dequeue() {
		if (header.next == null) {
			System.out.println("No element in the queue");
			return -1;
		} // Of if

		int resultValue = header.next.data;

		header.next = header.next.next;

		return resultValue;
	}// Of dequeue


	public String toString() {
		String resultString = "";

		if (header.next == null) {
			return "empty";
		} // Of if

		Node tempNode = header.next;
		while (tempNode != null) {
			resultString += tempNode.data + ", ";
			tempNode = tempNode.next;
		} // Of while

		return resultString;
	}// Of toString


	public static void main(String args[]) {
		LinkedQueue tempQueue = new LinkedQueue();
		System.out.println("Initialized, the list is: " + tempQueue.toString());

		for (int i = 0; i < 5; i++) {
			tempQueue.enqueue(i + 1);
		} // Of for i
		System.out.println("Enqueue, the queue is: " + tempQueue.toString());

		tempQueue.dequeue();
		System.out.println("Dequeue, the queue is: " + tempQueue.toString());

		int tempValue;
		for (int i = 0; i < 5; i++) {
			tempValue = tempQueue.dequeue();
			System.out.println(
					"Looped delete " + tempValue + ", the new queue is: " + tempQueue.toString());
		} // Of for i
	}// Of main
}// Of class LinkedQueue

输出结果:

Initialized, the list is: empty
Enqueue, the queue is: 1, 2, 3, 4, 5, 
Dequeue, the queue is: 2, 3, 4, 5, 
Looped delete 2, the new queue is: 3, 4, 5, 
Looped delete 3, the new queue is: 4, 5, 
Looped delete 4, the new queue is: 5, 
Looped delete 5, the new queue is: empty
No element in the queue
Looped delete -1, the new queue is: empty

java学习第十八天:循环队列

学习:

18.1 整除的作用.
18.2 想像操场跑道里放一队人, 循环的感觉就出来了.
18.3 为了区分空队列与满队列, 需要留一个空间. 相当于不允许首尾相连. 还是画个图吧, 否则容易进坑.
18.4 用链式结构, 空间的分配与回收由系统做, 用循环队列, 则是自己把控. 想像自己写的是操作系统, 从这个代码可以感受下内存的管理.
 

今天的学习跟昨天的学习有相似之处,相似之处就是今天的内容头尾相接,头尾相接的顺序存储结构称为循环队列。

 

代码输入:

package datastructure.queue;


public class CircleCharQueue {


	public static final int TOTAL_SPACE = 10;


	char[] data;


	int head;


	int tail;


	public CircleCharQueue() {
		data = new char[TOTAL_SPACE];
		head = 0;
		tail = 0;
	}// Of the first constructor


	public void enqueue(char paraValue) {
		if ((tail + 1) % TOTAL_SPACE == head) {
			System.out.println("Queue full.");
			return;
		} // Of if

		data[tail % TOTAL_SPACE] = paraValue;
		tail++;
	}// Of enqueue


	public char dequeue() {
		if (head == tail) {
			System.out.println("No element in the queue");
			return '\0';
		} // Of if

		char resultValue = data[head];

		head++;

		return resultValue;
	}// Of dequeue


	public String toString() {
		String resultString = "";

		if (head == tail) {
			return "empty";
		} // Of if

		for (int i = head; i < tail; i++) {
			resultString += data[i % TOTAL_SPACE] + ", ";
		} // Of for i

		return resultString;
	}// Of toString


	public static void main(String args[]) {
		CircleCharQueue tempQueue = new CircleCharQueue();
		System.out.println("Initialized, the list is: " + tempQueue.toString());

		for (char i = '0'; i < '5'; i++) {
			tempQueue.enqueue(i);
		} // Of for i
		System.out.println("Enqueue, the queue is: " + tempQueue.toString());

		int tempValue = tempQueue.dequeue();
		System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());

		for (char i = 'a'; i < 'f'; i++) {
			tempQueue.enqueue(i);
			System.out.println("Enqueue, the queue is: " + tempQueue.toString());
		} // Of for i

		for (int i = 0; i < 3; i++) {
			tempValue = tempQueue.dequeue();
			System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
		} // Of for i

		for (char i = 'A'; i < 'F'; i++) {
			tempQueue.enqueue(i);
			System.out.println("Enqueue, the queue is: " + tempQueue.toString());
		} // Of for i
	}// Of main

}// Of CircleCharQueue

输出结果:

Initialized, the list is: empty
Enqueue, the queue is: 0, 1, 2, 3, 4, 
Dequeue 48, the queue is: 1, 2, 3, 4, 
Enqueue, the queue is: 1, 2, 3, 4, a, 
Enqueue, the queue is: 1, 2, 3, 4, a, b, 
Enqueue, the queue is: 1, 2, 3, 4, a, b, c, 
Enqueue, the queue is: 1, 2, 3, 4, a, b, c, d, 
Enqueue, the queue is: 1, 2, 3, 4, a, b, c, d, e, 
Dequeue 49, the queue is: 2, 3, 4, a, b, c, d, e, 
Dequeue 50, the queue is: 3, 4, a, b, c, d, e, 
Dequeue 51, the queue is: 4, a, b, c, d, e, 
Enqueue, the queue is: 4, a, b, c, d, e, A, 
Enqueue, the queue is: 4, a, b, c, d, e, A, B, 
Enqueue, the queue is: 4, a, b, c, d, e, A, B, C, 
Queue full.
Enqueue, the queue is: 4, a, b, c, d, e, A, B, C, 
Queue full.
Enqueue, the queue is: 4, a, b, c, d, e, A, B, C, 

java学习第十九天:字符串匹配

学习:

19.1 String 是 Java 常用的类, 这里重新实现下部分功能.
19.2 转义符 , 有了它才能正常打印引号.
19.3 简单的越界检查.

串(string):是由零个或多个字符组成的有限序列,又名字符串。通过看书得知串的链式存储结构跟线性表是相似的,但结构中每个元素数据是字符。

 

 

代码输入:

package datastructure;


public class MyString {

	public static final int MAX_LENGTH = 10;

	int length;


	char[] data;


	public MyString() {
		length = 0;
		data = new char[MAX_LENGTH];
	}// Of the first constructor


	public MyString(String paraString) {
		data = new char[MAX_LENGTH];
		length = paraString.length();

		// Copy data.
		for (int i = 0; i < length; i++) {
			data[i] = paraString.charAt(i);
		} // Of for i
	}// Of the second constructor


	public String toString() {
		String resultString = "";

		for (int i = 0; i < length; i++) {
			resultString += data[i];
		} // Of for i

		return resultString;
	}// Of toString


	public int locate(MyString paraMyString) {
		boolean tempMatch = false;
		for (int i = 0; i < length - paraMyString.length + 1; i++) {
			// Initialize.
			tempMatch = true;
			for (int j = 0; j < paraMyString.length; j++) {
				if (data[i + j] != paraMyString.data[j]) {
					tempMatch = false;
					break;
				} // Of if
			} // Of for j

			if (tempMatch) {
				return i;
			} // Of if
		} // Of for i
		return -1;
	}// Of locate


	public MyString substring(int paraStartPosition, int paraLength) {
		if (paraStartPosition + paraLength > length) {
			System.out.println("The bound is exceeded.");
			return null;
		} // Of if

		MyString resultMyString = new MyString();
		resultMyString.length = paraLength;
		for (int i = 0; i < paraLength; i++) {
			resultMyString.data[i] = data[paraStartPosition + i];
		} // Of for i

		return resultMyString;
	}// Of substring


	public static void main(String args[]) {
		MyString tempFirstString = new MyString("I like ik.");
		MyString tempSecondString = new MyString("ik");
		int tempPosition = tempFirstString.locate(tempSecondString);
		System.out.println("The position of \"" + tempSecondString + "\" in \"" + tempFirstString
				+ "\" is: " + tempPosition);

		MyString tempThirdString = new MyString("ki");
		tempPosition = tempFirstString.locate(tempThirdString);
		System.out.println("The position of \"" + tempThirdString + "\" in \"" + tempFirstString
				+ "\" is: " + tempPosition);

		tempThirdString = tempFirstString.substring(1, 2);
		System.out.println("The substring is: \"" + tempThirdString + "\"");

		tempThirdString = tempFirstString.substring(5, 5);
		System.out.println("The substring is: \"" + tempThirdString + "\"");

		tempThirdString = tempFirstString.substring(5, 6);
		System.out.println("The substring is: \"" + tempThirdString + "\"");
	}// Of main
}// Of class MyString

输出结果:

The position of "ik" in "I like ik." is: 3
The position of "ki" in "I like ik." is: -1
The substring is: " l"
The substring is: "e ik."
The bound is exceeded.
The substring is: "null"

java学习第二十天:综合任务 2

1、面向对象与面向过程相比, 有哪些优势? 注: 第 1 - 10 天的程序, 就是面向过程的.
面向对象有的时候比面向过程更快一些,面向对象先抽出对象再寻找面向对象的方式解决问题,面向过程就是拆成一个个方法再去解决问题。

2、比较线性表和链表的异同.
线性表和链表都都可以存储数据,但是线性表里面元素是连续的,链表里面不是。

3、分析线性表和链表的优缺点.
经常性的添加删除,长度不固定的话就用链表

4、分析调试程序常见的问题及解决方案.
括号分号的遗漏,初值忘记赋,赋值与定义搞混。

5、分析链队列与循环队列的优缺点.

在可以确定队列长度最大值的情况下,建议用循环队列,如果你无法预估队列的长度时,则用链队列。

6、第 18 天建立的两个队列, 其区别仅在于基础数据不同, 一个是 int, 一个是 char. 按这种思路, 对于不同的基础数据类型, 都需要重写一个类, 这样合理吗? 你想怎么样?
不合理 这样就太麻烦了 可以提前声明
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值