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

时间:2022-09-05 19:52:38
 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;

}

Here when I insert a node at middle or starting, it is working. But when I try to insert a node at end, this will crash..please help me out.

这里当我在中间插入一个节点或开始时,它正在工作。但是当我尝试在结尾插入一个节点时,这会崩溃..请帮助我。

4 个解决方案

#1


2  

Instead of temp2->prev=temp at the last line, write

写,而不是temp2-> prev = temp在最后一行

if (temp2)
    temp2->prev=temp

Because temp2 is NULL when you insert at last position.

因为在最后位置插入时temp2为NULL。

#2


0  

Suppose there is only one node. Your for loop won't run so temp1 is pointing to first node. Then you do

假设只有一个节点。你的for循环不会运行,所以temp1指向第一个节点。然后你做

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

You have to handle case when there is only one node and you try to insert at the end.

当只有一个节点并且您尝试在最后插入时,您必须处理大小写。

#3


0  

Finally I debugged my code..This is the Final code...Thanks for helping dudes :)

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

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;
    }
}

#4


0  

Here is the simple code for that

这是简单的代码

//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;
}

#1


2  

Instead of temp2->prev=temp at the last line, write

写,而不是temp2-> prev = temp在最后一行

if (temp2)
    temp2->prev=temp

Because temp2 is NULL when you insert at last position.

因为在最后位置插入时temp2为NULL。

#2


0  

Suppose there is only one node. Your for loop won't run so temp1 is pointing to first node. Then you do

假设只有一个节点。你的for循环不会运行,所以temp1指向第一个节点。然后你做

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

You have to handle case when there is only one node and you try to insert at the end.

当只有一个节点并且您尝试在最后插入时,您必须处理大小写。

#3


0  

Finally I debugged my code..This is the Final code...Thanks for helping dudes :)

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

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;
    }
}

#4


0  

Here is the simple code for that

这是简单的代码

//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;
}