void InitQueue() { Queue *temp; for(int i=0;i<4;i++) { temp = (Queue*)malloc(sizeof(Queue)); temp->front = NULL; temp->TimeSlice = pow(2,i)*T; temp->length = 10; temp->next = NULL; if(head == NULL) { head = temp; temp = NULL; } else { Queue *temp2; temp2 = head; while(temp2->next != NULL) { temp2 = temp2->next; } temp2->next = temp; temp = NULL; } } }
时间: 2023-07-30 09:10:59 浏览: 45
这段代码看起来像是一个初始化队列的函数,其中包括了四个队列,并且每个队列的时间片长度递增,初始值为T,每递增一个队列的长度就翻倍。每个队列的长度都为10。队列的结构体应该包含了队首指针front,时间片长度TimeSlice,队列长度length和下一个队列的指针next。当head为空时,将队列头指向temp,否则遍历链表找到最后一个队列,将其next指向temp。
相关问题
/*杨辉三角形 - 循环队列 【问题描述】参考教材P103例3.6,完成杨辉三角形的打印,请用循环队列实现(利用之前已实现的循环队列数据结构的代码)。不采用“循环队列”,不给分。 【输出要求】用 cout << setw(3) << x 这样来使每个数据输出都占3列;需包含<iomanip> 【样例输入】 4 【样例输出】 1 1 1 1 2 1 1 3 3 1*/ #include<stdio.h> #define MAXSIZE 100 #include<iostream> #include<stdlib.h> typedef struct Queue{ int elem[MAXSIZE]; int front; int rear; }SeqQueue; void InitQueue(SeqQueue *q){ q->front=q->rear=0; } int EnterQueue(SeqQueue *q,int x){ if((q->rear+1)%MAXSIZE==1)return 1; else{ q->elem[q->rear]=0; q->rear=(q->rear+1)%MAXSIZE; return 0; } } int DeleteQueue(SeqQueue *q,int *x){ if(q->rear==q->front)return 1; else{ *x=q->elem[q->front]; q->front=(q->front+1)%MAXSIZE; return 0; } } int GetHead(SeqQueue *q,int *x){ if(q->rear==q->front)return 1; else{ *x=q->elem[q->front]; return 0; } } void YangHui(SeqQueue *q,int N){ int x,temp; EnterQueue(q,1); for(int n=2;n<=N;n++){ EnterQueue(q,1); for(int i=0;i<N-2;i++){ DeleteQueue(q,&temp); printf("%6d ",temp); GetHead(q,&x); temp=temp+x; EnterQueue(q,temp); }DeleteQueue(q,&x); printf("%6d ",x); EnterQueue(q,1); printf("\n"); } while(q->front!=q->rear) { DeleteQueue(q,&x); printf("%6d",x); } } int main(){ SeqQueue *q; q=(SeqQueue*)malloc(sizeof(SeqQueue)); InitQueue(q); int N; scanf("%d",&N); YangHui(q,N); return 0; }
这段代码中似乎并没有输出地址的部分,但是确实存在一些问题。例如,在EnterQueue函数中,如果队列已满,返回值应该是1而不是0。此外,YangHui函数中的循环条件应该是i<n-2而不是i<N-2。另外,还需要在文件头添加 #include<iomanip> 以使用 setw 函数。以下是已经修改过的代码:
```
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<iomanip>
#define MAXSIZE 100
typedef struct Queue{
int elem[MAXSIZE];
int front;
int rear;
}SeqQueue;
void InitQueue(SeqQueue *q){
q->front=q->rear=0;
}
int EnterQueue(SeqQueue *q,int x){
if((q->rear+1)%MAXSIZE==q->front) return 1;
else{
q->elem[q->rear] = x;
q->rear=(q->rear+1)%MAXSIZE;
return 0;
}
}
int DeleteQueue(SeqQueue *q,int *x){
if(q->rear==q->front) return 1;
else{
*x=q->elem[q->front];
q->front=(q->front+1)%MAXSIZE;
return 0;
}
}
int GetHead(SeqQueue *q,int *x){
if(q->rear==q->front) return 1;
else{
*x=q->elem[q->front];
return 0;
}
}
void YangHui(SeqQueue *q,int N){
int x,temp;
EnterQueue(q,1);
for(int n=2;n<=N;n++){
EnterQueue(q,1);
for(int i=0;i<n-2;i++){
DeleteQueue(q,&temp);
std::cout << std::setw(3) << temp << " ";
GetHead(q,&x);
temp=temp+x;
EnterQueue(q,temp);
}
DeleteQueue(q,&x);
std::cout << std::setw(3) << x << " ";
EnterQueue(q,1);
std::cout << std::endl;
}
while(q->front!=q->rear) {
DeleteQueue(q,&x);
std::cout << std::setw(3) << x;
}
}
int main(){
SeqQueue *q;
q=(SeqQueue*)malloc(sizeof(SeqQueue));
InitQueue(q);
int N;
scanf("%d",&N);
YangHui(q,N);
return 0;
}
```
希望对你有帮助!
C语言实现利用队列设计一个算法,实现存储在数组A[n]中的n个元素循环左移k(0<k<n)位
以下是利用队列实现数组循环左移k位的C语言代码:
```c
#include <stdio.h>
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int front, rear;
} Queue;
void initQueue(Queue *q) {
q->front = q->rear = 0;
}
int isQueueEmpty(Queue *q) {
return (q->front == q->rear);
}
int isQueueFull(Queue *q) {
return ((q->rear + 1) % MAXSIZE == q->front);
}
int enQueue(Queue *q, int x) {
if (isQueueFull(q)) {
return 0;
}
q->data[q->rear] = x;
q->rear = (q->rear + 1) % MAXSIZE;
return 1;
}
int deQueue(Queue *q, int *x) {
if (isQueueEmpty(q)) {
return 0;
}
*x = q->data[q->front];
q->front = (q->front + 1) % MAXSIZE;
return 1;
}
void reverse(int a[], int left, int right) {
while (left < right) {
int temp = a[left];
a[left] = a[right];
a[right] = temp;
left++;
right--;
}
}
void leftRotate(int a[], int n, int k) {
k %= n;
if (k == 0) {
return;
}
Queue q;
initQueue(&q);
for (int i = 0; i < k; i++) {
enQueue(&q, a[i]);
}
for (int i = k; i < n; i++) {
int x;
deQueue(&q, &x);
enQueue(&q, a[i]);
a[i] = x;
}
for (int i = 0; i < k; i++) {
int x;
deQueue(&q, &x);
a[i] = x;
}
reverse(a, 0, n - 1);
reverse(a, 0, k - 1);
reverse(a, k, n - 1);
}
int main() {
int a[] = {1, 2, 3, 4, 5};
int n = sizeof(a) / sizeof(a[0]);
int k = 3;
leftRotate(a, n, k);
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
```
这里利用队列的思路是,先将数组中的前k个元素入队,然后从第k个元素开始,每次将队头元素出队并入队到队尾,同时将数组中的当前位置设置为出队的元素值,直到遍历完整个数组。接着将队列中剩余的元素依次出队,并将它们赋值给数组中前k个位置。最后对数组进行三次翻转操作,即可得到循环左移k位后的数组。
需要注意的是,由于数组是循环的,所以k可能超过n,因此在实际计算中需要先对k取模,以确保k在合理的范围内(0<k<n)。同时,队列的实现中需要注意队列满和队列空的判断条件。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)