队列的基本操作

来源:互联网 发布:windows切换mac系统 编辑:程序博客网 时间:2024/04/25 14:54

队列的基本操作

 
转载

要求:

(1)编写链接队列的基本操作函数

1.进队函数 EnQueue(LinkQueue *Q,QElemType e)

2.出队函数 ,队空 DeQueue(LinkQueue *Q,QElemType e)

3.输出队列中元素  OutputQueue(LinkQueue *Q)

 

(2)调用上述函数实现下列操作,操作步骤如下

1.调用进队函数建立一个队列

2.读取队列中的第一个元素

2.从队列中删除元素

4.输出队列中的所有元素

 

(3)编写环形队列的基本操作函数

1.进队函数 EnQueue(SqQueue *Q,QElemType e)

2.出队函数 ,队空 DeQueue(SqQueue *Q,QElemType e)

3.输出队列中元素  OutputQueue(SqQueue *Q)

(4)调用上述函数实现下列操作,操作步骤如下

1.调用进队函数建立一个队列

2.读取队列中的第一个元素

2.从队列中删除元素

4.输出队列中的所有元素

 

 

 

 

 

 

 

 

1.链接队列:

#include<stdio.h>

#include<malloc.h>

typedef struct node

 {int data;

  struct node *next;

  };

typedef struct 

{struct node *front;

 struct node *rear;

}LinkQueue;

 

InitQueue(LinkQueue *Q)

Q->front=(struct node *)malloc(sizeof( struct node));

  Q->rear=Q->front;

  Q->front->next=NULL;

}

 

 EnQueue(LinkQueue *Q,int e)

{struct node *p;

 p=(struct node *)malloc(sizeof(struct node ));

 p->data=e;

 p->next=NULL;

 Q->rear->next=p;

 Q->rear=p; 

}

 

 DeQueue(LinkQueue *Q,int e)

struct node *p;

  if(Q->front==Q->rear)

return 0;

 

 else{

        p=Q->front->next;

        Q->front->next=p->next;

        if(p->next=NULL)Q->rear=Q->front;

        return(p->data);

        free(p);

  }

}

 

OutputQueue(LinkQueue *Q)

struct node *p;

 p=Q->front->next;

 while(p!=NULL)

 printf("%d ",p->data);

   p=p->next;

 }

}

 

GetFront(LinkQueue *Q)

 struct node *p;

   p=Q->front->next;

   printf("%d",p->data);

}

 

 

void main()

LinkQueue s;

  int i,max,e,item;

  InitQueue(&s);

  printf("put max:");

  scanf("%d",&max);

  printf("shu ru yuan su");

  for(i=0;i<max;i++){ 

      scanf("%d",&e);

      EnQueue(&s,e);}

  OutputQueue(&s);

  printf("\n");

  printf("di yi ge yuan su wei:");

  GetFront(&s);

  printf("\n");

  printf("shu ru shan chu yuan su :");

  scanf("%d",&item);

  DeQueue(&s,item);

  OutputQueue(&s);

  printf("\n");

}

 

 

 

 

 

 

 

2.环形队列:

#define MAXQSIZE 100

#include<stdio.h>

#include<malloc.h>

typedef struct{

 int *base;

 int front;

 int rear;

}SqQueue;

 

InitQueue(SqQueue *Q)

Q->base=(int *)malloc(MAXQSIZE sizeof(int));

  if(!Q->base)exit(1);

  Q->front=Q->rear=0;

}

 

EnQueue(SqQueue *Q,int e)

Q->base[Q->rear]=e;

  Q->rear=(Q->rear+1)%MAXQSIZE;

}

 

DeQueue(SqQueue *Q,int *e)

if(Q->front==Q->rear)return 0;

  e=Q->base[Q->front];

  Q->front=(Q->front+1)%MAXQSIZE;

}

 

GetFront(SqQueue *Q)

if(Q->front==Q->rear)return 0;

  else printf("%d",Q->base[(Q->front)%MAXQSIZE]);

}

 

OutputQueue(SqQueue *Q)

int i;

  i=Q->front;

  if(!(Q->front==Q->rear))

  {

  while((i%MAXQSIZE)!=Q->rear)

   printf("%d ",Q->base[i%MAXQSIZE]);

      i++;

   }

  }

 

}

 

 

void main()

SqQueue *s;

  int i,max,e,item;

  InitQueue(&s);

  printf("put max:");

  scanf("%d",&max);

  printf("shu ru yuan su :");

  for(i=0;i<max;i++){ 

      scanf("%d",&e);

      EnQueue(&s,e);}

  OutputQueue(&s);

  printf("\n");

  printf("di yi ge yuan su wei :");

  GetFront(&s);

  printf("\n");

  printf("shu ru shan chu yuan su:");

  scanf("%d",&item);

  DeQueue(&s,item);

  OutputQueue(&s);

}

0 0
原创粉丝点击