赞
踩
队列的操作与栈的操作类似,不同的是,删除是在表的头部(队头)进行的。队列也有两种存储表示,顺序表示和链式表示。
和顺序栈相类似,在队列的顺序存储结构中,除了用一组地址连续的存储单元依次存储从队头到队尾的元素外,尚需附设两个整形变量front和rear分别指示队头元素及队尾元素的位置。附设整型变量size用来表示队列的最大存储元素个数。
以下就是顺序队列的不同代码实现:
C语言(SequentialQueue):
- #ifndef SEQUENTIALQUEUE_H_INCLUDED
- #define SEQUENTIALQUEUE_H_INCLUDED
- #define TRUE 1
- #define FALSE -1
- typedef int ElemType;
- typedef struct Node
- {
- volatile ElemType *base;
- volatile int front;
- volatile int rear;
- volatile int size;
- }SqQueue;
- int InitQueue(SqQueue *S,int size)
- {
- S->base=(SqQueue*)malloc(sizeof(SqQueue)*size);
- if(S->base!=NULL)
- {
- S->front=0;
- S->rear=0;
- S->size=size;
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- void DestroyQueue(SqQueue *S)
- {
- free(S->base);
- S->base=NULL;
- S->front=NULL;
- S->rear=NULL;
- S->size=NULL;
- }
- void ClearQueue(SqQueue *S)
- {
- for(int i=0;i<S->size;i++)
- {
- *(S->base+i)=NULL;
- }
- S->front=0;
- S->rear=0;
- }
- int QueueEmpty(SqQueue *S)
- {
- if(S->front==S->rear)
- {
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- int QueueLength(SqQueue *S)
- {
- return S->rear-S->front;
- }
- ElemType GetHead(SqQueue *S)
- {
- if(S->front==S->rear)
- {
- return FALSE;
- }
- else
- {
- return S->base[S->front];
- }
- }
- int EnQueue(SqQueue *S,ElemType elem)
- {
- if(S->rear-S->front==S->size)
- {
- return FALSE;
- }
- else
- {
- S->base[S->rear]=elem;
- S->rear++;
- return TRUE;
- }
- }
- ElemType DeQueue(SqQueue *S)
- {
- if(S->front==S->rear)
- {
- return FALSE;
- }
- else
- {
- ElemType tmp=S->base[S->front];
- S->front++;
- return tmp;
- }
- }
- void QueueTraverse(SqQueue *S)
- {
- int i=S->front;
- while(i<S->rear)
- {
- printf("%d\t",*(S->base+i));
- i++;
- }
- printf("\n");
- }
- #endif // SEQUENTIALQUEUE_H_INCLUDED
-
Java语言:
- package DataStructure.LinearList;
- class SequentialList1
- {
- protected Object []elem;
- protected int front;
- protected int rear;
- public SequentialList1(int maxsize)
- {
- this.elem=new Object[maxsize];
- }
- public SequentialList1() {}
- }
- public class SequentialQueue extends SequentialList1{
- private SequentialList1 Queue;
- public void InitQueue(int size)
- {
- this.Queue=new SequentialList1(size);
- this.Queue.front=0;
- this.Queue.rear=0;
- }
- public Boolean DestoryQueue()
- {
- this.ClearQueue();
- this.Queue.elem=null;
- this.Queue=null;
- return true;
- }
- public Boolean ClearQueue()
- {
- int i=this.QueueLength();
- while (i<this.Queue.rear)
- {
- this.DeQueue();
- i++;
- }
- this.Queue.rear=this.Queue.front=0;
- return true;
- }
- public Boolean QueueEmpty()
- {
- if (this.Queue.rear==this.Queue.front)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public int QueueLength()
- {
- return this.Queue.rear-this.Queue.front;
- }
- public Object GetHead()
- {
- if (this.Queue.rear==this.Queue.front)
- {
- return -1;
- }
- else
- {
- return this.Queue.elem[this.Queue.front];
- }
- }
- public Boolean EnQueue(Object elem)
- {
- if (this.Queue.rear==this.Queue.elem.length)
- {
- return false;
- }
- else
- {
- this.Queue.elem[this.Queue.rear]=elem;
- this.Queue.rear++;
- return true;
- }
- }
- public Object DeQueue()
- {
- if (this.Queue.front==this.Queue.rear)
- {
- return -1;
- }
- Object Get=this.Queue.elem[this.Queue.front];
- this.Queue.elem[this.Queue.front]=null;
- this.Queue.front++;
- return Get;
- }
- public void QueueTraverse()
- {
- int i=this.Queue.front;
- while (i<this.Queue.rear)
- {
- System.out.print(this.Queue.elem[i]+"\t");
- i++;
- }
- System.out.println("");
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。