I want to return the beginning of the queue that received the deque function so that the non-start remains.

Asked 2 years ago, Updated 2 years ago, 39 views

I'd like to return the beginning of the queue where I received the deque function so that the other queue remains.

/*basetypes.c*/

#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)
{
  /* Declare Variables*/
  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);
  One PCB*;
  /* 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;
}

/*basetypes.h*/

#defineBASETYPES_H
typeef int boolean;
# define true(1==1)
#define false0

typeefenum {RUNNING, READY, WAITING} Pstate;

/* Process Control Block*/
typeef struct PCB {
  struct PCB*next;
  intpid; /* Process ID */
  Pstate state; /* joutai*/
  int waiting_time; /* jikkoumachi denokeika jikan*/
  int executed_time; /*jikkou zumi jikan*/
  int start_time; /*seisei jikoku*/
  intfinished_time; /*shuryo jikoku*/
  int required_time; /* hituyou jikan*/
} PCBs;

/* PCB comparing function type*/
typeef 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;

c

2022-09-30 19:17

1 Answers

Wouldn't it be better to specify NULL as the link destination for the first element you took out?

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


2022-09-30 19:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.