day 8

循环队列

package day10;

public class CircleIntQueue {

	public static final int TOTAL_SPACE = 10;
	
	int[] data;
	int head;
	int tail;
	
	public CircleIntQueue() {
		data = new int[TOTAL_SPACE];
		head = 0;
		tail = 0;
	}// Of the first constructor
	
	public void enqueue(int paraValue) {
		if((tail + 1) % TOTAL_SPACE == head) {
			System.out.println("Queue full.");
			return;
		} // Of if
		data[tail % TOTAL_SPACE] = paraValue;
		tail ++;
	}// Of enqueue
	
	public int dequeue() {
		if(head == tail) {
			System.out.println("No element in the queue");
			return -1;
		} // Of if
		
		int resultValue = data[head % TOTAL_SPACE];
		
		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) {
		CircleIntQueue tempQueue = new CircleIntQueue();
		System.out.println("Initialized, the list is: " + tempQueue.toString());
		
		for(int 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(int i = 0; i < 6; i++) {
			tempQueue.enqueue(i + 10);
			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 (int i = 0; i < 6; i++) {
			tempQueue.enqueue(i + 100);
			System.out.println("Enqueue, the queue is: " + tempQueue.toString());
		}// Of for i
	}// Of main

}// Of CircleIntQueue

运行截图:

 

 由于后面还要用到字符队列, 就把上面这个程序中的 int 改成 char, 并作相应调整, 获得如下程序.

package day10;

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 % TOTAL_SPACE];
		
		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());

		char 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

运行截图:

 字符串匹配:
 

