我正在编写一个程序来执行先到先得的服务算法,它显示分段错误

问题描述 投票:0回答:1
#include<stdio.h>
#include<stdlib.h>
struct pro{
int at;
int cpu;
struct pro *next;
};
struct pro *head=NULL;

int main()
{
 int n,i,t,c;
 struct pro *temp;
 struct pro *newnode;
 printf("/nenter no. of processes=");
 scanf("%d",&n);
 for (i=0;i<=n;i++)
 {
   newnode=((struct pro *)malloc(sizeof(struct pro)));
   printf("/n enter arrival time=");
   scanf("%d",&t);
   newnode->at=t;
   printf("/n enter cpu burst time=");
   scanf("%d",&c);
   newnode->cpu=c;
   newnode->next=NULL;
   if (head==NULL)
   {`
     head=newnode;
   }
   else
    {temp=head;:
     while(temp!=NULL)
     {
       temp=temp->next;
      }
     newnode=temp->next;
    }
 }
  temp=head;
   while(temp!=NULL)
   {
     printf("\narrival time =%d",temp->at);
     printf("\ncpu birst time=%d",temp->cpu);
   }
 return 0;
}

~    

我正在编写一个程序首先进入并显示为先服务算法细节,但是似乎程序只是在我进入计算部分之前才被卡在这里。它显示了错误代码转储,我在这里做什么错了?

〜〜

c linked-list cpu-usage
1个回答
0
投票

下面是代码的一部分,正在遍历直到链表末尾并尝试访问temp,即NULL并尝试执行(NULL)->next

© www.soinside.com 2019 - 2024. All rights reserved.