如何在不破坏列表的情况下修改链接列表元素的值

问题描述 投票:0回答:1

我正在使用链表结构在C语言中研究“先来先服务调度程序”算法。当我尝试对链表的每个元素执行所需的计算时,我遇到一个问题。我似乎无法弄清楚如何修改链接列表的值而不通过迭代遍历列表来破坏列表。有解决此问题的好方法吗?

为了解释我正在做的计算,这是一个示例。

process no. arrivalTime burstTime 
1           0           12
2           7           11
3           13          2

我正在做的是使用此调度算法找到所有模拟过程的完成时间和等待时间。

process no. waitingTime finishTime 
1           0           12   
2           5           23
3           10          25

第一组循环实质上跟踪过程的周转时间。我用它来解决第二个循环集中的完成时间和等待时间。

//Linked List struct
typedef struct process
{
    int processId;
    int arrivalTime;
    int burstTime;
    int ct;
    int waitingTime;
    int finishTime;
    int priorityValue;
    int turnAroundTime;
    struct process* next;
} process;

void firstComeFirstServed(process* list)
{
    int calc;
    process* temp1 = list;
    process* temp2 = list;
    while(temp1!=NULL)
    {
        calc=0;
        while(temp2!=NULL)
        {
            calc =  calc + temp2->burstTime;
            temp1->ct = calc;
            temp2 = temp2->next;
        }
        //Keep iterationg through List here. Not sure how to get changed Elements into orginal list without also breaking it
        temp1 = temp1->next;
    }
    //Have not worked on this part yet, but probably has same issue
    while(list!=NULL)
    {
        list->finishTime = list->ct + list->burstTime;
        list->waitingTime = list->ct - list->arrivalTime;
    }
    //listHead is just a global variable list, so I just want to copy this at the end
    listHead = list;
}

c linked-list scheduler
1个回答
0
投票

基本上,您想实现stack(FIFO或FCFS数据结构)在https://www.geeksforgeeks.org/implement-a-stack-using-singly-linked-list/中是一个示例,如何使用单链表实现此功能。

但是由于您的计算,我认为您必须重新排列列表节点,并且您需要执行此操作[[就地(要求您就地进行此操作,而无需更改节点的值。)

用于重新排列链表

就地

的示例代码在https://www.geeksforgeeks.org/rearrange-a-given-linked-list-in-place/
© www.soinside.com 2019 - 2024. All rights reserved.