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