用一个一维数组,存放队列,head为队列的头,tail为队孝卜樱列的尾,tail==-1,时队列为空:
#include <stdio.h>#define M 100
int head,tail,que[M]={41,167,34,100,269}
int insert(int t)
{
弊游 if (tail==-1)
{
tail++
que[tail]=t
return 0
}
else
{
tail=(tail+1)%M
if (tail==head)
{
printf("队列满,请等待\n")
tail--
return 1
}
else
que[tail]=t
return 0
}
}
int serviceone() /*服务一个人,返回这个人的服务时间*/
{
int t
t=que[head]
if (tail==-1)
{
printf("没有人排队\n")
t=0
}
else if (head==tail)
{
head=0
tail=-1
}
else
巧丛 {
head=(head+1)%M
}
return t
}
int waittime()
{
int t,i
if (tail==-1) t=0
else
{
t=0
i=head
do
{
t+=que[i]
if (i==tail) return t
else i=(i+1)%M
}while (1)
}
}
int main()
{
int i,j,m,t
m=5
head=0tail=4
printf("当前新准备进入队列者要等待的时间为:%ds\n",waittime())
printf("加入一个人,服务时间100s\n")
insert(100)
m++
printf("当前新准备进入队列者要等待的时间为:%ds\n",waittime())
printf("服务一人,所花时间:%ds\n",serviceone())
m--
printf("当前新准备进入队列者要等待的时间为:%ds\n",waittime())
system("PAUSE")
return 0
}
import java.util.LinkedList
public class Demo01 {
private LinkedList<Object> linkedList
public Demo01() 卖岁虚{
linkedList = new LinkedList<Object>()
}
public void put(Object object) {
linkedList.add(object)
}
public Object get() {
Object object = null
if (linkedList.size() != 0) {
object = 中燃linkedList.get(0)
linkedList.remove(0)
}
return object
}
public boolean isEmpty() {
if (linkedList.size() != 0) {
return true
} else {
return false
}
}
public static void main(String[] args) {
Demo01 demo01 = new Demo01()
demo01.put("1")
demo01.put("雀念2")
System.out.println(demo01.get())
System.out.println(demo01.get())
System.out.println(demo01.isEmpty())
}
}
结果:
1
2
false
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)