03:队列操作

03:队列操作

时间限制: 
100ms
  内存限制: 
100kB
描述
假设以带头节点的循环链表表示队列,并且只设一个指针指向队尾元素节点(不设头指针),节点元素这里设为整型,编写队列的初始化、入队和出队算法。其中入队元素个数n及其节点数据,和出队元素个数m都是从键盘输入(默认n、m都不小于0),然后输出出队元素,出队不合法(自己想想什么情况下不合法)则输出Error。
输入
6 (n的值)
-2 0 1 7 10 -1
3 (m的值)
输出
-2 0 1 (出队元素)
样例输入

0 3 1 21 9 -1
4
样例输出
0 3 1 21 


 
#include <stdio.h>
#include <stdlib.h>

typedef struct Node{
int elem;
struct Node *next;
}Node;
//Creat a Circle Linklist
Node *CCLL(int *a ,int n)
{
Node*tail=(Node*)malloc(sizeof(Node));
Node*temp=(Node*)malloc(sizeof(Node));
tail->next=tail;
int i;
for(i=0;i<n;i++)
{
Node*p=(Node*)malloc(sizeof(Node));
p->elem=a[i];
temp=tail;
while(temp->next!=tail)
temp=temp->next;
temp->next=p;
p->next=tail;
}
return tail;
}
int outQueue(Node *tail)
{
int result;
Node*temp=(Node*)malloc(sizeof(Node));
temp=tail->next;
result=temp->elem;
tail->next=temp->next;
free(temp);
return result;
}

int main()
{
int i,n,m;
scanf("%d",&n);
int *a=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);  
  Node *tail=(Node*)malloc(sizeof(Node));
  tail=CCLL(a,n);
if(n<m)
{
printf("Error\n");
return 0;
}
  for(i=0;i<m;i++)
  {
printf("%d ",outQueue(tail));
}
printf("\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值