#include <stdio.h> #include <stdlib.h> typedef struct{ char name[5]; int need_time; int privilege; char state; }NODE; typedef struct node{ NODE data; struct node *link; }LNODE; void delay(int i) { int x,y; while(i--) { x=0 ; while(x < 10000) { y = 0; while(y < 40000) y++; x++ ; } } } void len_queue(LNODE **hpt, NODE x) { LNODE *q,*r,*p; q = *hpt; 8 r = *hpt; p = (LNODE *)malloc(sizeof(LNODE)); p->data = x; p->link = NULL; if(*hpt == NULL) *hpt = p; else { while(q!=NULL && (p->data).privilege < (q->data).privilege) { r = q; q = q->link; } if(q == NULL) r->link = p; else if(r == q) { p->link = *hpt; *hpt = p; }else { r->link = p; p->link = q; } } } void lde_queue(LNODE **hpt, NODE *cp) { LNODE *p = *hpt; *cp = (*hpt)->data; *hpt = (*hpt)->link; free(p); printf("the elected process's name : %s \n",cp->name); } void output(LNODE **hpt) { LNODE *p = *hpt; printf("Name \t Need_time \t privilege \t state\n"); do { 9 printf("%s \t %d \t\t %d \t\t %c \n", (p->data).name,(p->data).need_time,(p->data).privilege,(p->data).state); p = p->link; }while(p!= NULL); delay(4); } int main() { LNODE *head = NULL; NODE curr,temp; printf("The period time is 4s \n"); printf("please input \n"); printf("if need_time = 0,input over\n"); printf("Name\t Need_time\t privilege\n"); while(1) { scanf("%s %d %d", temp.name,&temp.need_time,&temp.privilege); if(temp.need_time == 0) break; temp.state = 'R'; len_queue(&head,temp); } while(head != NULL) { output(&head); lde_queue(&head,&curr); curr.need_time-- ; curr.privilege-- ; if(curr.need_time != 0) len_queue(&head,curr); } return 0; }
时间: 2024-03-26 22:39:52 浏览: 25
以下是将该段 C 代码转换为 Python 的结果:
```python
import time
class Node:
def __init__(self, name, need_time, privilege, state):
self.name = name
self.need_time = need_time
self.privilege = privilege
self.state = state
class LNode:
def __init__(self, data, link):
self.data = data
self.link = link
def delay(i):
while i > 0:
x = 0
while x < 10000:
y = 0
while y < 40000:
y += 1
x += 1
i -= 1
def len_queue(hpt, x):
p = LNode(x, None)
q = hpt
r = hpt
if hpt is None:
hpt = p
else:
while q is not None and p.data.privilege < q.data.privilege:
r = q
q = q.link
if q is None:
r.link = p
elif r == q:
p.link = hpt
hpt = p
else:
r.link = p
p.link = q
return hpt
def lde_queue(hpt):
if hpt is None:
return None, hpt
else:
cp = hpt.data
hpt = hpt.link
print(f"the elected process's name: {cp.name}")
return cp, hpt
def output(hpt):
p = hpt
print("Name\tNeed_time\tPrivilege\tState")
while p is not None:
print(f"{p.data.name}\t{p.data.need_time}\t\t{p.data.privilege}\t\t{p.data.state}")
p = p.link
delay(4)
def main():
head = None
curr = None
print("The period time is 4s")
print("Please input")
print("If need_time = 0, input over")
print("Name\tNeed_time\tPrivilege")
while True:
name, need_time, privilege = input().split()
need_time = int(need_time)
privilege = int(privilege)
if need_time == 0:
break
temp = Node(name, need_time, privilege, 'R')
head = len_queue(head, temp)
while head is not None:
output(head)
curr, head = lde_queue(head)
curr.need_time -= 1
curr.privilege -= 1
if curr.need_time != 0:
head = len_queue(head, curr)
if __name__ == "__main__":
main()
```
注意,由于 Python 的语法和 C 有所不同,因此代码中存在一些细微的差异,例如:
- 在 Python 中,结构体可以使用类来实现;
- 在 Python 中,链表节点可以使用类来实现;
- Python 中没有指针类型,因此需要使用类或者元组来返回多个值;
- Python 中没有 switch-case 语句,可以使用 if-elif-else 语句来代替;
- Python 中字符串可以使用单引号或者双引号来表示。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![image/gif](https://img-home.csdnimg.cn/images/20210720083352.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)