如题
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
struct node{
int val;
struct node* next;
};
struct queue{
struct node* head;
struct node* end;
};
struct queue* iniqueue(){
struct queue* q=( struct queue*)malloc(sizeof( struct queue));
q->head=q->end=(struct node*)malloc(sizeof(struct node));
q->head->next=NULL;
return q;
}
struct node* addqueue(struct queue* q,int x){
struct node *s=(struct node*)malloc(sizeof(struct node));
s->val=x;
s->next=NULL;
q->end->next=s;
q->end=s;
return s;
}
void dequeue(struct queue* q){
if(q->head->next==NULL)return;
struct node* s=q->head->next;
q->head->next=s->next;
free(s);
s=NULL;
return;
}
int max(int a[],int k){
int s;
for(int i=0;i<k;i++){
if(i==0)s=a[0];
else if(a[i]>=a[i-1])s=a[i];
}
return s;
}
int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize){
if(k==1)return nums;
int i=0;
int n=1;
int *r=malloc(sizeof(int) * (numsSize - k + 1));
struct queue*q=iniqueue();
struct node*f;
struct node*g;
while(1){
if(i==(k-1)){
f=addqueue(q,*(nums+i));
break;
}
else addqueue(q,*(nums+i));
i++;
}
g=q->head;
i=0;
int j=k;
int a[numsSize-k+1];
int b[numsSize-k+1];
int u;
while(i<(numsSize-k+1)){
u=0;
while(1){
g=g->next;
a[u]=g->val;
if(g==f)break;
u++;
}
dequeue(q);
g=q->head;
*(r+i)=max(a,k);
i++;
while(n){
f=addqueue(q,*(nums+j));
j++;
if(j==(numsSize))n=0;
}
}
return r;
}