数据结构-->队列
2017-07-07 11:19
274 查看
package 队列;
import java.util.Scanner;
public class QueueType {
static final int QUEUELEN=15;
DATA [] Queuedata=new DATA[QUEUELEN];//队列数组
int head;//队头
int tail;//队尾
/*队列初始化
* (1)按符号常量QUEUELEN指定的大小申请一片内存空间,用来保存队列中的数据
* (2)设置p.head=0,p.tail=0,表示一个空栈
* */
QueueType init(){
QueueType p;
if((p=new QueueType())!=null){//申请内存
p.head=0;//设置队头
p.tail=0;//设置队尾
return p;
}else{
return null;//返回空
}
}
/判断空队列/
int QueueEmpty(QueueType p){
int temp=0;
if(p.head==p.tail)
temp=1;
return temp;
}
/判断满队列/
int QueueFull(QueueType p){
int temp=0;
if(p.tail==QUEUELEN)
temp=1;
return temp;
}
class DATA{
String name;
int age;
}
我不知道,为啥输入两次0才会退出??????
import java.util.Scanner;
public class QueueType {
static final int QUEUELEN=15;
DATA [] Queuedata=new DATA[QUEUELEN];//队列数组
int head;//队头
int tail;//队尾
/*队列初始化
* (1)按符号常量QUEUELEN指定的大小申请一片内存空间,用来保存队列中的数据
* (2)设置p.head=0,p.tail=0,表示一个空栈
* */
QueueType init(){
QueueType p;
if((p=new QueueType())!=null){//申请内存
p.head=0;//设置队头
p.tail=0;//设置队尾
return p;
}else{
return null;//返回空
}
}
/判断空队列/
int QueueEmpty(QueueType p){
int temp=0;
if(p.head==p.tail)
temp=1;
return temp;
}
/判断满队列/
int QueueFull(QueueType p){
int temp=0;
if(p.tail==QUEUELEN)
temp=1;
return temp;
} /*清空队列*/ void QueueClear(QueueType p){ p.head=0;//设置队头 p.tail=0;//设置队尾 } /*释放队列*/ void QueueFree(QueueType p){ if(p!=null){ p=null; } } /*入队列 * (1)首先判断队列顶tail,如果tail等于QUEUELEN说明队列满了,表示溢出,进行出错处理,否则执行系列操作 * (2)设置tail=tail+1;(队列顶引用+1,指向入队列地址 * (3)将入队元素保存到tail指向的位置 * */ int QueueIn(QueueType p,DATA Queuedata){ if(p.tail==QUEUELEN){ System.out.println("队列已经满了,不能入队列"); return(0); } p.Queuedata[p.tail++]=Queuedata;//将元素入队列 return (1); } /*出队列 * (1)判断队列head,如果head等于tail,则表示空队列,进行出错处理,否则窒息感下列操作 * (2)从队列头部取出队头元素(实际返回队头元素的引用) * (3)去修改队头head序号,使其指向后一个元素 * */ DATA QueuePop(QueueType p){ if(p.head==p.tail){ System.out.println("空队列,不能进行出队列操作"); System.exit(0); }else{ return p.Queuedata[p.head++]; } return null; } /*读节点数据*/ DATA Queuedata(QueueType p){ if(QueueEmpty(p)==1){ System.out.println("空队列"); return null; } return p.Queuedata[p.head]; } /*计算队列长度*/ int QueueLen(QueueType p){ int temp; temp=p.tail-p.head; return temp; } public static void main (String []args){ QueueType type=new QueueType(); DATA data1=new DATA(); Scanner sc=new Scanner(System.in); QueueType stack=type.init();//初始化队列 System.out.println("入队列操作"); System.out.println("输入姓名:年龄"); do{ DATA data2=new DATA(); data2.name=sc.next(); data2.age=sc.nextInt(); if(data2.name.equals("0")) break;//若输入为0,则退出 type.QueueIn(stack,data2); }while(true); String temp="1"; System.out.println("出队列操作:按任意非0继续"); temp=sc.next(); while(!temp.equals("0")){ data1=type.QueuePop(stack); System.out.printf("出队列的操作数据是(%s,%d)\n",data1.name,data1.age); temp=sc.next(); } System.out.println("测试结束"); type.QueueFree(stack);//释放队列所占用空间 }
}
class DATA{
String name;
int age;
}
我不知道,为啥输入两次0才会退出??????
相关文章推荐
- 数据结构---->队列
- 数据结构基础(3)-->队列
- 数据结构-->(循环)队列 【队列的顺序实现】ADT
- 数据结构---->队列
- <数据结构-队列>
- 数据结构Java实现——②队列--->队列的“奇葩”一循环顺序队列
- <数据结构> 队列[转]
- 数据结构Java实现——②队列-->队列的“奇葩”二 优先级队列
- 数据结构->栈和队列的基本操作
- PHP数据结构大全地址之->模拟静态队列
- 《数据结构》第三章:栈和队列——>总结
- 数据结构-->队列的链式实现 ADT
- C语言数据结构之栈与队列的应用(2)
- 数据结构_队列
- 循环队列及C语言实现<一>
- java 数据结构-->希尔排序
- 数据结构->排序
- 数据结构(C实现)------- 顺序队列(循环队列之计数器实现)
- (C语言)数据结构 --- 队列、栈
- Java数据结构---两个栈形成一个队列