使用C在双向链表的第N个位置插入一个节点

Insert a Node at Nth position in Doubly linked list using C

 void InsertAtN() 
 {
    int get,i;
    struct node* temp=(struct node*) malloc(sizeof(node)),*temp1,*temp2;
    printf("\nEnter the Position : ");
    scanf("%d",&get);
    printf("\nEnter the Data : ");
    scanf("%d",&temp->data);
    if(get==1 || head==NULL)
    {
        if(head==NULL && get==1)
        {
            temp->prev=temp->next=NULL;
            head=temp;
            return;
        }
        head->prev=temp;
        temp->prev=NULL;
        temp->next=head;
        head=temp;
        return;
    }
    temp1=head;
    for(i=0;i<get-2;i++)
    {
        temp1=temp1->next;

    }
    temp->next=temp1->next;
    temp->prev=temp1;
    temp1->next=temp;
    temp2=temp->next;
    temp2->prev=temp;

}

这里当我在中间或开始插入一个节点时,它起作用了。但是当我尝试在最后插入一个节点时,这会崩溃..请帮助我。

在最后一行写

而不是 temp2->prev=temp
if (temp2)
    temp2->prev=temp

因为temp2在最后插入时是NULL

假设只有一个节点。 你的 for loop 不会 运行 所以 temp1 指向第一个节点。 那你做

temp2=temp->next; //temp2 is NULL
temp2->prev=temp; //NULL->prev

你必须处理只有一个节点的情况,你尝试在最后插入。

最后我调试了我的代码..这是最终代码...感谢您的帮助 :)

void InsertAtN()
{
    int get,i;
    struct node* temp=(struct node*) malloc(sizeof(node)),*temp1,*temp2;
    printf("\nEnter the Position : ");
    scanf("%d",&get);
    printf("\nEnter the Data : ");
    scanf("%d",&temp->data);
    if(get==1 || head==NULL)
    {
        if(head==NULL && get==1)
        {
            temp->prev=temp->next=NULL;
            head=temp;
            return;
        }
        head->prev=temp;
        temp->prev=NULL;
        temp->next=head;
        head=temp;
        return;
    }
    temp1=head;
    for(i=0;i<get-2;i++)
    {
        temp1=temp1->next;

    }
    temp->next=temp1->next;
    temp->prev=temp1;
    temp1->next=temp;
    if(temp->next!=NULL)
    {
    temp2=temp->next;
    temp2->prev=temp;
    }
}

这是简单的代码

//Insertion at beginning and end are written separately :??

#include<iostream>
using namespace std;
class Node{
public:
    int data;
    Node* prev;
    Node* next;

    //constructor

    Node(int d){
        data=d;
        prev=NULL;
        next=NULL;
    }
};
int main(){

    //create head point to first node;

    Node* head=NULL;
    cout<<"Enter node to add at front"<<endl;

    Node* firstNode=new Node(2);
    head=firstNode;

    int d;
    cin>>d;

    Node* frontNode=new Node(d);

    //algorithm to add at front
    frontNode->next=head;
    head->prev=frontNode;
    head=frontNode;

    Node* ptr=head;
    while(ptr!=NULL){
        cout<<ptr->data<<"=>";
        ptr=ptr->next;
    }

    ptr=head;
    cout<<endl;

    cout<<"Enter data to add at last"<<endl;

    int d1;
    cin>>d1;
    Node* lastNode=new Node(d1);

    while(ptr->next!=NULL){
        ptr=ptr->next;
    }

    //now Algo for insertion at last
    ptr->next=lastNode;
    lastNode->prev=ptr;

    ptr=head;

    while(ptr!=NULL){
        cout<<ptr->data<<"=>";
        ptr=ptr->next;
    }
    ptr=head;

    cout<<endl<<"Enter position and data to insert"<<endl;

    int p,d2;
    cin>>p>>d2;
p=p-2;
    while(p!=0){
        ptr=ptr->next;
        p--;
    }

    Node* middleNode=new Node(d2);
    middleNode->next=ptr->next;
    ptr->next->prev=middleNode;

    ptr->next=middleNode;
    middleNode->prev=ptr;

    ptr=head;

    while(ptr!=NULL){
        cout<<ptr->data<<"=>";
        ptr=ptr->next;
    }

    return 0;
}