使用C在双链表的第n个位置插入一个节点 void InsertAtN() { int get,我; 结构节点*temp=(结构节点*)malloc(节点大小)),*temp1,*temp2; printf(“\n输入位置:”); scanf(“%d”,获取(&get)); printf(“\n输入数据:”); scanf(“%d”,&temp->data); if(get==1 | | head==NULL) { if(head==NULL&&get==1) { temp->prev=temp->next=NULL; 压头=温度; 返回; } 压头->上一个=温度; temp->prev=NULL; 温度->下一步=头部; 压头=温度; 返回; } temp1=头部; 对于(i=0;不精确; } temp->next=temp1->next; temp->prev=temp1; temp1->next=temp; temp2=temp->next; temp2->prev=温度; }
当我在中间或开始插入节点时,它正在工作。但当我尝试在末尾插入节点时,这将崩溃。请帮助我解决问题。使用C在双链表的第n个位置插入一个节点 void InsertAtN() { int get,我; 结构节点*temp=(结构节点*)malloc(节点大小)),*temp1,*temp2; printf(“\n输入位置:”); scanf(“%d”,获取(&get)); printf(“\n输入数据:”); scanf(“%d”,&temp->data); if(get==1 | | head==NULL) { if(head==NULL&&get==1) { temp->prev=temp->next=NULL; 压头=温度; 返回; } 压头->上一个=温度; temp->prev=NULL; 温度->下一步=头部; 压头=温度; 返回; } temp1=头部; 对于(i=0;不精确; } temp->next=temp1->next; temp->prev=temp1; temp1->next=temp; temp2=temp->next; temp2->prev=温度; },c,data-structures,linked-list,doubly-linked-list,C,Data Structures,Linked List,Doubly Linked List,当我在中间或开始插入节点时,它正在工作。但当我尝试在末尾插入节点时,这将崩溃。请帮助我解决问题。temp2->prev=temp,而不是最后一行的temp2->prev=temp void InsertAtN() { int get,i; struct node* temp=(struct node*) malloc(sizeof(node)),*temp1,*temp2; printf("\nEnter the Position : "); scanf("
temp2->prev=temp
,而不是最后一行的temp2->prev=temp
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
为NULL
。假设只有一个节点。
您的for循环
将不会运行,因此temp1
指向第一个节点。
那你呢
if (temp2)
temp2->prev=temp
当只有一个节点时,您必须处理这种情况,并尝试在末尾插入。最后我调试了我的代码。。这是最后的代码…感谢您的帮助。)
void InsertAtN()
{
int get,我;
结构节点*temp=(结构节点*)malloc(节点大小)),*temp1,*temp2;
printf(“\n输入位置:”);
scanf(“%d”,获取(&get));
printf(“\n输入数据:”);
scanf(“%d”,&temp->data);
if(get==1 | | head==NULL)
{
if(head==NULL&&get==1)
{
temp->prev=temp->next=NULL;
压头=温度;
返回;
}
压头->上一个=温度;
temp->prev=NULL;
温度->下一步=头部;
压头=温度;
返回;
}
temp1=头部;
对于(i=0;不精确;
}
temp->next=temp1->next;
temp->prev=temp1;
temp1->next=temp;
如果(临时->下一步!=NULL)
{
temp2=temp->next;
temp2->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;
}
}
//开头和结尾的插入是分开写的:??
#包括
使用名称空间std;
类节点{
公众:
int数据;
节点*prev;
节点*下一步;
//建造师
节点(int d){
数据=d;
prev=NULL;
next=NULL;
}
};
int main(){
//创建指向第一个节点的头点;
Node*head=NULL;
coutprev=frontNode;
头部=前部节点;
节点*ptr=头部;
while(ptr!=NULL){
coutnext->prev=中间节点;
ptr->next=middleNode;
middleNode->prev=ptr;
ptr=头部;
while(ptr!=NULL){
库托克,我可以帮你。在你的调试器下运行它,在每一步检查变量值时逐步检查失败的情况。temp2=temp->next;temp2->prev=temp;
:temp->next
可能为空。
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;
}