package day10;

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();
		
		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++) {
			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

运行截图:

1.面向对象与面向过程相比, 有哪些优势? 注: 第 1 - 10 天的程序, 就是面向过程的.

面向过程就像是分析出解决问题要几步,再用函数一步一步实现,使用时一步步调用。

面向对象就像把事务分解成不同的对象。

有听说过一个很容易理解的例子,面向过程就像蛋炒饭,面向对象就像盖浇饭,你可能不爱吃鸡蛋,那面向过程的蛋炒饭就得重新来过,面向对象就可以直接给你换一份上面的盖的菜。

两者各有所长,但是面向对象它更灵活,喜欢红烧肉就把饭留下并且把盖的菜给你换成红烧肉,可维护性好。

2.比较顺序表和链表的异同.3.分析顺序表和链表的优缺点.

当时学习数据结构的时候也着重看过这一点。

对于单链表来说,首先:空间上顺序表在空间上必须用一段连续的存储单元依次存储数据,但是链表可以用任意的存储单元存放元素,每个元素都有一个指针指向下一个元素的位置,这也使得链表不需要一整段连续的空间。

第二:链表中的每个数据元素都有指向下一个元素的指针,因此链表的空间利用率其实并没有顺序表高。

第三:顺序表在存储数据之前必须先开辟好存储空间。但是链表存储数据时一次只开辟存储一个节点的空间,如果需要还可以再申请。

第四:顺序表可以实现随机存取,但是链表只能实现顺序存取。

第五:顺序表的插入和删除操作会对进行操作的元素后的所有元素都产生位置上的影响,即需要移动元素,但是链表在插入删除操作时并没有这种情况 。

总的来说,我个人认为,平时如果表中的数据需要频繁的插入或者删除,那么可能顺序表并不是最好的选择;但如果经常需要查找任意位置的数据,那么顺序表会更合适。



4.分析调拭程序常见的问题及解决方案.

调拭程序真的是很正常很正常的问题,之前使用Dev-C++的时候,有时候程序出现某些错误,虽然仍然会跳出运行成功后的黑框,但是真的只是一个黑框,在这段程序里我就算开头有一句最简单的输出都不会出结果,这种真的最让我烦恼,因为我的程序它确确实实有bug,但是又没有错误语句的提示,只能一句一句再重新理顺逻辑。 

有时候也会出现能正常弹出黑框,上半段代码能正常输出,但是后半段的却不能,可以在某个语句处打个断点,不过我自己也会用一句很简单的输出"hi"的语句,放在不能正确出结果的那部分代码的某一句后面,如果放在某句后面"hi"能正确输出,那么这句就没什么问题,若是不能,那最好还是看看这句话问题在哪。

最近的编程中,我个人觉得我最容易出现问题的地方其实是数据结构的那部分内容,数据结构这部分内容在写代码时必须要聚精会神,有时候出问题后真的要再次理一遍自己的代码逻辑。不过代码调多了有时候问题一出现,大概也能意识到是大致哪块代码的问题。


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

首先,两者的出队和入队都很方便,不过循环队列必须有一个固定的长度,所以存储空间有一定的限制,相对来说链队列就没有这个限制,不存在溢出的问题。

循环队列判断队列是否已满比较困难,在之前的代码中我们使用的是舍去一个空间来判断,这样也是浪费了一个空间,有时候也可以用一个常量计队列内的元素个数,以此判断是否已满。

总的来说,如果可以确定队列最长是多少的时候,可以使用循环队列,当无法预估队列的长度时,链队列会更好。


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

其实最开始看到这两个队列的时候,我觉得这两段代码相似度也太高了,只是说一个是 int类型, 一个是 char类型,但是如果要把它们放在一段代码里的话也有点太多了吧,我有想过这块能不能用继承,但是也确实离之前学习java已经很长一段时间没复习了,已经有些模糊了,也对java理解不深,之后会认真复习这部分的内容。

二叉树的深度遍历的递归实现:

package day20;

public class BinaryCharTree {

	char value;
	BinaryCharTree leftChild;
	BinaryCharTree rightChild;
	
	public BinaryCharTree (char paraName) {
		value = paraName;
		leftChild = null;
		rightChild = null;
	}// Of the constructor
	
	public static BinaryCharTree manualConstructTree() {
		BinaryCharTree resultTree = new BinaryCharTree('a');
		BinaryCharTree tempTreeB = new BinaryCharTree('b');
		BinaryCharTree tempTreeC = new BinaryCharTree('c');
		BinaryCharTree tempTreeD = new BinaryCharTree('d');
		BinaryCharTree tempTreeE = new BinaryCharTree('e');
		BinaryCharTree tempTreeF = new BinaryCharTree('f');
		BinaryCharTree tempTreeG = new BinaryCharTree('g');
		
		resultTree.leftChild = tempTreeB;
		resultTree.rightChild = tempTreeC;
		tempTreeB.rightChild = tempTreeD;
		tempTreeC.leftChild = tempTreeE;
		tempTreeD.leftChild = tempTreeF;
		tempTreeD.rightChild = tempTreeG;
		return resultTree;
	}// Of manualConstructTree
	
	public void preOrderVisit() {
		System.out.print("" + value + " ");

		if (leftChild != null) {
			leftChild.preOrderVisit();
		} // Of if
		
		if (rightChild != null) {
			rightChild.preOrderVisit();
		} // Of if
	}// Of preOrderVisit
	
	public void inOrderVisit() {
		if (leftChild != null) {
			leftChild.inOrderVisit();
		} // Of if

		System.out.print("" + value + " ");

		if (rightChild != null) {
			rightChild.inOrderVisit();
		} // Of if
	}// Of inOrderVisit

	public void postOrderVisit() {
		if (leftChild != null) {
			leftChild.postOrderVisit();
		} // Of if

		if (rightChild != null) {
			rightChild.postOrderVisit();
		} // Of if

		System.out.print("" + value + " ");
	}// Of postOrderVisit

	public int getDepth() {
		if((leftChild == null) && (rightChild == null)) {
			return 1;
		}// Of if
		
		int tempLeftDepth = 0;
		if(leftChild != null) {
			tempLeftDepth = leftChild.getDepth();
		}// Of if
		
		int tempRightDepth = 0;
		if (rightChild != null) {
			tempRightDepth = rightChild.getDepth();
		} // Of if
		
		if (tempLeftDepth >= tempRightDepth) {
			return tempLeftDepth + 1;
		} else {
			return tempRightDepth + 1;
		} // Of if
	}// Of getDepth
	
	public int getNumNodes() {
		if((leftChild == null) && (rightChild == null)) {
			return 1;
		}// Of if
		
		int tempLeftNodes = 0;
		if(leftChild != null) {
			tempLeftNodes = leftChild.getNumNodes();
		} // Of if
		
		int tempRightNodes = 0;
		if(rightChild != null) {
			tempRightNodes = rightChild.getNumNodes();
		} // Of if
		
		return tempLeftNodes + tempRightNodes + 1;
	}
	public static void main(String[] args) {
		
		BinaryCharTree tempTree = manualConstructTree();
		System.out.println("\r\nPreorder visit:");
		tempTree.preOrderVisit();
		System.out.println("\r\nIn-order visit:");
		tempTree.inOrderVisit();
		System.out.println("\r\nPost-order visit:");
		tempTree.postOrderVisit();

		System.out.println("\r\n\r\nThe depth is: " + tempTree.getDepth());
		System.out.println("The number of nodes is: " + tempTree.getNumNodes());
	}// Of main
}// Of BinaryCharTree

运行截图: 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值