数组队列
1.为什么要使用数组队列?
数组相当于是一个容器,可以存放多个相同类型的数据。
优点:有序性,清晰,可以快速地查找数据;具有连续的存储空间
缺点:在定义的时候数组长度已经固定,不可改变。例如在画板存储图形的时候,如果数组长度太小,会造成画了一定的图形之后,没有存储空间。如果数组长度太大,会造成存储空间的浪费。
2.数组队列的实现
数据类型 [ ] 数组名 = new 数据类型 [数组长度]
对象名中存储什么?对象在内存中的首地址。
数组名中存储什么?数组对象在内存中的首地址。
数组队列是通过改变数组的首地址来改变数组的长度和内容
3.数组队列实现步骤
1.定义一个接口,在接口中定义需要实现的抽象方法
2.定义一个类来实现接口,重写接口中的方法
3.实现方法
4.注意事项
1.首先要注意的是在实现抽象方法,比如移除、添加等方法的时候,注意数组队列长度的变化
2.一定要让原数组指向暂时存储数据的数组
//主函数 public class Manager { public static void main(String[] args) { System.out.println("开始测试"); MyList ml1 = new MyListImpl(); MyList ml2 = new MyListImpl(); for (int i = 0; i < 3; i++) { ml2.add(new Student("m2学生" + i, i)); System.out.println("m2学生" + i + "学号" + i); } // System.out.println("这是ml1"); for (int i = 0; i < 2; i++) { ml1.add(new Student("学生" + i, i)); System.out.println("学生" + i + "学号" + i); } Student stu = new Student("学生1", 1); // 添加新元素 ml1.add(stu); System.out.println("给ml1加上stu"); for (int i = 0; i < ml1.size(); i++) { Student student = ml1.get(i); System.out.println("学生是" + student.getName()); } // 在指定位置添加一个新元素 ml1.add(1, stu); System.out.println("给ml1在指定位置1添加一个新元素加上stu"); for (int i = 0; i < ml1.size(); i++) { Student student = ml1.get(i); System.out.println("学生是" + student.getName()); } // 将数组队列ml2中的数据添加到当前数组队列的末尾 ml1.add(ml2); System.out.println("将数组队列ml2中的数据添加到当前数组队列的末尾"); for (int i = 0; i < ml1.size(); i++) { Student student = ml1.get(i); System.out.println("学生是" + student.getName()); } // 在指定位置2将参数数组队列ml2中的元素添加到该位置 ml1.add(2, ml2); System.out.println("在指定位置2将参数数组队列ml2中的元素添加到该位置"); for (int i = 0; i < ml1.size(); i++) { Student student = ml1.get(i); System.out.println("学生是" + student.getName()); } // 移除指定索引位置的元素 ml1.remove(2); System.out.println("移除指定索引位置2的元素"); for (int i = 0; i < ml1.size(); i++) { Student student = ml1.get(i); System.out.println("学生是" + student.getName()); } // 移除指定的元素对象 ml1.remove(stu); System.out.println("移除指定的元素对象"); for (int i = 0; i < ml1.size(); i++) { Student student = ml1.get(i); System.out.println("学生是" + student.getName()); } // 移除mal数组队列中的元素 ml1.remove(ml2); System.out.println("移除ml2数组队列中的元素"); for (int i = 0; i < ml1.size(); i++) { Student student = ml1.get(i); System.out.println("学生是" + student.getName()); } // 修改指定索引位置1的元素,用stu来代替 ml1.set(1, stu); System.out.println("修改指定索引位置1的元素,用stu来代替"); for (int i = 0; i < ml1.size(); i++) { Student student = ml1.get(i); System.out.println("学生是" + student.getName()); } // 获取指定索引位置的元素 ml1.get(0); System.out.println("获取指定索引位置的元素"); for (int i = 0; i < ml1.size(); i++) { Student student = ml1.get(i); System.out.println("学生是" + student.getName()); } // 返回数组队列中首次出现的指定元素的索引 System.out.println("返回数组队列中首次出现的指定元素的索引" + ml1.indexOf(stu)); // 判断数组队列中是否有元素 System.out.println("判断数组队列中是否有元素" + ml1.isEmpty()); // 返回此列表中最后一次出现的指定元素的索引 System.out.println("返回此列表中最后一次出现的指定元素的索引" + ml1.lastIndexOf(stu)); // 清除数组队列中所有的元素 ml1.clear(); System.out.println("清除数组队列中所有的元素"); } }
//接口 public interface MyList { /** * 添加新元素的方法 * @param stu是新元素 */ public void add(Student stu); /** * 在指定位置添加一个新元素 * @param index指定的索引位置 * @param stu要添加的新元素 * @return 返回true表示添加成功,返回false表示添加失败 */ public boolean add(int index ,Student stu); /** * 将数组队列mal中的数据添加到当前数组队列的末尾 * @param mal要被添加的数组队列 */ public void add(MyList mal); /** * 在指定位置将参数数组队列中的元素添加到该位置 * @param index指定的索引位置 * @param mal要被添加的数组队列 * @return 返回true表示添加成功,返回false表示添加失败 */ public boolean add(int index,MyList mal); /** * 移除指定索引位置的元素 * @param index指定的索引位置 * @return 返回被移除的对象,如果返回Null则表示index的索引不存在 */ public Student remove(int index); /** * 移除指定的元素对象 * @param stu要被移除的元素 * @return 返回true表示移除成功,返回false表示移除失败 */ public boolean remove(Student stu); /** * 移除mal数组队列中的元素 * @param mal要被移除的数组队列 * @return 返回true表示移除成功,返回false表示移除失败 */ public boolean remove(MyList mal); /** * 修改指定索引位置的元素,用stu来代替 * @param index指定的索引位置 * @param stu要替换的新元素 * @return 返回被替换的对象,如果返回Null则表示index的索引不存在 */ public Student set(int index,Student stu); /** * 获取指定索引位置的元素 * @param index指定的索引位置 * @return 返回索引位置的对象,如果返回Null则表示index的索引不存在 */ public Student get(int index); /** * 返回数组队列中存储的元素总数 * @return 返回元素总数 */ public int size(); /** * 清除数组队列中所有的元素 */ public void clear(); /** * 返回数组队列中首次出现的指定元素的索引,或如果此数组队列不包含元素,则返回 -1。 * @param stu要查找的元素 * @return 返回元素的索引,如果返回-1表示元素不存在 */ public int indexOf(Student stu); /** * 判断数组队列中是否有元素 * @return 返回true表示没有元素是空的;返回false表示非空 */ public boolean isEmpty(); /** * 返回此列表中最后一次出现的指定元素的索引,或如果此列表不包含索引,则返回 -1。 * @param stu要查找的元素 * @return 返回元素的索引,如果返回-1表示元素不存在 */ public int lastIndexOf(Student stu); }
//实现接口的类 public class MyListImpl implements MyList { private Student[] array = new Student[0]; public MyListImpl() { } /** * 添加新元素的方法 * * @param stu是新元素 */ public void add(Student stu) { Student[] temp = new Student[array.length + 1]; for (int i = 0; i < array.length; i++) { temp[i] = array[i]; } temp[array.length] = stu; array = temp; } /** * 在指定位置添加一个新元素 * * @param index指定的索引位置 * @param stu要添加的新元素 * @return 返回true表示添加成功,返回false表示添加失败 */ public boolean add(int index, Student stu) { Student[] temp = new Student[array.length + 1]; if (index < array.length) { for (int i = 0; i < index; i++) { temp[i] = array[i]; } temp[index] = stu; for (int i = index; i < array.length; i++) { temp[i + 1] = array[i]; } array = temp; return true; } else { return false; } } /** * 将数组队列mal中的数据添加到当前数组队列的末尾 * * @param mal要被添加的数组队列 */ public void add(MyList dest) { // 是原长加上目标长 Student[] temp = new Student[array.length + dest.size()]; // copy src for (int i = 0; i < array.length; i++) { temp[i] = array[i]; } for (int i = array.length; i < array.length + dest.size(); i++) { temp[i] = dest.get(i - array.length); } array = temp; } /** * 在指定位置将参数数组队列中的元素添加到该位置 * * @param index指定的索引位置 * @param mal要被添加的数组队列 * @return 返回true表示添加成功,返回false表示添加失败 */ public boolean add(int index, MyList mal) { Student[] temp = new Student[array.length + mal.size()]; if (index < array.length) { for (int i = 0; i < index; i++) { temp[i] = array[i]; } for (int i = index, j = 0; i < array.length + mal.size(); i++, j++) { temp[i] = mal.get(j); } for (int i = index + mal.size(), j = index; i < array.length + mal.size(); j++, i++) { temp[i] = array[j]; } array = temp; return true; } else { return false; } } /** * 移除指定索引位置的元素 * * @param index指定的索引位置 * @return 返回被移除的对象,如果返回Null则表示index的索引不存在 */ public Student remove(int index) { Student[] temp = new Student[array.length - 1]; if (index < array.length) { for (int i = 0; i < index; i++) { temp[i] = array[i]; } for (int i = index; i < array.length - 1; i++) { temp[i] = array[i + 1]; } Student indexdata = array[index]; array = temp; return indexdata; } else { return null; } } /** * 移除指定的元素对象 * * @param stu要被移除的元素 * @return 返回true表示移除成功,返回false表示移除失败 */ public boolean remove(Student stu) { int index = -1; for (int j = 0; j < array.length; j++) { if (array[j].equals(stu)) { index = j; Student[] temp = new Student[array.length - 1]; if (index < array.length) { for (int i = 0; i < index; i++) { temp[i] = array[i]; } for (int i = index + 1; i < array.length; i++) { temp[i - 1] = array[i]; } array = temp; } } } if (index >= 0 && index < array.length) { return true; } else { return false; } } /** * 移除mal数组队列中的元素 * * @param mal要被移除的数组队列 * @return 返回true表示移除成功,返回false表示移除失败 */ public boolean remove(MyList mal) { Student[] temp = new Student[array.length - mal.size()]; int index = -1; for (int i = 0; i < array.length; i++) { if (array[i].equals(mal.get(i))) { index = i; } } for (int j = 0; j < index; j++) { temp[j] = array[j]; } for (int j = index; j < array.length - mal.size(); j++) { temp[j] = array[j + mal.size()]; } array = temp; if (index > 0 && index < array.length) { return true; } else { return false; } } /** * 修改指定索引位置的元素,用stu来代替 * * @param index指定的索引位置 * @param stu要替换的新元素 * @return 返回被替换的对象,如果返回Null则表示index的索引不存在 */ public Student set(int index, Student stu) { Student[] temp = new Student[array.length]; if (index < array.length) { for (int i = 0; i < index; i++) { temp[i] = array[i]; } temp[index] = stu; for (int i = index + 1; i < array.length; i++) { temp[i] = array[i - 1]; } array = temp; return stu; } else { return null; } } /** * 获取指定索引位置的元素 * * @param index指定的索引位置 * @return 返回索引位置的对象,如果返回Null则表示index的索引不存在 */ public Student get(int index) { if (index < array.length) { return array[index]; } else { return null; } } /** * 返回数组队列中存储的元素总数 * * @return 返回元素总数 */ public int size() { return array.length; } /** * 清除数组队列中所有的元素 */ public void clear() { for (int i = 0; i < array.length; i++) { array[i] = null; } } /** * 返回数组队列中首次出现的指定元素的索引,或如果此数组队列不包含元素,则返回 -1。 * * @param stu要查找的元素 * @return 返回元素的索引,如果返回-1表示元素不存在 */ public int indexOf(Student stu) { int index = -1; for (int j = 0; j < array.length; j++) { if (array[j].equals(stu)) { index = j; break; } } return index; } /** * 判断数组队列中是否有元素 * * @return 返回true表示没有元素是空的;返回false表示非空 */ public boolean isEmpty() { if (array.length != 0) { return true; } return false; } /** * 返回此列表中最后一次出现的指定元素的索引,或如果此列表不包含索引,则返回 -1。 * * @param stu要查找的元素 * @return 返回元素的索引,如果返回-1表示元素不存在 */ public int lastIndexOf(Student stu) { int index = -1; for (int j = array.length - 1; j >= 0; j--) { if (array[j].equals(stu)) { index = j; break; } } return index; } }
<!--EndFragment-->
相关推荐
相比于传统的队列,循环队列利用数组的循环特性,避免了队列满或空时需要重新分配内存的问题,提高了空间利用率和操作效率。在本文中,我们将深入探讨循环队列的概念、实现方式以及其优缺点。 ### 循环队列概念 ...
下面将详细介绍静态数组及其在C语言中的应用,以及如何实现一个简单的静态数组队列。 首先,静态数组是固定大小的数组,其大小在声明时必须确定,之后不能更改。在C语言中,声明静态数组的基本语法如下: ```c ...
总结,数组是编程中不可或缺的数据结构,理解和掌握数组的定义、引用、存储和初始化对于编写高效和可靠的程序至关重要。在实际编程中,数组常常用于处理批量数据,例如,存储矩阵、队列、栈等。通过熟练运用数组,...
在JavaScript中实现队列数据结构主要有两种方式:使用数组和使用对象。本篇文档将探讨如何使用数组来实现一个队列,并展示它的基本操作,如添加元素、删除元素、读取队首和队尾元素等。 ### 队列的概念与特点 队列...
总结来说,栈、队列和数组是C语言中不可或缺的数据结构,它们各有特点,各有优势。理解它们的工作原理和适用场景对于任何想要深入学习计算机科学和编程的开发者来说,都是一个不可或缺的过程。通过本资源,我们希望...
链式队列是一种在计算机科学中广泛使用的数据结构,它基于链表实现,与传统的数组队列相比,具有更大的灵活性。在本程序中,我们主要关注链式队列的六个核心操作,这些操作对于理解和应用链式队列至关重要。 1. **...
数组队列的另一个问题是数组元素移动带来的性能开销,特别是当频繁入队出队操作时,这将影响程序的运行效率。而链表实现的队列则可以克服这些问题,它提供了动态大小调整的能力,避免了不必要的元素移动,但由于链表...
这可以有效地解决普通数组队列在满或空时需要额外空间或无法插入新元素的问题。在循环队列中,我们需要特别处理“假溢出”情况,即队列未满但看起来已满,或队列已空但看起来未空的状态。 此外,栈和队列的综合应用...
总结起来,线性表、堆栈和队列是数据结构的基础,它们在算法设计和程序实现中有着广泛应用。理解并掌握这些概念,以及如何在C++中实现它们,对于提升编程技能和解决实际问题至关重要。通过实践和学习,我们可以更好...
通过这个示例,我们可以学习到在JavaScript中处理数组中对象去重的一种有效方法,这对于优化代码性能和避免重复数据存储非常有用。同时,了解和掌握相关工具和专题知识能帮助我们更好地应对各种编程挑战。
总结来说,这个C语言实例展示了如何使用顺序数组实现基本的队列操作。通过阅读和理解源代码,我们可以学习到队列数据结构的实现细节,这对于理解和编写涉及队列操作的其他C程序非常有帮助。同时,这也为扩展和优化...
3. 空间利用率:链队列可以充分利用内存空间,不会出现数组队列因固定大小导致的空间浪费。 链队列在实际问题中的应用: 链队列在很多领域都有应用,如操作系统中的任务调度、网络数据包的处理、图形算法中的路径...
总结来说,“入队列出队列练习”是学习LabVIEW中队列操作的一个良好起点。通过实践,用户可以深入理解队列的工作原理,以及如何在实际项目中有效地使用队列。通过不断地实践和探索,LabVIEW的使用者能够逐步提高其在...
- `char *elem`:字符型数组,用于存储队列中的元素。 - `int n`:队列的最大容量。 - `int f`:队头指针,指向队头元素的位置。 - `int r`:队尾指针,指向队尾元素的下一个位置,保持队头队尾之间有一个空闲...
循环队列是用数组实现的队列,通过数组下标运算模拟队列的“满”和“空”的状态,避免了数组大小的限制。 3.6 队列的链式存储结构及操作实现 链式队列通过链表实现,每个节点包含数据和指向下一个节点的指针,同样...
在IT领域,数组是一种基本且重要的数据结构,用于存储固定大小的同类型元素集合。数组的存储和操作是计算机科学中的基础概念,广泛应用于各种编程语言中。...理解数组的存储和操作机制对于编程学习和软件开发至关重要。
【栈和队列的基本概念】 栈是一种特殊的线性表,具有“后进先出”(LIFO,Last In First Out)...通过这个实验,学生不仅能深入理解栈和队列的数据结构,还能锻炼编程和问题解决能力,为后续的算法学习打下坚实基础。
数组队列使用固定大小的数组,通过两个指针分别跟踪队首和队尾的位置。入队操作将元素添加到队尾,而出队操作从队首移除元素。当队列满或空时,需要特别处理。 ```cpp class ArrayQueue { public: ArrayQueue(int ...
在CS6版的课程中,可能会涉及循环队列的概念,这是一种优化的数组队列,通过巧妙地设置队首和队尾指针,可以避免数组满时的特殊情况。此外,还可能介绍优先级队列,它允许根据优先级而非FIFO原则进行出队,常用于...
链式队列是队列的一种实现方式,与数组队列不同,它不是通过连续的内存空间来存储元素,而是通过链表来组织元素。每个元素(节点)包含数据和指向下一个元素的指针。这样的设计允许队列在内存中灵活地扩展,无需预先...