user-0 提问: 为什么我的就不显示  DOWN  哪里错了

回复:
user: user-1
content: 我把你的代码稍稍整理了一下，功能上是相同的: #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> typedef struct _proc {     char name[32];           /*定义进程名称*/     int cylinder;                /*定义柱面号*/     int track;                  /*定义磁道面号*/     int rec;                 /*定义记录号*/     struct _proc *prior;     struct _proc *next; }PRO;       PRO *g_head=NULL,*g_curr=NULL,local/* no need to dynamically allocation local*/; int g_cylinder=0;                       //初始柱面号 int direction=1;                          //初始方向 int rec0=0;                        //初始记录号  // Add a new process with given parameters to the end of the  // global doubly linked list pointed by g_head //  void AddProcess(const char * name, int cylinder, int track, int rec) { 	PRO * p=(PRO*)malloc(sizeof(PRO)); 	strcpy(p->name, name); 	p->cylinder=cylinder; 	p->track=track; 	p->rec=rec; 	p->next=NULL; 	 	// should use a PRO* to point to the tail, or is g_curr designed for that purpose?? 	PRO** c=&g_head, *prior=NULL; 	while(*c) 		prior=*c, c=&(*c)->next;  	*c=p; 	p->prior=prior; 	printf("AddProcess %s, prior is %p\n", p->name, p->prior); }  // PRO as per its definition, can be copied bitwise void CopyProcess(PRO* dest, const PRO* src) { 	memcpy(dest, src, sizeof(PRO)); }  // Remove an existing PRO node from the double list pointed by g_head; // void RemoveProcess(PRO * p) { 	PRO * prev, *next; 	// FOR DEBUG 	printf("RemoveProcess:%s, %p, %p\n", p->name, p->prior, p->next); 	prev=p->prior; 	next=p->next; 	if(prev) 		prev->next=next; 	else // is the first node 		g_head=next; 	 	if(next) 		next->prior=prev; 	 	free(p); }   void init() { 	AddProcess("P1", 1, 2, 0); 	AddProcess("P2",2,3,3); 	AddProcess("P3", 3,5, 5); 	AddProcess("P4", 4, 5, 1); 	AddProcess("P5", 3, 5, 1); 	AddProcess("P6", 2, 5, 1);       	memset(&local, 0, sizeof(PRO));     strcpy(local.name, "P0"); }         void PrintInit()                                 /*打印I/O表*/ {     PRO *t = g_head;     printf("Current I/O table is：\n");     printf("         **********************************\n");     printf("         ");     printf("*Process   Cylinder   Track   Record*\n");     while(t!=NULL)     {         printf("         *%4s %8d %8d %5d    *\n", t->name, t->cylinder, t->track, t->rec );         t = t->next;     }     printf("         **********************************\n");     printf("Most recently disptached process:\n");     printf(" Process  Cylinder  Track  Record\n");     printf("%7s %8d %7d, %8d\n\nCurrent direction: %s\n---------------------\n", local.name, local.cylinder,     	local.track, local.rec, direction?"UP":"DOWN" ); }         void acceptreq()                        /*接受请求函数*/ {     PRO p;     printf("please input the information of the new process\n");     printf("Process:");     scanf("%s",p.name);     printf("Cylinder(0-199):");     scanf("%d",&p.cylinder);                    /*输入请求进程信息*/     printf("Track(0-20):");     scanf("%d",&p.track);     printf("Record(0-7):");     scanf("%d",&p.rec);     getchar();     AddProcess(p.name, p.cylinder, p.track, p.rec); }         void qddd()                         /*驱动调度函数*/ {     PRO *out, *p;     int deng=0;     int deng1=0;     int min;     int max;  	if(!g_head) 	{ 		printf("\n\n***Cannot disptach with an empty I/O table!***\n\n"); 		return; 	} 	     min=g_head->cylinder;     max=g_head->cylinder;       for (p=g_head;p!=NULL;p=p->next)     {         if(p->cylinder==g_cylinder)         {             min=p->rec;             out=p;             deng=1;             break;         }     }     switch(deng)     {     case 1:         for (p=g_head;p!=NULL;p=p->next)         {             if(p->cylinder==g_cylinder&&abs(p->rec-rec0)<=abs(min-rec0))             {                 min=p->rec;                 out=p;             }         }         break;     case 0:         switch (direction)         {         case 1:             for (p=g_head;p!=NULL;p=p->next)             {                 if (p->cylinder > g_cylinder)                 {                     min = p->cylinder; // what's the purpose of min? value assigned here never used later                     deng1=1;                     break;                 }             }             switch(deng1)             {             case 1:                 for (p=g_head;p!=NULL;p=p->next)                     {                         if (min>=p->cylinder&&p->cylinder>g_cylinder)                         {                             min=p->cylinder;                             out=p;                         }                     }                     break;             case 0:                 direction=0;                 for (p=g_head;p!=NULL;p=p->next)                 {                     if (max<=p->cylinder)                     {                         min=p->cylinder;                         out=p;                     }                 }                 break;             }             break;         case 0:             for (p=g_head;p!=NULL;p=p->next)             {                 if (p->cylinder < g_cylinder)                 {                     max = p->cylinder;                     deng1=1;                     break;                 }             }             switch(deng1)             {             case 1:                 for (p=g_head;p!=NULL;p=p->next)                     {                         if (max<=p->cylinder&&p->cylinder<g_cylinder)                         {                             max=p->cylinder;                             out=p;                         }                     }                     break;                 case 0:                     direction=1;                     for (p=g_head;p!=NULL;p=p->next)                     {                         if (min>=p->cylinder)                         {                             min=p->cylinder;                             out=p;                         }                     }                     break;             }             break;         }         break;     }     CopyProcess(&local, out);     RemoveProcess(out);     g_cylinder = local.cylinder;     rec0=local.rec; }     // prompt for user input // 0: 驱动调度 // 1： 接受请求 // other: exit void acceptinput()                             /*通过输入0或1选择‘驱动调度’或是‘接受请求’*/ {     int num, done;     do     {         printf("Please enter an integer to choose action to be taken: \n");         printf("0:Accept Request;    1:Driver Dispatch;    otherwise: Exit\n");         printf("num:");         scanf("%d",&num);         done=0;         switch(num){         case 1:    /*驱动调度*/             printf("----------------------------\n");             printf("Dispatching...\n");             qddd();                          /*调用函数进行调度*/ 			break; 		case 0: /*接受请求*/             printf("---------------------------------\n");             printf("Adding a new request...\n");             acceptreq();             break;         default:         	return; 		}         PrintInit();     }while(true); }         int main ()                           /*主程序*/ {     init();     PrintInit();     acceptinput();     return 0; }  注意，你的调度程序保证了柱面小的进程总是优先被调用，这样第172到174行的代码，转贴如下              {                 if (p->cylinder > g_cylinder)                 {                     min = p->cylinder; // what's the purpose of min? value assigned here never used later                     deng1=1;                     break;                 }             } 这个判断总是为真(p->cylinder等于g_cylinder的情形在157-164行处理了), 这样deng1永远不会为0，从而方向永远没有机会改变，总是输出为UP。 这是个逻辑错误，如果你贴出你的qddd的调度算法的描述或伪码，大家会更容易找出问题。  你的代码中修改的部分，比如local直接用一个全局PROC对象而非指针，g_head->next大部分替换为个g_head, 插入节点、删除节点、复制节点的代码抽取出来成为独立的函数等，我个人觉得是更好的设计。为了我个人的理解，对象名也通过查找替代整理了一下，如果原来的名字更方便你自己理解，当然应该保留。  不好意思，C我也不算熟，如果口气有点托大，请谅解。