提问者:小点点

删除第n个位置的单个链表


# 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

共2个答案

匿名用户

delete函数中,您有一个循环

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

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

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

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

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

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

匿名用户

对于初学者,单个链表节点的声明具有多余的数据成员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