void showNode1(Link head) { Link p = head->next; bool flag=true; while (p&&flag) { cout << "新闻标题:" << p->data.name << "新闻号:" << p->data.num << " 新闻内容:" << p->data.n << endl; p = p->next; if (p == nullptr) { flag = false; } },这段代码在p==nullptr的if判定条件,即使p为空指针了但是这个if条件依旧没法触发,怎么办
时间: 2024-02-25 20:58:44 浏览: 27
在这段代码中,p已经是空指针时,if判定条件确实不会被触发,因此需要另外一种方式来结束循环。可以在while循环中加入一个额外的判定条件,以便在满足该条件时结束循环。例如,可以使用一个计数器来记录已经遍历的节点数,当计数器达到链表长度时,也就完成了遍历,可以结束循环。具体代码如下:
```
void showNode1(Link head) {
Link p = head->next;
bool flag = true;
int count = 0; // 计数器初始化为0
while (p && flag && count < 链表长度) { // 判定计数器是否小于链表长度
cout << "新闻标题:" << p->data.name << "新闻号:" << p->data.num << " 新闻内容:" << p->data.n << endl;
p = p->next;
count++; // 每遍历一个节点,计数器加1
}
}
```
这种方法可以保证遍历每个节点,并且在遍历完成后自动结束循环,不需要使用p==nullptr的判定条件。需要注意的是,需要将链表长度替换成实际的链表长度。
相关问题
void showNode1(Link head) { Link p = head->next; bool flag = true; int count = 0; // 计数器初始化为0 while (p && flag && count < 链表长度) { // 判定计数器是否小于链表长度 cout << "新闻标题:" << p->data.name << "新闻号:" << p->data.num << " 新闻内容:" << p->data.n << endl; p = p->next; count++; // 每遍历一个节点,计数器加1 }},我的意思是请问这里面的链表长度怎么实时计算?
如果要实时计算链表长度,可以在链表的插入和删除操作中更新链表长度,将链表长度存储到链表的结构体中,这样就可以实时计算链表长度了。具体实现方法如下:
```
struct Node {
// 结构体中添加链表长度len
string name;
int num;
string n;
int len;
Node *next;
};
void insertNode(Link head, Node *node) {
node->next = head->next;
head->next = node;
head->len++; // 在插入操作中更新链表长度
}
void deleteNode(Link head, int num) {
Link p = head->next;
Link pre = head;
while (p) {
if (p->data.num == num) {
pre->next = p->next;
delete p;
head->len--; // 在删除操作中更新链表长度
break;
}
pre = p;
p = p->next;
}
}
```
在这里,链表长度len被添加到Node结构体中,并且在插入和删除操作中更新链表长度。这样,在遍历链表时,就可以使用链表的长度len来作为循环终止条件,实时计算链表的长度。具体代码如下:
```
void showNode1(Link head) {
Link p = head->next;
while (p) {
cout << "新闻标题:" << p->data.name << "新闻号:" << p->data.num << " 新闻内容:" << p->data.n << endl;
p = p->next;
}
cout << "链表长度为:" << head->len << endl; // 输出链表长度
}
```
这样,每次遍历链表时,都可以通过链表的长度len来实时计算链表的长度。
bool ff(int number,int size)//首次适应算法 { pNod temp= new Free_Node; Free_Node *p=headNode->next; //从首节点开始 temp->date.number=number; temp->date.size=size; temp->date.flag=BUSY; while(p) { if (p->date.flag==FREE && p->date.size == size )//请求大小刚好满足 { p->date.flag=BUSY; p->date.number=number; return true; } if (p->date.flag==FREE && p->date.size>size)//说明还有其他的空闲区间 { temp->next=p; temp->prior=p->prior; temp->date.begin_address=p->date.begin_address; p->prior->next=temp; p->prior=temp; p->date.begin_address=temp->date.begin_address+temp->date.size;//空闲分区开始地址+此次分配的空间 p->date.size-=size; //分配空闲作业 return true; } p=p->next; } return false; }改为最差适应算法
将该函数改为最差适应算法的代码如下所示:
```
bool wf(int number, int size) {
Free_Node* temp = new Free_Node;
Free_Node* p = headNode->next;
Free_Node* worst_node = nullptr; // 最差适应算法需要记录最大空闲区间
int max_size = -1; // 最差适应算法需要记录最大空闲区间的大小
temp->date.number = number;
temp->date.size = size;
temp->date.flag = BUSY;
while (p) {
if (p->date.flag == FREE && p->date.size >= size) { // 如果空闲区间大小满足要求
if (p->date.size > max_size) { // 找到更大的空闲区间
max_size = p->date.size;
worst_node = p;
}
}
p = p->next;
}
if (worst_node) { // 找到了空闲区间
temp->next = worst_node;
temp->prior = worst_node->prior;
temp->date.begin_address = worst_node->date.begin_address;
worst_node->prior->next = temp;
worst_node->prior = temp;
worst_node->date.begin_address = temp->date.begin_address + temp->date.size;
worst_node->date.size -= size;
worst_node->date.flag = FREE;
return true;
}
return false;
}
```
需要注意的是,最差适应算法需要记录最大空闲区间,因此在遍历空闲区间时需要记录当前的最大空闲区间。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)