与链接列表运算符中的传递值(例如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?

本文关键字:删除 front 函数 布尔 何使用 文件 运算符 列表 链接 例如      更新时间:2023-10-16

我不断遇到编译器错误,专门针对我的功能而没有分开。

这是我的标题文件声明

    #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;
    }