读书人

链表有关问题C语言模拟时间片轮转法

发布时间: 2012-02-17 17:50:42 作者: rapoo

链表问题,C语言模拟时间片轮转法,很急!
//时间片轮转法,C语言实现
#define TIME 100 //PRINT time slice(时间片)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <windows.h>
//struct of process
typedef struct node
{
char name[10]; //process name
int have; //have resource
int need; //need resource(资源)
struct node *next; //pointer area
}Squeue;

void CreatePro(Squeue *S)
{
//Initialize the process
system( "cls ");
Squeue *p,*q;
char cho;
p=S=(Squeue *)malloc(sizeof(Squeue)); //create head node
printf( "Please enter the process information!\n ");
for(;;)
{
printf( "Now enter 'C ' to continue and 'E ' to exit: ");
scanf( "%c ",&cho);
getchar();
cho=toupper(cho);
if(cho== 'C ') //Input the infomation of process
{
system( "cls ");
q=(Squeue *)malloc(sizeof(Squeue));
printf( "Process name: ");
scanf( "%s ",&q-> name);
getchar();
printf( "process need: ");
scanf( "%d ",&q-> need);
getchar();
q-> have=0;
p-> next=q;
p=q;
}
else if(cho== 'E ')
{
break;
}
else
{
printf( "Error...\n ");
continue;
}
}
p-> next=NULL;
}

void ProWork(Squeue *S)
{
//begin to work
system( "cls ");
Squeue *p,*q,*rear;
int time;
rear=p=S; //point the head
while(rear-> next!=NULL)rear=rear-> next; //rear point the end
for(;;) //process change
{
if(p-> next==NULL){printf( "break\n ");break;} //have no any process yet


q=p-> next;
for(time=0;time <TIME;time++) //every process work for TIME time
{
q-> have++;
if(q-> have> =q-> need) //process completed
{
printf( "%s completed\n ",q-> name);
p-> next=q-> next; //change pointer area
//free(q); //free memory
}
}
rear-> next=q;
p-> next=q-> next;
while(rear-> next!=NULL)rear=rear-> next;
printf( "Another Process...\n ");
}
printf( "All Processes Completed...\n ");
}

int main()
{
Squeue *L;
int c;
while(1)
{
printf( "----时间片轮转法模拟实验----\n ");
printf( "1.创建进程\n ");
printf( "2.进程开始\n ");
printf( "0.退出\n ");
printf( "----------------------------\n ");
printf( "Choose Number: ");
scanf( "%d ",&c);
getchar();
if(c==1)CreatePro(L);
if(c==2)ProWork(L);
if(c==0)break;
}
getchar();
getchar();
return 0;
}


我用C语言实现时间片轮转法的模拟,主要是建立一个链表,一个个的轮着来,时间片用完了就放到链表的末尾去,rear始终指向链表最后一个结点。
请问我这里建链表有问题吗?
创建了几个结点后,在ProWork阶段时候就发生 .exe错误了
再就是,如果我主函数里面定义的是L,而不是*L的话,用Create(&L),那么程序运行ProWork的时候好想始终认为S-> next这个结点是NULL,不管创建多少歌结点都是一个样?到底怎么回事啊?
经过Dev-cpp调试,没有语法错误。

[解决办法]
哦?

CreatePro(L); 没有修改main里面的局部变量L。
应该把接口改一下,例如: L = CreatePro();

ProWork(L)调用时,L就不知道指到哪里了

读书人网 >软件架构设计

热点推荐