while (current != NULL) { if(current->next!=NULL) { if(recordNode->next->data > pos) { recordNode = recordNode->next; while(count < filesize){ Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = pos++; newNode->next = NULL; count++; current->next = newNode; current = current->next; } current->next = recordNode; break; } current = current->next; recordNode = current; } } printf("文件已成功删除"); } else printf("未找到此文件\n"); goback(thirdpart); }
时间: 2024-01-10 21:02:48 浏览: 53
这段代码实现了将释放的空闲块添加到空闲链表中。在上一个循环中,已经记录了新的空闲块的位置到指针变量 `recordNode` 中,现在需要将其添加到空闲链表中。
首先,使用一个 `while` 循环遍历空闲链表,找到新的空闲块应该插入的位置。如果当前节点的下一个节点的位置大于要插入的位置 `pos`,则表示应该将新的空闲块插入到当前节点和下一个节点之间。在插入过程中,使用一个 `while` 循环向空闲链表中添加新的节点,直到添加了足够数量的节点,使其大小等于文件大小 `filesize`。同时,将变量 `count` 加 1,表示已经添加了一个节点。在添加节点的过程中,需要更新当前节点的 `next` 成员,将其指向新创建的节点。
最后,将新的节点的 `next` 成员指向指针变量 `recordNode`,将其添加到空闲链表中。如果无法找到新的空闲块应该插入的位置,则说明新的空闲块应该添加到链表的尾部,直接在尾部添加即可。
最后,输出提示信息,表示文件已成功删除,并调用 `goback()` 函数返回到删除文件的菜单界面。
相关问题
w: printf("输入文件内容(长度不超过50):"); scanf("%s", content); if(strlen(content)>50) { printf("文件内容过长,请重新输入"); goto w; } F[record].size=strlen(content); //将文件大小记录下来 Node* current = freeList;// 指针指向空闲链表 Node* recordNode = current; int recordData=0; count = 0; 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; } }
这段代码实现了将文件内容写入磁盘的功能。在之前的代码中,已经将文件名记录到了文件数组 `F` 对应位置的 `name` 成员中,现在需要将文件内容写入磁盘,并记录文件的大小和位置。
代码首先输出提示信息,要求用户输入文件内容。然后使用 `goto` 语句实现了一个简单的输入检查,要求文件内容长度不超过 50 个字符。如果输入不符合要求,则返回标签 `w` 重新输入。
接着,将文件的大小记录到文件数组 `F` 对应位置的 `size` 成员中。然后,定义了两个指针变量 `current` 和 `recordNode`,分别指向空闲链表的头节点和记录位置的节点。
接下来,使用一个循环遍历空闲链表,并记录当前可用的连续空间的大小到变量 `count` 中。如果 `count` 等于文件大小,说明找到了足够的连续空间,记录下位置并退出循环。
如果当前空闲块和下一个空闲块是连续的,则将 `count` 加 1。如果不连续,则将 `count` 重置为 1,并记录当前位置到 `recordNode` 和 `recordData` 中。
最后,将指向空闲链表的节点 `recordNode` 的 `next` 成员指向当前节点的 `next` 成员,从而删除已经被占用的空闲块。同时,将文件的位置记录到文件数组 `F` 对应位置的 `pos` 成员中。
数据结构类库积累--题目1.2--链表的基本操作 要求: 1.实现链表的基本操作,包括前插法建立、后插法建立、按值查找、按位置查找、按值插入、按位置插入、前插、后插、按值删除、按位置删除;
### 链表基本操作的实现
#### 前插法建立链表
前插法是在每次创建新节点时将其插入到链表头部之前的位置。这种方法使得最近添加的数据位于最前面。
```c
void createListFront(Node **head, int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->next = (*head);
(*head) = newNode; // 更新头指针指向新的首元结点
}
```
此函数通过分配内存给一个新的节点并初始化其成员变量来工作[^1]。
#### 后插法建立链表
后插法则相反,它总是把新元素附加在当前列表的最后一项之后。
```c
void createListRear(Node **tail, int data){
Node *newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if(*tail == NULL){
*tail = newNode;
(*tail)->next = *tail; // 形成环形结构
}else{
Node* temp=*tail;
while(temp->next != *tail)
temp=temp->next;
temp->next=newNode;
newNode->next= *tail;
*tail=newNode;
}
}
```
上述代码展示了如何构建循环单链表,并保持`*tail`始终指向最新的尾部节点[^2]。
#### 按值查找节点
为了找到具有特定值的第一个匹配项:
```c
Node* findValue(Node *head, int target){
Node *current = head;
do {
if(current->data == target)
return current;
current=current->next;
}while(current!=head);
return NULL; // 如果未发现目标则返回NULL
}
```
这段程序遍历整个链条直到遇到相同数值为止;如果找不到,则最终会回到起点处退出循环。
#### 按位置查找节点
要定位指定索引处的对象可以这样做:
```c
Node* findByPosition(Node *head,unsigned pos){
unsigned count=0;
Node *temp=head;
if(pos==0 || !head)//处理特殊情况:当pos为零或为空表时直接返回头指针
return head;
do{
++count;
if(count==pos)
break;
temp=temp->next;
}while(temp && temp->next!=head);
return ((count==pos)?temp:NULL);
}
```
这里实现了对于任意有效下标的访问逻辑,同时也考虑到了边界条件下的异常情况处理。
#### 插入操作
无论是基于位置还是依据关键字来进行插入动作都涉及到相似的过程—即先确定待插入之处再执行实际链接变更.
##### 按位置插入
```c
bool insertByPos(Node **head,int value,unsigned index){
Node *prev=findByPosition((*head),index-1);
if(!prev&&index>0)return false;//越界检测
Node *newnode=(Node*)malloc(sizeof(Node));
newnode->data=value;
newnode->next=((index<=0)?(*head):prev->next);
if(index<=0){
prev=newnode;
while(prev->next!=(*head))
prev=prev->next;
prev->next=newnode;
(*head)=newnode;
} else {
prev->next=newnode;
}
return true;
}
```
该算法首先调用了findByPosition()辅助功能获取前一节点的信息以便于后续连接调整.
##### 按值插入
```c
bool insertAfterValue(Node **head, int oldValue, int newValue){
Node *target = findValue(*head,oldValue);
if(target==NULL)return false;
Node *newNode =(Node*)malloc(sizeof(Node));
newNode ->data =newValue ;
newNode ->next=target->next ;
target->next=newNode ;
return true;
}
```
这个版本接受两个参数分别代表旧键名和希望追加的新条目内容.
#### 删除操作
同样地,移除也可以按照两种方式完成 —— 或者是指定序号上的项目被清除掉或者是满足一定属性特征的目标对象遭到剔除。
##### 按位置删除
```c
bool deleteByIndex(Node **head,unsigned idx){
if(idx==0){//特殊情形:删除第一个元素
Node *toDel=*head;
if(toDel->next==toDel){
free(toDel );
*head=NULL;
return true;
}
Node *last=*head;
while(last->next!=*head) last=last->next;
*head=(*head )->next ;
last->next=*head;
free(toDel );
return true;
}
Node *pre=findByPosition(*head,idx-1);
if(pre==NULL||!pre->next )return false;
Node *toBeDeleted=pre->next ;
pre->next=toBeDeleted->next ;
free(toBeDeleted );
return true;
}
```
此处特别注意了对首个单元格单独作出安排以适应可能存在的自引用特性.
##### 按值删除
```c
bool removeByKey(Node **head ,int key ){
Node *curr,*prev;
curr=*head ;
bool isFirst=true;
do{
if(curr->data==key){
if(isFirst){
Node *tmp=*head ;
if(tmp->next==tmp){
free(tmp );
*head=NULL;
return true;
}
while(tmp->next!=*head )
tmp=tmp->next ;
*head=(*head )->next ;
tmp->next=*head ;
free(curr );
curr=*head ;
continue;
}
prev->next=curr->next ;
free(curr );
curr=prev->next ;
}else{
prev=curr ;
curr=curr->next ;
isFirst=false;
}
}while(curr!=*head );
return curr!=NULL;
}
```
以上就是关于在一个简单的双向闭合型单向链表中实施增删查改四种核心行为的具体编码实例.
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)