【问题标题】:Unable to add a node in a linked list无法在链表中添加节点
【发布时间】:2022-01-07 10:39:11
【问题描述】:

我尝试修改代码几次,但无法在代码中找到错误。我的程序正在运行,但我很难将节点添加到正确的位置。

#include<iostream>

using namespace std;

struct Node
{
    int data;
   struct Node *next;
}*HEAD=NULL;

void create(int a[],int n)
{
    struct Node *p;
    cout<<"Enter the number of elements of LL you want to display "<<endl;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        if(i==0)
        {
            HEAD=new Node;
            p=HEAD;
            p->data=a[0];
        }
        else
        {
            p->next=new Node;
            p=p->next;
            p->data=a[i];
        }
        
        
          p->next=NULL;
    }
 
}   
 void insertion(struct Node *p,int pos,int x)
{
    struct Node *t;
    int i,n;
    if(pos<0||pos>n)
    cout<<"Invalid position "<<endl;
    t=new Node;
     t->data=x;
    // t->next==NULL;
     if(pos==0)
     {
         t->next=HEAD;
         HEAD=t;
     }
     
    else
    for(i=0;i<pos-1;i++)
    {
        p=p->next;
        t->next=p->next;
        p->next=t;
    }
    
}
void display(struct Node *HEAD)
{   
    struct Node *p;
    p=HEAD;
    while(p!=NULL)
    {
        cout<<p->data;
        p=p->next;
        
    }
}
int main()
{
    struct Node *temp;
    int n;
    cout<<"enter the value of n "<<endl;
    cin>>n;
    int a[n];
    cout<<"Array elements are "<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    create(a,n);
   insertion(HEAD,1,15);
    display(HEAD);
}

【问题讨论】:

  • 首先,请尽早测试,经常测试。不要在没有构建(启用额外的警告并且您将其视为错误)和测试的情况下编写大部分代码。这使得更容易理解问题出现的时间和地点,以及调试代码(例如,在监视变量及其值的同时,使用调试器逐语句逐句执行代码)。调试时的另一个提示:使用铅笔和纸来可视化您拥有的结构、节点和链接。将节点绘制为正方形,将链接绘制为箭头,在对它们进行操作时擦除和重绘箭头。
  • 仅供参考,在 C++ 中,声明变量或参数时不需要关键字 struct
  • @Rohan Shahi 此代码 sn-p int i,n; if(posn) 没有意义,因为变量 n 未初始化。
  • 既然n已经作为参数提供,并且已经在主程序中输入了,为什么还要输入n

标签: c++ algorithm data-structures linked-list


【解决方案1】:

有几个问题:

  • insertion 中,您应该将以下语句移出循环,因为它们应该只发生一次,而且当循环根本不迭代时也应该发生一次。这就是为什么在传递位置 1 时看不到添加节点的原因:

    t->next = p->next;
    p->next = t;
    
  • if(pos&lt;0||pos&gt;n) 不可靠,因为 n 未初始化。你根本不需要n。您可以在遍历列表并在到达所需位置之前碰到列表末尾时进行第二次测试。

  • 不要在create 中要求输入n,因为您已经在主程序中获得了此输入。

  • 您的display 函数将所有输出粘贴在一起,因此您无法真正看到哪个是哪个。您应该使用像空格这样的分隔符来分隔值。

  • 不要在列表中使用全局变量。这是一种不好的做法,并且不允许您在拥有多个列表时使用这些功能。

  • 不要用所有大写字母命名该变量。这通常是为常量保留的。

  • 在 C++ 中,在分配/比较节点指针时使用 nullptr 而不是 NULL

  • 当发现位置超出范围时,应退出该函数。

  • 由于head 不是全局变量,所以让create 返回它,并通过引用传递给insertion

  • 注意Why is "using namespace std;" considered bad practice?

这是一个更正的版本:

#include<iostream>

struct Node {
    int data;
    Node *next;
};

// Let function return the new linked list
Node *create(int a[], int n) {
    Node *head = nullptr; // local variable & not CAPS
    Node *p;
    if (n == 0)
        return nullptr;
    // No input should be asked here
    head = new Node;
    p = head;
    p->data = a[0];
    for (int i = 1; i < n; i++) {
        p->next = new Node;
        p = p->next;
        p->data = a[i];
    }
    p->next = nullptr;
    return head;
}

void insertion(Node **head, int pos, int x) {
    if (pos < 0) {
        std::cout << "Invalid position " << std::endl;
        return; // Should quit!
    }
    Node *t = new Node;
    t->data = x;
    if (pos == 0) {
        t->next = *head;
        *head = t;
    } else {
        Node *p = *head;
        for (int i = 0; i < pos - 1; i++) {
            p = p->next;
            // Without n you can test out of range here:
            if (p == nullptr) {
                std::cout << "Invalid position" << std::endl;
                delete t;
                return;
            }
        }
        // The following should happen outside the loop
        t->next = p->next;
        p->next = t;
    }
}

void display(Node *head) {   
    Node *p = head;
    while (p != nullptr) {
        std::cout << p->data << " "; // separate with space
        p = p->next;
    }
    std::cout << std::endl; // Newline
}

int main() {
    int n;
    std::cout << "Enter the value of n " << std::endl;
    std::cin >> n;

    int a[n];
    std::cout << "Array elements are " << std::endl;
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    // Define head as local variable - no CAPS
    Node *head = create(a,n);
    display(head);
    insertion(&head, 1, 15);
    display(head);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    • 2020-05-03
    相关资源
    最近更新 更多