我试图通过指定要插入新节点的节点的位置,在给定节点之前插入一个节点。我得到了该节点位置内的数据,并使用while循环将这些数据与每个节点的数据进行比较,直到我到达应该插入节点的位置。
但是,当我尝试使用while语句显示元素时,我的程序进入了中篇循环。我检查了head节点所指向的位置,它的to仅指向single列表的第一个节点。有人能帮帮我吗?
#include <iostream>
#include<stdlib.h>
using namespace std;
void display(struct node *);
struct node{
int data;
struct node *next;
};
struct node *ptr,*head=NULL;
void insertt(struct node *head){ //insert function to insert node before a node
struct node *ptr1=head;
int pos,poss,value;
cin>>poss; //getting position and value
cin>>value;
pos=poss-1;
while(pos--){ //moving to that position
ptr1=ptr1->next;
}
struct node *ptr2=ptr1;
struct node *newnode = (struct node*)malloc(sizeof(struct node)); //creating new node for insertion
newnode->next=NULL;
newnode->data=value;
struct node *preptr;
preptr=ptr1;
int c=ptr2->data; //getting value present in that particular position(node) of the list
while(ptr1->data!=c){ //inserting before node
preptr=ptr1;
ptr1=ptr1->next;
}
preptr->next=newnode;
newnode->next=ptr1;
display(head);
}
void display(struct node *head){ //displaying linked list
struct node *ptr2=head;
while(ptr2!=NULL){
cout<<ptr2->data;
ptr2=ptr2->next;
}
}
int main()
{
int n,val,i;
cin>>n; //number of nodes
for(i=0;i<n;i++){ //node creation
cin>>val;
struct node *newnode = (struct node*)malloc(sizeof(struct node));
newnode->data=val;
newnode->next=NULL;
if(head==NULL){
ptr=head;
head=newnode;
}
else{
ptr=head;
while(ptr->next!=NULL){
ptr=ptr->next;
}
ptr->next=newnode;
}
}
insertt(head); //insertion
return 0;
}
- 找到
ptr1
后,将preptr
和ptr2
设置为ptr1
- 然后得到
c = ptr2->data
,它恰好与ptr1->data
相同 - 然后继续检查
while (ptr1->data != c)
,它总是错误的。因此底部while
循环不起任何作用 - 最后一行
preptr
和ptr1
指向同一个节点n
- 现在使
n->next
(preptr->next
(指向newnode
,使newnode->next
指向ptr1
(n
(:一个无限循环
n newnode
-------- ---------
preptr ---> | c | | value |
ptr1 ---> -------- ---------
| next | ---> | |
| | <--- | next |
-------- ---------
void insertt(struct node *head) {
struct node *ptr1 = head;
int pos, poss, value;
cin >> poss;
cin >> value;
pos = poss - 1;
while (pos--) {
ptr1 = ptr1->next;
}
struct node *ptr2 = ptr1;
struct node *newnode = (struct node *) malloc(sizeof(struct node));
newnode->next = NULL;
newnode->data = value;
struct node *preptr;
preptr = ptr1; // ptr1, ptr2, and preptr point to the same node n
int c = ptr2->data; // c = ptr1->data too
while (ptr1->data != c) { // always false
preptr = ptr1;
ptr1 = ptr1->next;
}
preptr->next = newnode; // n->next = newnode
newnode->next = ptr1; // newnode-next = n
display(head);
}
这里有一个可能的解决方案。老实说,你的代码有点乱,重写它更容易。我不确定你的代码出了什么问题,但你可以与下面的代码进行比较,然后试着弄清楚
注意:
您不需要将*ptr
和*head
作为全局变量,最好将它们保留在主变量中
使用while循环浏览列表以到达位置的方式有点不清楚,您可以使用for循环以更可读的方式(在我看来(进行导航
没有错误预防/处理,这让我很紧张,但如果这只是为了玩,你应该没事
// Example program
#include <iostream>
#include<stdlib.h>
using namespace std;
struct node{
int data;
struct node *next;
};
void insertt( node *head, int pos, int val){ //insert function to insert node before a node
struct node *ptr = head;
struct node *next;
for (int i = 0; i < pos-1; i++)
ptr=ptr->next;
next = ptr->next;
ptr->next = (struct node*)malloc(sizeof(struct node)); //creating new node for insertion
ptr->next->data = val;
ptr->next->next = next;
}
void rdisplay(struct node *head){ //displaying linked list
struct node *ptr=head;
if(ptr!=NULL){
if (ptr->next!=NULL)
rdisplay(ptr->next);
cout<<ptr->data <<std::endl;
}
}
void fdisplay(struct node *head){ //displaying linked list
struct node *ptr=head;
if(ptr!=NULL){
std::cout << ptr->data << std::endl;
if (ptr->next!=NULL)
fdisplay(ptr->next);
}
}
int main()
{
struct node *ptr = NULL,*head=NULL;
int n,val,i;
std::cout << "Incert number of values to have in the list:";
cin>>n; //number of nodes
for(i=0;i<n;i++){ //node creation
std::cout << "Incert value for node[" << i << "]:";
cin>>val;
struct node *newnode = (struct node*)malloc(sizeof(struct node));
newnode->data=val;
newnode->next=NULL;
if(head==NULL){
head=newnode;
}
else{
ptr=head;
while(ptr->next!=NULL){
ptr=ptr->next;
}
ptr->next=newnode;
}
}
std::cout <<std::endl;
std::cout <<std::endl;
fdisplay(head);
std::cout <<std::endl;
std::cout <<std::endl;
int pos;
std::cout << "At which position do you want to incert: ";
cin>>pos; //getting position and value
std::cout << "What value do you want to incert: ";
cin>>val;
insertt(head, pos, val); //insertion
std::cout <<std::endl;
std::cout <<std::endl;
fdisplay(head);
std::cout <<std::endl;
std::cout <<std::endl;
return 0;
}