基本操作:出队操作
时间: 1ms 内存:128M
描述:
实现循环队列的出队操作 bool deQueue(SqQueue *&q,ElemType &e)(队列q的元素出队,用e保存)。假设顺序队列的元素类型为char,主函数已给出。
注意:只提交 bool deQueue(SqQueue *&q,ElemType &e) 部分。
#include <stdio.h>
#include <malloc.h>
#define MaxSize 5
typedef char ElemType;
typedef struct
{
ElemType data[MaxSize];
int front,rear; //队首和队尾指针
} SqQueue;void InitQueue(SqQueue *&q) //初始化队列
{
q=(SqQueue *)malloc (sizeof(SqQueue));
q->front=q->rear=0;
}
void DestroyQueue(SqQueue *&q) //销毁队列
{
free(q);
}
bool QueueEmpty(SqQueue *q) //判断队列空
{
return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e) //进队
{
if ((q->rear+1)%MaxSize==q->front) //队满上溢出
return false;
q->rear=(q->rear+1)%MaxSize;
q->data[q->rear]=e;
return true;
}int main()
{
ElemType a,b,c,d,e;
SqQueue *q;
scanf("%c %c %c %c",&a,&b,&c,&d);
InitQueue(q);//初始化队列
enQueue(q,a); //依次进队列元素a,b,c,d
enQueue(q,b);
enQueue(q,c);
enQueue(q,d);
while (!QueueEmpty(q))
{
deQueue(q,e);
printf("%c ",e);
}
printf("\n");
DestroyQueue(q);
return 0;
}
输入:
输入4个入队的元素
输出:
输出4个出队的元素
示例输入:
2 1 4 3
示例输出:
2 1 4 3
提示:
参考答案(内存最优[0]):
#include <stdio.h>
#include <malloc.h>
#define MaxSize 5
typedef char ElemType;
typedef struct
{
ElemType data[MaxSize];
int front,rear; //队首和队尾指针
} SqQueue;
void InitQueue(SqQueue *&q) //初始化队列
{
q=(SqQueue *)malloc (sizeof(SqQueue));
q->front=q->rear=0;
}
void DestroyQueue(SqQueue *&q) //销毁队列
{
free(q);
}
bool QueueEmpty(SqQueue *q) //判断队列空
{
return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e) //进队
{
if ((q->rear+1)%MaxSize==q->front) //队满上溢出
return false;
q->rear=(q->rear+1)%MaxSize;
q->data[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,ElemType &e)
{
if(q->front==q->rear)
return false;
q->front++;
e=q->data[q->front];
return true;
}
int main()
{
ElemType a,b,c,d,e;
SqQueue *q;
scanf("%c %c %c %c",&a,&b,&c,&d);
InitQueue(q);//初始化队列
enQueue(q,a); //依次进队列元素a,b,c,d
enQueue(q,b);
enQueue(q,c);
enQueue(q,d);
while (!QueueEmpty(q))
{
deQueue(q,e);
printf("%c ",e);
}
printf("\n");
DestroyQueue(q);
return 0;
}
参考答案(时间最优[0]):
#include <stdio.h>
#include <malloc.h>
typedef char ElemType;
typedef struct LNode //定义单链表结点类型
{
ElemType data;
struct LNode *next;
} LinkList;
void InitList(LinkList *&L) //初始化线性表
{
L=(LinkList *)malloc(sizeof(LinkList)); //创建头结点
L->next=NULL;
}
void DestroyList(LinkList *&L) //销毁线性表
{
LinkList *p=L,*q=p->next;
while (q!=NULL)
{
free(p);
p=q;
q=p->next;
}
free(p);
}
bool ListEmpty(LinkList *L) //判线性表是否为空表
{
return(L->next==NULL);
}
int ListLength(LinkList *L) //求线性表的长度
{
LinkList *p=L;
int i=0;
while (p->next!=NULL)
{
i++;
p=p->next;
}
return(i);
}
void DispList(LinkList *L) //输出线性表
{
LinkList *p=L->next;
while (p!=NULL)
{
printf("%c ",p->data);
p=p->next;
}
printf("\n");
}
bool ListInsert(LinkList *&L,int i,ElemType e) //插入数据元素
{
int j=0;
LinkList *p=L,*s; //p指向头节点,j置为0(即头节点的序号为0)
while (j<i-1 && p!=NULL) //查找第i-1个节点
{
j++;
p=p->next;
}
if (p==NULL) //未找到第i-1个节点,返回false
return false;
else //找到第i-1个节点*p,插入新节点并返回1
{
s=(LinkList *)malloc(sizeof(LinkList));
s->data=e; //创建新节点*s,其data域置为e
s->next=p->next; //将*s插入到*p之后
p->next=s;
return true;
}
}
int main()
{
ElemType a,b,c,d,e,f;
LinkList *h;
InitList(h);
printf("依次采用尾插法插入a,b,c,d,e元素\n");
scanf("%c %c %c %c %c%*c",&a,&b,&c,&d,&e);
ListInsert(h,1,a);
ListInsert(h,2,b);
ListInsert(h,3,c);
ListInsert(h,4,d);
ListInsert(h,5,e);
DispList(h);
printf("单链表h长度=%d\n",ListLength(h));
printf("在第4个元素位置上插入f元素\n");
scanf("%c",&f);
ListInsert(h,4,f);
DispList(h);
DestroyList(h);
return 0;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。