`

数据结构-----基本结构

阅读更多

public class MyArray {
	private int[] array;
	private int lastIndex = -1;
	private int length;
	
	public MyArray(){
		array = new int[50];
		this.length = 50;
	}
	
	public MyArray(int length){
		array = new int[length];
		this.length = length;
		lastIndex = length-1;
	}
	
	/*
	 * 按索引顺序加入
	 */
	public void add(int value){
		if(isEnded())throw new ArrayIndexOutOfBoundsException();
		lastIndex++;
		array[lastIndex] = value;
		
	}
	
	/**
	 * 有序插入,从小到大
	 * @param value
	 */
	public void add2(int value){
		int i ;
		if(isEnded())throw new ArrayIndexOutOfBoundsException();
		for(i=0;i<=lastIndex;i++){
			if(value<array[i]){
				for(int j=lastIndex;j>=i ;j--){
					array[j+1]=array[j];
				}
				array[i] = value;
				break;
			}
		}
		if(i>lastIndex)array[i]=value;
		lastIndex ++;
		
		
	}
	
	public int valueOf(int index){
		if(isEnded(index)) return -1;
		
		return array[index];
	}
	public void remove(int  index){
		if(isOutOfBoundary(index)) throw new ArrayIndexOutOfBoundsException();
		else if(!isEnded(index)){
			for(int i=index; i<=lastIndex;i++){
				array[i] = array[i+1];
			}
		}
		lastIndex --;
	}
	
	/**
	 * 线性查找
	 * @param value
	 * @return
	 */
	public long indexOf(int value){
		for(int i=0;i<=lastIndex;i++){
			if(value == array[i])return i;
		}
		return -1;
	}
	
	/*
	 * 二分法查找:前提:有序数组
	 * 
	 */
	public int binarySearch(int value){
		int low = 0;
		int hight = lastIndex;
		int middle ;
		while(true){
			middle = (low + hight)/2;
			if(low>hight)return -1;
			else if(value>array[middle] ){
				low = middle+1;
				
			}else if(value<array[middle]){
				hight = middle -1;
			}else if(value == array[middle] )return middle;
		}
	}
	
	/**
	 * 冒泡排序
	 */
	public void bubbleSort(int value){
		
		for(int i=0;i<lastIndex;i++){//i表示趟数,因为每次上升一个泡泡
			//j是扫描指针,扫一个就交换一次,所以他正好也记录的当前最小的
			for(int j=lastIndex;j>i;j--){ 
				
				bubble(array[j],array[j-1]);
				
			}
		}
	}
	
	
	private void bubble(int bubble1, int bubble2) {
		if(bubble1 < bubble2){
			int temp = bubble1;
			bubble1 = bubble2;
			bubble1 = temp;
		}
		
	}

	public String toString(){
		String arrStr = "[";
		for(int i=0;i<lastIndex;i++){
			arrStr += array[i]+",";
		}
		
		arrStr +=array[lastIndex]+"]";
		return arrStr;
	}
	
	public boolean isEnded(){
		if(length == lastIndex + 1)return true;
		return false;
	}
	public boolean isEnded(int index){
		if(length == index + 1)return true;
		return false;
	}
	public boolean isOutOfBoundary(int index){
		if(index <0 || index >lastIndex)return true;
		else return false;
	}
}



LinkedList

package com.zhe.arr;

public class LinkedList {
	Node first; //头结点,里面的data域是空值
	
	public LinkedList(){
		first = new Node();
	}
	
	public void add2First(int value){
		Node node = new Node(value);
		node.next = first.next;
		first.next = node;
	}
	
	public Node removeFromFirst(){
		Node node = first.next;
		first.next = node.next;
		return node;
	}
	
	public Node find(int data){
		Node current = first.next;
		while(current != null ){
			if(current.data == data)break;
			current = current.next;
		}
		return current;
	}
	
	public void display(){
		Node current = first.next;
		while(current != null){
			System.out.print(current);
			current = current.next;
		}
	}
}


Info
package com.zhe.arr;
/**
 * 员工信息类
 * @author Administrator
 *
 */
public class Info {
	private String key;
	private String value;
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
}

package com.zhe.arr;

public class Node {
	int data;
	Node next;
	
	public Node(){
		
	}
	
	public Node(int data){
		this.data = data;
	}
	
	public String toString(){
		return data + " ";
	}
}


package com.zhe.arr;

import java.math.BigInteger;

/**
 * hashTable就是根据对象的某个值算出在数组中的位置,从而快速定位对象所在的地方
 * @author Administrator
 *
 */
public class HashTable {
	private Info[] arr;
	private int elements;
	public HashTable(){
		arr = new Info[100];
		elements = 0;
	}
	
	public HashTable(int length){
		arr = new Info[length];
		elements = 0;
	}

	public void insert(Info info){
		if(elements == arr.length)return;
		if(arr[hashCode(info.getKey())] != null ){
			int i=0;
			while(arr[hashCode(info.getKey())+i]!= null){
				if(i+1 == arr.length)i=0;
				else i++;
			}
			arr[hashCode(info.getKey())+i] = info;
			elements ++;
		}
	}
	
	public int hashCode(String key) {
		//方法   1*27 + 2*27 + 。。。。
		BigInteger hasVal = new BigInteger("0");
		for(int i = 0 ;i<key.length();i++){
			hasVal = new BigInteger(String.valueOf(key.charAt(i) - 96))
			            .multiply(new BigInteger("27"))
			            .multiply(new BigInteger(String.valueOf(i+1)));
		}
		return hasVal.mod(new BigInteger(String.valueOf(arr.length))).intValue();
	}
	
	
}


package com.zhe.arr;
/**
 * 这里的队列使用循环队列
 * @author Administrator
 *
 */
public class MyQueue {
	
	private long[] arr;
	int first = -1; //指向第一个元素的前一个空
	int end = -1;  //由初始化可知 first == end 成立时是空队列
	
	public MyQueue(){
		arr = new long[10];
	}
	
	public MyQueue(int length){
		arr = new long[length];
	}
	
	public void push(long value){
		end++;
		if(end == arr.length)end = 0;
		arr[end] = value;
	}
	
	public Long remove(){
		if(isEmpty())return null;
		first ++;
		if(first == arr.length) first = 0;
		return arr[first];
	}

	private boolean isEmpty() {
		return end == first;
	}
	
}

分享到:
评论

相关推荐

    数据结构-算法

    顺序表是一种基本的数据结构,它通过连续的内存空间来存储数据元素。在本例中,顺序表被定义为一个结构体`SeqList`,其包含两个成员: - `DataTypedata[ListSize]`:用于存储数据元素的数组。 - `intlength`:表示...

    数据结构---C语言描述-(耿国华)-高等教育出版社出版-课后习题答案

    在本资源中,我们将从数据结构的基本概念开始,逐步深入到线性表、栈、队列、树形结构、图状结构等复杂的数据结构中。每种数据结构都有其特点和应用场景,我们将通过实例和习题来帮助读者更好地理解和掌握这些知识点...

    图解数据结构--使用Java

    全书内容浅显易懂,利用大量且丰富的图示与范例, 详解复杂的抽象理论,从最基本的数据结构概念开始 说明,再以Java工具加以诠释阵列结构、堆栈、链表 、队列、排序、查找等重要的概念,引领读者抓住重 点轻松进入...

    山东大学2019年-数据结构-期末试卷真题.pdf

    根据提供的文件信息,本文将基于“山东大学2019年-数据结构-期末试卷真题”这一主题展开,深入探讨数据结构领域的核心知识点及其在实际考试中的应用。 ### 数据结构概览 数据结构是计算机科学的一个核心概念,它...

    数据结构--顺序栈的基本运算.pdf

    数据结构--顺序栈的基本运算.pdf

    数据结构-基本算法-静态链表

    数据结构-基本算法-静态链表(学生时代源码,调试可运行)

    湖南大学-数据结构-期末试题【2016-2019】.zip

    数据结构是计算机科学中的核心课程,它探讨了如何在计算机中高效地组织和管理数据,以便于进行各种操作。湖南大学的数据结构期末试题涵盖了这门课程的重要概念和技术,旨在检验学生对这些知识的理解和应用能力。以下...

    数据结构-基本算法-哈夫曼编译码器

    数据结构-基本算法-哈夫曼编译码器(学生时代源码,调试可运行)

    数据结构-基本算法-顺序栈

    数据结构-基本算法-顺序栈(学生时代源码,调试可运行)

    数据结构--C语言描述-(耿国华)课后习题答案

    该资源是数据结构的课后习题答案,涵盖了数据结构的基本概念、线性表、栈、队列、树、图等数据结构的实现和操作。下面是该资源的详细知识点: 一、基本概念 * 数据抽象:数据结构的基本概念,强调对数据的描述和...

    数据结构-抽象数据类型-树

    数据结构是计算机科学中的核心概念,它涉及到如何高效地存储和组织数据,以便于执行各种操作。在数据结构中,树是一种非线性的数据结构,它模拟了自然界中树的概念,具有分层和父子关系的特点。在本压缩包中,我们...

    数据结构---C语言描述-(耿国华)-高等教育出版社出版-课后习题答案.pdf

    耿国华教授编写的《数据结构---C语言描述》一书,结合C语言的特点,系统地介绍了数据结构的基本概念和常用算法,是高等教育出版社出版的一本经典教材。通过该书的学习,读者能够深入理解数据结构的基本原理,并掌握...

    数据结构-----------课件

    数组是最基本的数据结构,提供了随机访问元素的能力,但插入和删除操作较为困难。链表则解决了这个问题,允许在任意位置插入和删除元素,但访问速度较慢。栈是一种后进先出(LIFO)的数据结构,常用于表达式求值和...

    数据结构-基本算法-无向图存储转换

    数据结构-基本算法-无向图存储转换(学生时代源码,调试可运行)

    数据结构\数据结构-CH1绪论

    ### 数据结构概述与基本概念 #### 一、数据结构的重要性 数据结构是计算机科学的核心领域之一,它专注于非数值计算的程序设计问题中,研究如何有效地组织和存储数据,以便于进行高效的数据处理和信息检索。正如...

    数据结构-基本算法-不带头结点的循环链表

    数据结构-基本算法-不带头结点的循环链表(学生时代源码,调试可运行)

    python-数据结构-书.docx

    数据结构基础知识:介绍数据结构的基本概念、分类、特点等内容,包括数组、链表、栈、队列、树、图等。 2. Python数据结构:介绍Python中常用的数据结构,包括列表、元组、字典、集合等,以及它们的特点、使用方法...

Global site tag (gtag.js) - Google Analytics