与链接列表运算符中的传递值(例如C 标头文件中的删除(front))中,我如何使用布尔函数
How do I work with boolean functions in relation to passing value within linked list operators such as remove(front) in a C++ header file?
我不断遇到编译器错误,专门针对我的功能而没有分开。
这是我的标题文件声明
#ifndef LL_H
#define LL_H
// include this library to use NULL, otherwise use nullptr instead
#include <cstddef>
// include iostream so anything that includes this file can use cout
#include <iostream>
// Struct which will be the building block of our list
struct node{
int val;
node* next;
node* prev;
};
Here is the linked list class definition
// Linked list class definition
class LL{
public:
LL();
void prepend(int);
void append(int);
void remove(int);
bool removeFront();
bool removeBack();
node* search(int);
void print();
void deleteList();
private:
node* head;
};
#endif
Here is the header file that contains the linked list functions.
#include "ll.h"
LL::LL()
{
head = NULL;
}
void LL::prepend(int num)
{
node* newNode = new node;
newNode->val = num;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
return;
}
else
{
newNode->next = head;
head = newNode;
newNode = NULL;
}
}
void LL::append(int num)
{
node* newNode = new node;
newNode->val = num;
newNode->next = NULL;
//If there are no nodes, make newNode the first node
if (head == NULL)
{
head = newNode;
newNode->next = NULL;
}
else
{
newNode->next = head;
}
}
void LL::remove(int num){
//This function searches for a node with num as its value, if found, it is deleted from the list
node* second = head;
node* first = head->next;
if (head == NULL)
{
return;
}
else if (head->val == num)
{
node* temp = head;
head = head->next;
delete temp;
}
while (first&&first->val != num)
{
second = first;
first = first->next;
}
if (first)
{
second->next = first->next;
delete first;
}
}
bool LL::removeFront()
{
node* newNode = new node;
head = head->next;
if (head == NULL)
{
newNode = head;
head = head->next;
delete newNode;
}
}
bool LL::removeBack()
{
if (head == NULL)
{
return (false);
}
else
{
node* newNode = new node;
while(newNode->next)
{
newNode->next = newNode;
}
delete newNode;
return (true);
}
}
node* LL::Search(int num)
{
node* newNode = head;
while (newNode == NULL)
{
if (newNode->num == num)
{
return newNode;
}
newNode = newNode->next;
}
return NULL;
}
void LL::print()
{
node* temp = head;
while(temp !=NULL)
{
temp = temp->next;
}
}
void LL::deleteList()
{
node* temp = head->next;
while(temp != NULL)
{
delete(head);
head = temp;
temp = temp->next;
}
}
Do I turn my header bool functions into integer passes? or do I do something else to prevent the compile errors? I can't use any standard functions in a standard library.
我正在使用此main.cpp来测试我们从第一列输入值的代码,然后是一个数字,该数字指示函数,然后是一个整数,该整数传递到该函数以操纵链接的列表。<<<<<</p>
#include <fstream> // Include to use ifstream
#include "ll.h" // Include so can access our class
using namespace std; // Include so we don't need to put std:: infront
// of cout and endl
int main()
{
LL myList;
ifstream input;
int cmd, argument, ret;
node* searchResult = NULL;
input.open("cmd.txt");
// while there is something to read from the file, read
while (input >> cmd)
{
// switch on the command we read from the file
switch (cmd)
{
// if the cmd requires a parameter, read it from the file and call the
// associated function
case 1:
input >> argument;
myList.prepend(argument);
cout << "Prepended " << argument << endl;
break;
case 2:
input >> argument;
myList.append(argument);
cout << "Apended " << argument << endl;
break;
case 3:
if(myList.removeFront())
{
cout << "Succesfully removed the front of the listn";
}
else
{
cout << "List is empty, nothing to removen";
}
break;
case 4:
if(myList.removeBack())
{
cout << "Succesfully removed the back of the listn";
}
else
{
cout << "List is empty, nothing to removen";
}
break;
case 5:
input >> argument;
searchResult = myList.search(argument);
if(NULL != searchResult)
cout << "Found " << searchResult->val <<" in list!"<<endl;
else
cout << "Did not find " << argument << " in the list"<<endl;
break;
case 6:
myList.print();
break;
case 7:
input >> argument;
cout << "Attempting to remove " << argument << endl;
if(myList.remove(argument))
{
cout << "Succesfully removed the element from the listn";
}
else
{
cout << "Could not remove the element from the listn";
}
break;
}
}
input.close();
return 0;
}
这是定义搜索功能的正确方法
node* LL::search(int num)
{
node* newNode = new node;
newNode = head;
while (newNode->next != NULL)
{
if (newNode->val == num)
{
return newNode;
}
newNode = newNode->next;
}
return NULL;
}
相关文章:
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 使用函数"remove"删除重复元素
- 如何从多映射中删除特定的重复项
- 运算符C++ "delete []"仅删除 2 个前值
- 删除指向指针的指针是运行时错误吗
- 将指针设置为"nullptr"并不能防止双重删除?
- 为什么示例代码访问IUnknown中已删除的内存
- 如何通过 getter 函数删除矢量的元素?
- 从控制台中删除最后打印的元素
- C++中的线程安全删除
- 与链接列表运算符中的传递值(例如C 标头文件中的删除(front))中,我如何使用布尔函数
- 删除LinkedList中的front元素会产生垃圾