【问题标题】:Create and print a Linked list创建和打印链接列表
【发布时间】:2023-03-21 15:21:01
【问题描述】:
#include <iostream>     
      
using namespace std;
struct node   
{      
    int num;   
    node * next;   
};
//Create a list, if list is not empty have at least first middle and last node
void cList (node *);
//inserts a node
void iANode(node *);
//Inserts in order
void iIOrder(node *);

void main(){
    int numM;   
    node *list, *current;   
    list = new node;   
    current = list;
    cout<<"Input"<<endl;
    cin>>numM;

    //creates a list
    void clist(node * record){
        node * head = new node;
        (*head).d1=0;
        (*head).next =0;
        return head;
    }

    //inserts a node
    void iANode(node * record)
    {
        (*newnode).next = (*pred).next;
        (*pred).next= new node;
        ++(*phead).counter;
    }

    //inserts in order
    void iIOrder(node * new node, node*head){
        node *pred = head;
        int i = (*new node).d1;
        node*succ=(*pred);
    }
}

我正在尝试创建一个链表并在每次用户输入后对其进行排序。

目前出现大量编译错误。如果有人可以帮助并指出正确的方向,我将不胜感激。

提前致谢。 编译错误:

“cList”和“iANode”的局部函数定义是非法的

";"在 cList 中的“节点 * 记录”之后丢失

在“void iIOrder(node * new node”)中的节点之后期望“)”

【问题讨论】:

  • 将函数定义移出 main() 函数。
  • 如果你迫切需要一个列表,这里有std::list。但我更推荐std::vector
  • 什么编译错误?他们有什么不明白的地方?
  • void main 不好。 int main 很好。还有很多内存泄漏。
  • 另外(假设您正在尝试学习 C++),您正在像 C 一样编写此代码。相反,您应该有一个带有成员函数的 class

标签: c++ sorting pointers linked-list


【解决方案1】:
  1. 使用 struct node *next,而不是 node *next。同样适用于 *list 和 *current

  2. 某些编译器不接受 void main(),请尝试使用 int main()

  3. 把所有函数实现放在main()之外

  4. 将 *current 和 *list 声明为全局变量(在 main() 之外)

  5. C++ 区分大小写,cList 与 clist 不同。修复 cList 实现

  6. 不是错误,而是使用->操作符:head->num = 0;

  7. 结构节点(函数 cList 和 iIOrder)中没有字段 d1。使用字段编号。

  8. 要使指针无效,请使用 NULL 而不是 0

  9. cList函数无效,但是你返回的是一个指针,改变返回值

  10. 在 iANode 函数中,您使用了大量未声明的变量。您可能想要使用 *list、*current 和 *record。

有一堆分析错误,但您要求语法错误。以后可能会发现更多错误,先尝试修复这些。

【讨论】:

    猜你喜欢
    • 2017-10-29
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多