目录
由于队列的性质,增删只能在分开两头进行。
下面是书中的分析过程。
C语言
#include<stdio.h>
#include<stdbool.h>
#include<assert.h>
#include<stdlib.h>
typedef struct node* NODE;
struct node{
int maxsize;
int front, end;
int* data;
};
NODE creat()
{
int length = 0;
NODE head = (NODE)malloc(sizeof(struct node));
if (!head)
return NULL;
printf("请输入队列的长度:");
scanf_s("%d", &length);
head->maxsize=length;
head->front = head->end = 0;
head->data = (int*)malloc(head->maxsize * sizeof(int));
if (!head->data)
return NULL;
return head;
}
bool isempty(NODE head)
{
return head->end == head->front;
}
bool isFull(NODE head)
{
return (head->end+1) % head->maxsize == head->front;
}
bool addElement(NODE head)
{
assert(head);
if (isFull(head)) {
printf("队列已满!\n");
return true;
}
else {
int dat = 0;
printf("请输入需要添加的值:");
scanf_s("%d", &dat);
head->data[(head->end++) % head->maxsize] = dat;
printf("%d已添加\n",dat);
return false;
}
}
int delElement(NODE const head)
{
assert(head);
if (isempty(head))
{
printf("队列为空!!");
return -1;
}
else {
int value = head->data[head->front];
head->front=(++head->front)%head->maxsize;
return value;
}
}
void show(NODE const head)
{
assert(head);
int f=head->front;
int e = head->end;
while (f!=e)
{
printf("--%d", head->data[f++]);
}
printf("\n");
printf("打印完毕!!!\n");
}
int main()
{
NODE pointer=creat();
int flag;
while (true) {
bool s=addElement(pointer);
if (s)
break;
printf("是否继续添加(大于等于1继续 OR 0为停止):");
scanf_s("%d", &flag);
if (!flag)
break;
}
int over=delElement(pointer);
if (over == -1)
printf("无元素\n");
else
printf("%d已删除\n", over);
show(pointer);
over = delElement(pointer);
if (over == -1)
printf("无元素\n");
else
printf("%d已删除\n", over);
show(pointer);
return 0;
}
控制台打印
Java
import java.util.Scanner;
public class 队列 {
public static void main(String[] args) {
NODE pointer=creat();
int flag;
while (true) {
Boolean s=addElement(pointer);
if (s)
break;
System.out.println("是否继续添加(大于等于1继续 OR 0为停止):");
Scanner sc=new Scanner(System.in);
flag=sc.nextInt();
if (flag==0)
break;
}
int over=delElement(pointer);
if (over == -1)
System.out.println("无元素");
else
System.out.println( over+"已删除");
show(pointer);
over = delElement(pointer);
if (over == -1)
System.out.println("无元素");
else
System.out.println( over+"已删除");
show(pointer);
}
private static void show(NODE head) {
int f=head.front;
int e = head.end;
if(isempty(head)) {
System.out.println("队列为空!!");
}
while (f!=e)
{
System.out.println("--"+head.data[f++]);
}
System.out.println("打印完毕!!!\n");
}
private static int delElement(NODE head) {
if (isempty(head))
{
return -1;
}
else {
int value = head.data[head.front];
head.front=(++head.front)%head.maxsize;
return value;
}
}
public static Boolean isFull(NODE head){
return (head.end+1) % head.maxsize == head.front;
}
public static Boolean isempty(NODE head){
return head.end == head.front;
}
private static Boolean addElement(NODE head) {
if (isFull(head)) {
System.out.println("队列已满!");
return true;
}
else {
System.out.println("请输入需要添加的值:");
Scanner sc=new Scanner(System.in);
int dat = sc.nextInt();
head.data[(head.end++) % head.maxsize] = dat;
System.out.println(dat+"已添加");
return false;
}
}
private static NODE creat() {
NODE head = new NODE(0,0,0);
System.out.println("请输入队列的长度:");
Scanner sc=new Scanner(System.in);
int length = sc.nextInt();
head.maxsize=length;
head.data = new int[length];
return head;
}
}
class NODE{
int maxsize;
int front,end;
int[] data;
public NODE(int maxsize, int front, int end) {
this.maxsize = maxsize;
this.front = front;
this.end = end;
}
}