尝试删除所选节点的双向链表
Doubly linked list trying to delete selected node
我试图使用双向链表并从文本文件中读入以创建非常基本的缓存。目前缓存容量为 5,我从 txt 文件中读取了 20 个值 1-20。当我尝试删除头部然后打印链表时,它会打印 0,2,3-20,因此将 1 替换为 0。据我了解,我需要创建一个临时节点来设置为头部值,然后将头部指向下一个节点,最后删除临时节点,这就是我所做的,但我显然错过了一些重要的东西。
int main(int argc, char** argv) {
node* head;
node* tail;
node* n;
int capacity = 5;
int size = 0;
std::string fileName;
std::ifstream inFile;
int start_block, num_blocks, ignore, req_num;
std::cout << "Project 3 milestone: LRU" << std::endl;
std::cout << "Enter filename: " << std::endl;
std::cin >> fileName;
inFile.open(fileName.c_str(), std::ifstream::in);
if (!inFile) {
std::cerr << "Cannot open file! " << fileName << std::endl;
}
while(inFile.is_open()) {
inFile >> start_block >> num_blocks >> ignore >> req_num;
n = new node;
n->data = start_block;
n->prev = NULL; // 1st node
head = n;
tail = n;
size++;
while(!inFile.eof()) {
inFile >> start_block >> num_blocks >> ignore >> req_num;
n = new node;
n->data = start_block;
n->prev = tail;
tail->next = n;
tail = n;
size++;
//std::cout << start_block << " " << num_blocks << " " << ignore << " " << req_num << std::endl;
if (size == capacity) {
cout << "Reached capacity:" << capacity << endl;
// this is where I would delete the head node
}
}
inFile.close();
}
PrintForward(head);
//PrintReverse(tail);
SearchRecursive(head,18);
DeleteHead(head, tail);
PrintForward(head);
//DeleteHead(head, tail);
//PrintForward(head);
return 0;
}
void SearchRecursive(node* ptr, int searchValue) {
if(ptr == NULL) { // if we pssed through list and didnt find value
cout << searchValue << " was NOT found in the listn";
}
else if (ptr->data == searchValue) { // if we DID find it
cout << searchValue << " IS in the list!n";
}
else {
SearchRecursive(ptr->next, searchValue); // else search recursively
}
}
void DeleteHead(node* head, node* tail) {
if (head == tail) { // if only 1 element
cout << "Only 1 element here" << endl;
delete head;
head = NULL;
tail = NULL;
}
else {
cout << "More than 1 element here" << endl;
node *temp = head;
head = head->next;
delete temp;
}
}
编辑:我改进了SearchRecursive功能,现在可以删除除头部和尾部之外的节点。这是我正在使用的:
void SearchRecursive(node* ptr, int searchValue) {
if(ptr == NULL) { // if we pssed through list and didnt find value
cout << searchValue << " was NOT found in the listn";
}
else if (ptr->data == searchValue) { // if we DID find it
cout << searchValue << " IS in the list!n";
ptr->prev->next = ptr->next;
ptr->next->prev = ptr->prev;
delete ptr;
}
else {
SearchRecursive(ptr->next, searchValue); // else search recursively
}
}
因为您删除了头部指针,因此在调用DeleteHead
后头部指针现在无效,因为指针没有更改,函数中的更改仅将值反映到函数内部的 head
参数指向 where 的位置,而不会反映在此函数外部,您想要的是更改传递的指针的值, 这是通过引用完成的。所以你的函数现在正在获取对指针的引用,并且函数的签名现在是
void DeleteHead(node** head, node* tail)
并在函数内部执行*head = (*head)->next;
或者你可以从函数中返回新的头部值
node* DeleteHead(node* head, node* tail)
我最终所做的是上述答案和评论的结合。这是我现在用于删除头的代码
void DeleteHead(node*& head, node* tail) {
cout << "Deleting head..." << endl;
node* temp = head;
head = head->next;
size--;
delete temp;
}
相关文章:
- 为什么我的双向链表删除函数会删除多个节点?
- 在C++中删除双向链表的头节点后出现访问冲突异常
- 双向链表 - 无法删除第一个节点
- 双向链表 std::unique_ptr 类在节点删除时无法按预期工作
- 在给定节点之后插入双向链表中的节点
- 在双向链表中给定节点之后删除节点
- 尝试删除所选节点的双向链表
- 双向链表插入方法实现 - 正在搜索哪个节点
- 为什么我的函数没有打印出双向链表中的第一个节点?
- 使用类从双向链表中删除节点
- 从双向链表末尾删除节点时出现分段错误
- 删除双向链表 (C++) 中的节点
- 使用双向链表中的指针交换节点
- 在双向链表中插入节点
- 删除双向链表 (C++) 的最后一个节点
- 在循环双向链表的第一个节点之前/之后插入的算法是什么?
- 在双向链表的中间插入一个节点
- 删除循环双向链表中的节点
- 双向链表节点的下一个是私有的
- 双向链表节点交换问题