C++ 试图删除选定节点的双链接列表
我试图使用一个双链接列表并从文本文件中读取来创建一个非常基本的缓存。目前,缓存容量为5,我从txt文件中读取了20个值,即值1-20。当我尝试删除标题,然后打印链接列表时,它会打印0,2,3-20,因此将1替换为0。 据我所知,我需要创建一个临时节点来设置head值,然后将head指向下一个节点,最后删除temp节点,这就是我所做的,但我显然遗漏了一些重要的内容C++ 试图删除选定节点的双链接列表,c++,doubly-linked-list,C++,Doubly Linked List,我试图使用一个双链接列表并从文本文件中读取来创建一个非常基本的缓存。目前,缓存容量为5,我从txt文件中读取了20个值,即值1-20。当我尝试删除标题,然后打印链接列表时,它会打印0,2,3-20,因此将1替换为0。 据我所知,我需要创建一个临时节点来设置head值,然后将head指向下一个节点,最后删除temp节点,这就是我所做的,但我显然遗漏了一些重要的内容 int main(int argc, char** argv) { node* head; node* tail; node* n;
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 list\n";
}
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;
}
}
int main(int argc,char**argv){
节点*头;
节点*尾部;
节点*n;
int容量=5;
int size=0;
std::字符串文件名;
std::ifstream-infle;
int start_block,num_block,ignore,req_num;
std::cout>ignore>>请求数量;
n=新节点;
n->data=start\u块;
n->prev=NULL;//第一个节点
水头=n;
尾=n;
大小++;
而(!infle.eof()){
填充>>开始块>>数量块>>忽略>>请求数量;
n=新节点;
n->data=start\u块;
n->prev=尾部;
tail->next=n;
尾=n;
大小++;
//std::cout因为您删除了head指针,在调用DeleteHead
之后,head指针现在无效,因为指针没有更改,函数中的更改只反映函数内head
参数指向的位置的值,而不反映在此函数外,您需要更改传递的指针的值,这是通过引用完成的。
所以你们的函数现在得到了指针的引用,函数的签名现在是
void DeleteHead(node** head, node* tail)
在函数内部执行*head=(*head)->next;
或者您可以从函数返回新的头值
node* DeleteHead(node* head, node* tail)
我最后做的是把上面的回答和评论结合起来。
这是我的DeleteHead代码
void DeleteHead(node*& head, node* tail) {
cout << "Deleting head..." << endl;
node* temp = head;
head = head->next;
size--;
delete temp;
}
void DeleteHead(节点*&头,节点*尾){
CUT是否已经尝试用调试器调试?因为它是C++而不是C,所以您可能希望使用实际的C++引用语义,并使用<代码>和/代码>语法。
void DeleteHead(node*& head, node* tail) {
cout << "Deleting head..." << endl;
node* temp = head;
head = head->next;
size--;
delete temp;
}