栈:
package com.nanjing.dataStruc; /** * 后进先出(LIFO),栈顶压入,栈顶压出,只允许访问最后一个插入的数据项,应用:单词逆序,分隔符匹配 */ public class Stack { private int maxSize; private long[] stackArray; private int top; private int nItemes; public Stack(int s) { nItemes=0; maxSize = s; stackArray = new long[maxSize]; top = -1; } public void push(long a) throws Exception { if (top >= maxSize-1) throw new Exception("Stack is full~"); stackArray[++top] = a; nItemes++; } public long pop() { nItemes--; return stackArray[top--]; } public long peek() { return stackArray[top]; } public int getSize(){ return nItemes; } public boolean isEmpty() { return top == -1; } public boolean isFull() { return top == maxSize; } public static void main(String[] args) { Stack stack = new Stack(10); try { stack.push(23); stack.push(123); stack.push(4); stack.push(67); stack.push(45); stack.push(87); stack.push(65); stack.pop(); System.out.println("一共"+stack.getSize()+"个元素"); while (!stack.isEmpty()) { System.out.print(stack.pop() + "\t"); } } catch (Exception e) { e.printStackTrace(); } finally { } } }
队列:
package com.nanjing.dataStruc; /** * 先进先出(FIFO),只允许访问第一个插入的数据项,可以在队尾插入数据,在队头移除数据,应用:解析算术表达式 */ public class Queue { private long[] queueArray; private int tail; private int head; private int nItemes; private int maxSize; public Queue(int size) { queueArray = new long[size]; maxSize = size; tail = 0; head = -1; nItemes = 0; } public void insert(long element) { if (head == maxSize - 1) throw new ArrayIndexOutOfBoundsException("队列越界,不能添加"); queueArray[++head] = element; nItemes++; } public long remove() { if (tail == maxSize) throw new RuntimeException("队列越界,不能删除"); long q = queueArray[tail++]; nItemes--; return q; } public long getSize() { return nItemes; } public boolean isEmpty() { return nItemes == 0; } public boolean isFull() { return nItemes == maxSize; } public static void main(String[] args) { Queue queue = new Queue(10); queue.insert(23); queue.insert(54); queue.insert(76); queue.insert(87); queue.insert(65); queue.remove(); queue.remove(); queue.remove(); queue.remove(); queue.remove(); queue.insert(878); queue.insert(455); queue.insert(343); while (!queue.isEmpty()) { System.out.println(queue.remove()); } } }
优先级队列:
package com.nanjing.dataStruc; /** * 优先级队列,允许访问最小或者最大的数据项,可以有序的插入新数据项和移除关键字最小的数据项,最大的数据项先被处理 */ public class PriorityQ { private long[] priArray; private int maxSize; private int nItemes; public PriorityQ(int maxSize) { this.priArray = new long[maxSize]; this.maxSize = maxSize; nItemes = 0; } public void insert(long data) { int j; if (nItemes == 0) { priArray[nItemes++] = data; } else { for (j = nItemes - 1; j >= 0; j--) { if (data < priArray[j]) { priArray[j + 1] = priArray[j]; //笔插入数据大的整体右移 } else { break; } } priArray[j + 1] = data; nItemes++; } } public long remove() { long q = priArray[--nItemes]; return q; } public boolean isEmpty() { return nItemes == 0; } public boolean isFull() { return nItemes == maxSize; } public static void main(String[] args) { PriorityQ priorityQ = new PriorityQ(10); priorityQ.insert(23); priorityQ.insert(656); priorityQ.insert(134); priorityQ.insert(54); priorityQ.insert(12); long[] priarry = priorityQ.getPriArray(); System.out.print("插入了:"); for (int i = 0; i < priarry.length; i++) { System.out.print(+priarry[i] + "\t"); } System.out.println(); System.out.print("删除了:"); while (!priorityQ.isEmpty()) { System.out.print(priorityQ.remove() + "\t"); } } public long[] getPriArray() { return priArray; } public void setPriArray(long[] priArray) { this.priArray = priArray; } }
相关推荐
实验三 栈和队列 3.1实验目的: (1) 熟悉栈的特点(先进后出)及栈的基本操作,如入栈、出栈等,掌握栈的基本操作在栈的顺序存储结构和链式存储结构上的实现; (2) 熟悉队列的特点(先进先出)及队列的基本操作...
栈和队列的基本操作实验报告 一、实验目的 1、熟练掌握栈和队列的基本操作在两种存储结构上的实现。 2、会用栈和队列解决简单的实际问题。 二、实验内容 题目:试写一个算法,判断依次读入的一个以@为结束符的...
栈和队列考试题 复习 栈和队列考试题 复习栈和队列考试题 复习
数据结构栈和队列的课程指导上几实验代码 相抵部分
栈和队列 源代码 参考数据结构(JAVA版)
数据结果实验之回文判断程序栈和队列基本操作数据结果实验之回文判断程序栈和队列基本操作数据结果实验之回文判断程序栈和队列基本操作
基于c语言数据结构中栈和队列思想的简单停车场管理系统,以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入的输入数据序列进行模拟管理。每一组输入数据包括三个数据项:汽车“到达”或“离去”信息、汽车...
实验题目:栈和队列的应用 二. 实验内容:迷宫问题 三.实验目的:掌握栈和队列的概念及工作原理,运用其原理完成实验题目中的内容。 四.实验要求:为了使学生更好的掌握与理解课堂上老师所讲的概念与原理,实验...
PPT内容是数据结构中有关栈和队列的知识,非常适合正在学习数据结构基础的同学
栈的算法和应用,以及相应习题,队列的算法及应用,学会利用栈和队列解决一些问题
有关于栈和队列的简介 有关于栈和队列的简介 有关于栈和队列的简介
栈和队列(基础知识,单项选择题,填空题,简答题,程序)
栈和队列的基本操作及其应用 1、掌握栈和队列的顺序存储结构和链式存储结构,以便在实际中灵活应用。 2、掌握栈和队列的特点,即后进先出和先进先出的原则。 3、掌握栈和队列的基本运算,如:入栈与出栈,入队与出队...
用链表实现栈和队列的操作,使链表操作更加成熟,熟练栈和队列的机制
数据结构课件及课堂笔记 栈和队列 计算机类
栈和队列 栈和队列是两种特殊的线性表,是操作受限的线性表,称限定性DS 讲解了栈和队列的操作与应用
1、熟练掌握栈和队列的基本操作在两种存储结构上的实现。 2、会用栈和队列解决简单的实际问题。 二、实验内容 (可任选或全做) 假设称正读数和反读数都相同的字符序列为“回文”,例如,‘abba’和‘abcba’是回文...
线性表之栈和队列数据类型的概述内容。主要介绍栈和队列的逻辑特征和操作特性。
1.栈和队列的共同特点是( )。 A.只允许在端点处插入和删除元素 B.都是先进后出 C.都是先进先出 D.没有共同点 2.用链接方式存储的队列,在进行插入运算时( ). A. 仅修改头指针 B. 头、尾指针都要修改 C. 仅修改尾...
数据结构实验报告 《二、栈和队列的运用》