while (current != NULL) { if(current->next!=NULL) { if(count == F[record].size) { recordNode->next = current->next; break; } if(current->next->data - current->data == 1) { count += 1; } else { count = 1; recordNode = current; recordData = current->next->data; } current = current->next; } }
时间: 2024-01-17 14:03:48 浏览: 28
这段代码实现了向空闲链表中添加新的空闲块。首先使用一个循环遍历空闲链表,找到新的空闲块应该插入的位置。如果当前节点和下一个节点的位置是连续的,则将变量 `count` 加 1,表示当前连续的空闲块的大小。
如果当前节点和下一个节点的位置不连续,则将变量 `count` 重置为 1,并记录当前位置到指针变量 `recordNode` 中,记录当前写入的文件内容的位置到变量 `recordData` 中。
如果 `count` 等于文件大小,则说明找到了足够的连续空间,将指针变量 `recordNode` 的 `next` 成员指向当前节点的 `next` 成员,从而删除已经被占用的空闲块。使用 `break` 语句退出循环,因为已经找到了足够的连续空间。
最后,如果无法找到足够的连续空间,则说明空闲块应该添加到链表的尾部,直接在尾部添加即可。
相关问题
帮我修改一下以下代码int FirstAdjVertex(AdjList G,int v) { ArcNode *p=G.vertex[v].firstarc; if(p!=NULL) { return p->adjvex; } return -1; } int NextAdjVertex(AdjList G,int v,int w) { ArcNode *p=G.vertex[v].firstarc;; while(p!=NULL&&p->adjvex==w) { p=p->nextarc; } if(p==NULL) { return -1; } return p->nextarc->adjvex; }
以下是修改后的代码:
```c
int FirstAdjVertex(AdjList G, int v) {
ArcNode *p = G.vertex[v].firstarc;
if (p != NULL) {
return p->adjvex;
}
return -1;
}
int NextAdjVertex(AdjList G, int v, int w) {
ArcNode *p = G.vertex[v].firstarc;
while (p != NULL && p->adjvex != w) {
p = p->nextarc;
}
if (p == NULL || p->nextarc == NULL) {
return -1;
}
return p->nextarc->adjvex;
}
```
修改点:
1. 第二个函数中的 `while` 循环条件应该是 `p->adjvex != w`,表示找到当前节点的下一个节点。
2. 在第二个函数中,需要判断 `p->nextarc` 是否为 `NULL`,如果是,则说明当前节点是最后一个节点,返回值为 `-1`。
void Create(LinkList &Head, int score, int rank, int sex, char name[], char college[], char item[]) { LinkNode *p = Head->next, *s; //如果链表为空,则做初始化链表 if (p == NULL) { s = (LinkNode *)malloc(sizeof(LinkNode)); assert(s != NULL); s->score = score; s->rank = rank; s->sex = sex; strcpy(s->name, name); strcpy(s->college, college); strcpy(s->item, item); s->next = NULL; Head->next = s; } else { while (p->next != NULL) { p = p->next; } s = (LinkNode *)malloc(sizeof(LinkNode)); assert(s != NULL); s->score = score; s->rank = rank; s->sex = sex; strcpy(s->name, name); strcpy(s->college, college); strcpy(s->item, item); s->next = NULL; p->next = s; } }
这是一个函数,用于向链表中添加节点(学生信息),函数的参数包括链表头指针Head,学生的成绩score,名次rank,性别sex,姓名name,学院college和项目item。函数的具体实现如下:
1. 首先判断链表是否为空,如果为空则新建一个节点作为头节点,并将新节点作为第一个节点。
2. 如果链表不为空,则遍历链表,找到链表的末尾节点,将新节点添加在其后面。
3. 新建节点时,需要动态分配内存空间,判断是否分配成功。
4. 将学生的信息存储到节点中,包括成绩、名次、性别、姓名、学院和项目。
5. 将新节点添加到链表末尾,即链表尾部节点的next指针指向新节点。
函数的完整代码如下:
```c
void Create(LinkList &Head, int score, int rank, int sex, char name[], char college[], char item[]) {
LinkNode *p = Head->next, *s;
// 如果链表为空,则做初始化链表
if (p == NULL)
{
s = (LinkNode *)malloc(sizeof(LinkNode));
assert(s != NULL);
s->score = score;
s->rank = rank;
s->sex = sex;
strcpy(s->name, name);
strcpy(s->college, college);
strcpy(s->item, item);
s->next = NULL;
Head->next = s;
}
else
{
while (p->next != NULL)
{
p = p->next;
}
s = (LinkNode *)malloc(sizeof(LinkNode));
assert(s != NULL);
s->score = score;
s->rank = rank;
s->sex = sex;
strcpy(s->name, name);
strcpy(s->college, college);
strcpy(s->item, item);
s->next = NULL;
p->next = s;
}
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.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)