【问题标题】:Remove Singly Linked List at nth position删除第 n 个位置的单链表
【发布时间】:2020-11-17 22:05:25
【问题描述】:
# include <iostream>
using namespace std;
class Node
{
public:
    int d;Node*temp1;
    Node*next;Node*temp2;
};
void insert(Node*&head,int x) 
{
    Node*node = new Node(); // allocate memory 2 node let node be an abstract data
    node->d = x; // define data in the new node as new data (saving data define in there)
    node->next = head; // Let next of the new node as head
    head = node; // let pointer name head point new node
}
void print(Node*node) 
{ 
    while (node != NULL) 
    { 
        cout<<' '<<node->d; 
        node = node->next; 
    } 
}
void Delete(Node*&head,int n) // Delete node at position
{
    int i;Node*node=head;// temp1 points 2(n-1)th
    if(n==1)
    {
        head = node->next; // head now points 2 second node.
        return;
    }
    for(i=0;i<n-2;i++)
    {
        head = node->next;
    } // temp1 points 2 (n-1)th Node
    Node*nnode= node->next; // nth node temp1=node temp2=nnode
    node-> next = nnode->next; //(n+1)th Node
    
}
int main()
{
    Node*head = NULL; // Start with empty List
    int a,n,i,x;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>x;
        insert(*&head,x);  
    }
    cout<<"Enter a position:";
    cin>>a;
    Delete(head,a);print(head);
}

输出是:

3 // how many number that singly linked list can received
1 2 3 // define many numbers
Enter a position : 1
2 1 // false output it should be 2 3

输出应该是:

3
1 2 3
Enter a position : 1
Linked List is 1->2->3
position 1 is remove // at any position we want 2 remove it will show that position we remove
2->3  
Enter a position : 4
No data at 4th position
Linked List is 2->3

【问题讨论】:

  • *&amp;head 完全等于 head
  • 你有什么限制?你可以使用智能指针吗?
  • 顺便说一句,在你输入1 2 3后,链表是3-&gt;2-&gt;1而不是1-&gt;2-&gt;3

标签: c++ class singly-linked-list function-definition


【解决方案1】:

Delete 函数中有循环

for(i=0;i<n-2;i++)
{
    head = node->next;
}

因为您通过引用传递head,所以您使用此循环主动销毁列表。此外,由于您之前有node = head,因此在第一次迭代中分配实际上是head = head-&gt;next

您需要使用变量node 而不是head

for(i=0;i<n-2;i++)
{
    node = node->next;
}

您还需要防止超出列表末尾:

for(i = 0; (i < n - 2) && (node->next != nullptr) ;i++)

【讨论】:

  • 它说 nullptr 没有在这个范围内声明我如何声明 nullptr 我应该使用 Null 并且输出是这个 3(限制数)1 2 3 输入一个位置:1 然后我得到 3 2 1这不是 2 3
  • @Lizzar_00 那么你有一个非常旧的 C++ 编译器,请改用0
  • 其实当我进行测试或做 c++ 作业时,大学让我使用 dev-c,所以如果它是 2 旧的或者我的代码是 2 旧的,你并不感到惊讶,哈哈
  • 我有一个问题,教授要我 2 删除未在单链表中定义的位置,并让 2 在该位置发出消息没有数据,并在删除我开始的第 n 个位置后显示剩余的位置与 1
【解决方案2】:

对于初学者来说,单链表的节点声明有多余的数据成员temp1temp2,这没有意义。

声明看起来像

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

在这种情况下,函数insert(你可以这样调用

insert(head,x); 

而不是

insert(*&head,x); 

你正在做的)看起来像

void insert( Node * &head, int x )
{
    head = new Node { x, head };
}

在 C++(和 C)中,索引从 0 开始。因此函数 delete 也应接受从 0 开始的索引。相应参数的类型应为无符号整数类型,例如 size_t。否则用户可以传递一个负数作为索引。

该函数会产生内存泄漏,因为它实际上并没有释放分配的节点。当指向头节点的指针等于 NULL 时,它可以调用未定义的行为。而且一般来说这个功能没有意义。

可以这样定义

bool Delete( Node * &head, size_t n )
{
    Node **current = &head;

    while ( *current && n-- )
    {
        current = &( *current )->next;
    }

    bool success = *current != nullptr;

    if ( success )
    {
        Node *tmp = *current;
        *current = ( *current )->next;
        delete tmp;
    }

    return success;
}

这是一个演示程序。

#include <iostream>

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

void insert( Node * &head, int x )
{
    head = new Node { x, head };
}

bool Delete( Node * &head, size_t n )
{
    Node **current = &head;

    while ( *current && n-- )
    {
        current = &( *current )->next;
    }

    bool success = *current != nullptr;

    if ( success )
    {
        Node *tmp = *current;
        *current = ( *current )->next;
        delete tmp;
    }

    return success;
}

std::ostream & print( Node * &head, std::ostream &os = std::cout )
{
    for ( Node *current = head; current != nullptr; current = current->next )
    {
        os << current->data << " -> ";
    }
    
    return os << "null";
}

int main() 
{
    Node *head = nullptr;
    
    for ( int i = 3; i != 0; i-- ) insert( head, i );
    
    print( head ) << '\n';
    
    size_t pos = 0;

    if ( Delete( head, pos ) )
    {
        print( head ) << '\n';
    }
    else
    {
        std::cout << "No data at the position " << pos << '\n';
    }
    
    pos = 4;
    
    if ( Delete( head, pos ) )
    {
        print( head ) << '\n';
    }
    else
    {
        std::cout << "No data at the position " << pos << '\n';
    }
    
    pos = 1;
    
    if ( Delete( head, pos ) )
    {
        print( head ) << '\n';
    }
    else
    {
        std::cout << "No data at the position " << pos << '\n';
    }
    
    pos = 0;
    
    if ( Delete( head, pos ) )
    {
        print( head ) << '\n';
    }
    else
    {
        std::cout << "No data at the position " << pos << '\n';
    }
    
    return 0;
}

它的输出是

1 -> 2 -> 3 -> null
2 -> 3 -> null
No data at the position 4
2 -> null
null

【讨论】:

  • 编译运行的时候有很多bug
  • @Lizzar_00 所示演示程序编译成功。没有错误。
  • @Lizzar_00 也许您正在使用不满足 C++ 11 标准的非常旧的编译器。例如尝试 www.ideone.com 上的代码。
  • @Lizzar_00 在 www.ideone.com 选择编译器 C++ 14。
  • 好的,感谢大学让我使用 DEV-C 进行测试和作业
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
相关资源
最近更新 更多