algorithm - 链表错误

标签 algorithm data-structures linked-list

我只是在其中一个名为 name 的结构上测试函数,但它不会到达它们。 这是到目前为止的完整代码:

更新:

#include <iostream>
#include <string>
#include <cstdlib>
#include <cstddef>
using namespace std;

struct Node{
    string name;
    string address;
    int phoneNum;
    struct Node* next;
};

Node insertInOrder(Node p, string theName);
void push(struct Node*& head, string theName);


int main(){

    cout<<"\tEnter S or s to show the list contents \n"
        <<"\tEnter A or a to add a node to the list \n"
        <<"\tEnter D or d to delete a node from the list \n"
        <<"\tEnter Q or q to quiet the program \n"
    <<"Make your selection: "<<endl;

    struct Node* newNode = new Node;
    push(newNode, "yeah");

    cout<<newNode;

    return 0;

}

void push(struct Node*& head, string theName){
    struct Node* newNode = new Node;
    newNode->name = theName;
    newNode->next = head;
    head = newNode;
}

Node insertInOrder(Node p, string theName){
    if(p == NULL || p.name >= theName){
        return new Node(p, theName);
    }
    else{
            p.next = insertInOrder(p.next, theName);
            return p;
        }
    }

我收到一条错误消息:“sizeof”无效应用到此代码的不完整类型“Node”:

    void push(struct Node*& head, string theName){
    struct Node* newNode = malloc(sizeof(struct Node));
    newNode->name = theName;
    newNode->next = head;
    head = newNode;
}

我正在尝试将此代码转换为我的代码,但出现错误:

 Node insertInOrder( int k, Node p ) {
   if( p == " " || p.item >= k ) 
      return new Node( k, p ); 
   else {
      p.next = insertInOrder( k, p.next ); 
      return p;
   }
} 

我是这样翻译的:

Node insertInOrder(Node p, string theName){
    if(p == " " || p.name >= theName){
        return new Node(p, theName);
    }
    else{
            p.next = insertInOrder(p.next, theName);
            return p;
        }
    }

这是这段代码的错误:

 if(p == " " || p.name >= theName){
        return new Node(p, theName);

错误:

- comparison with string literal results in unspecified behaviour [-Waddress]
- request for member ‘name’ in ‘p’, which is of pointer type ‘Node*’ (maybe you meant to use ‘-
 >’ ?)
- comparison between distinct pointer types ‘Node*’ and ‘const char*’ lacks a cast [-
 fpermissive]

p.next = insertInOrder(p.next, theName); 返回 p;

错误:

Invalid arguments ' Candidates are: Node insertInOrder(Node, std::basic_string<char,std::char_traits<char>,std::allocator<char>>) '
- could not convert ‘p.Node::next’ from ‘Node*’ to ‘Node’

最佳答案

几点:

  • 忘记 malloc,因为您正在使用 C++ 并使用 newdelete
  • 你不需要在每次使用时再次指定 node 是一个结构体,所以 sizeof(Node) 就足够了,但你不会直接使用 malloc
  • 你的函数 Node insertInOrder(Node p, string theName) 接受一个具体的 Node 并返回一个具体的 Node 但你的结构中的 next 字段是一个指向 Node,我想你应该在你使用的东西上保持一致,因为你使用的是使用指针的链表更合适
  • 不能在值和字符串文字之间直接使用比较运算符 (p == ""),您应该只检查 name 字段

关于algorithm - 链表错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13515492/

相关文章:

c++ - "inherit"另一个关系 (1 :N) relationship

c - 拆分随机链表然后对它们进行排序

string - 在php中对字符串编号进行排序的算法

java - 为什么这个算法的Big-O是N^2*log N

mysql - 如何将经常访问的数据放入数据库中的 "quick access"区域

python - 是否有可排序和可搜索的 Python 数据结构?

algorithm - 如何在没有蛮力方法的情况下有效地组合不相交的集合?

algorithm - 'c' 中的 "f(n) <= c g(n)"是什么?你是怎么找到它的?

python - 使用java一段时间后学习python,为什么我的 "stutter"链接列表函数不起作用?

c - 下面合并k个链表的时间复杂度