

deque関数を受け取ったキューの先頭を返し、 先頭以外が残るようにしたいのですがご教授お願いいたします。

/* basetypes.c */

[code]

    #include <stdlib.h>
    #include <stdio.h>
    #include "basetypes.h"
    
    PCB *pcb_new (int start, int required)
    {
      PCB *pcb = (PCB *)malloc(sizeof(PCB));
      pcb->next = NULL;
      pcb->pid = 0;            
      pcb->state = READY;
      pcb->waiting_time = 0;
      pcb->executed_time = 0;
      pcb->start_time = start;     
      pcb->required_time = required; 
      return pcb;
    } 
    
    void pcb_enqueue(PCB *pcb, PCB **q)
    {
      if (*q == pcb) {
        printf("Failed!!!\n");
        exit(0);
      }
      pcb->next = NULL;
      if (! *q)
        *q = pcb;
      else {
        PCB *p = *q;
        while (p->next != NULL) {
          p = p->next;
        }
        if (p->next == pcb) {
          printf("Failed!!!\n");
          exit(0);
        }
        p->next = pcb;
      }
    }
    
    PCB *pcb_dequeue(PCB **q)
    {
      PCB *pcb;
      if (! *q)
        return NULL;
      PCB *p = (*q);
      pcb = (*q)->next;
      (*q) = pcb;
      return p;
    }
    
    void pcb_print_list(PCB *top)
    {
      if (top == NULL) {
        printf("[]");
      }
      else {
        while(top != NULL){
          printf("[pid%d,%d,%d]", top->pid, top->start_time, top->required_time);
          top=top->next;
          if (top)
        printf(", ");
        }
      }
      printf("\n");
    }
    
    int main (void)
    {
      /* 変数の宣言 */
      PCB *p1 = pcb_new(0, 1);
      PCB *p2 = pcb_new(1, 2);
      PCB *p3 = pcb_new(2, 3);
      PCB *p4 = pcb_new(3, 4);
      PCB *p5 = pcb_new(4, 5);
      PCB *x1;
      /* pcb_enqueue */
      pcb_enqueue(p2, &p1);
      pcb_enqueue(p3, &p1);
      pcb_enqueue(p4, &p1);
      pcb_enqueue(p5, &p1);
      pcb_print_list(p1);
      /* pcb_dequeue */
      x1 = pcb_dequeue(&p1);
      pcb_print_list(x1);
      pcb_print_list(p1);
    
      return 0;
    }
    
[/code]

/* basetypes.h */

[code]

    #define BASETYPES_H
    typedef int boolean;
    #define true    (1 == 1)
    #define false    0
    
    typedef enum { RUNNING, READY, WAITING } Pstate;
    
    /* Process Control Block */
    typedef struct PCB {
      struct PCB *next;
      int pid;            /* Process ID */
      Pstate state;            /* joutai */
      int waiting_time;        /* jikkoumachi deno keika jikan */
      int executed_time;        /* jikkou zumi jikan */
      int start_time;        /* seisei jikoku */
      int finished_time;        /* shuryo jikoku */
      int required_time;        /* hituyou jikan */
    } PCB;
    
    /* PCB comparing function type */
    typedef PCB *(pcb_compare_function)(PCB *, PCB *);
    
    PCB *pcb_new(int, int);
    void pcb_enqueue(PCB *, PCB **);
    PCB *pcb_dequeue(PCB **);
    void pcb_remove(PCB *, PCB **);
    void pcb_print_list(PCB*);
    
    int current_clock;
    
[/code]


CC BY-SA 3.0
2015-06-24T15:17:28.553
11694
2015-09-03T14:29:36.797
2015-09-03T14:29:36.797
10019
question
c
deque関数を受け取ったキューの先頭を返し、 先頭以外が残るようにしたい


取り出した先頭要素のリンク先に NULL を指定してあげれば良いのではないでしょうか。

[code]

    PCB *pcb_dequeue(PCB **q)
    {
      if (! *q)
        return NULL;
      PCB *p = *q;
      *q = (*q)->next;
      p->next = NULL;
      return p;
    }
    
[/code]


CC BY-SA 3.0
2015-06-24T15:44:44.760
11696
2015-06-24T15:44:44.760
11694
answer