我試圖通過指定要在其之前插入新節點的節點的位置來在給定節點之前插入一個節點。我得到了該節點位置內的資料并使用while回圈,將此資料與每個節點的資料進行比較,直到我到達我應該插入節點的位置。
但是當我嘗試使用 while 陳述句顯示元素時,我的程式進入了一個無限回圈。我檢查了頭節點指向的位置以及它僅指向單個串列的第一個節點。有人可以幫我嗎?
#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;
}
uj5u.com熱心網友回復:
- 一旦你找到
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);
}
uj5u.com熱心網友回復:
這是一個可能的解決方案。老實說,你的代碼有點亂,重寫它更容易。我不確定您的問題是什么,但您可以與下面的代碼進行比較并嘗試找出答案。
幾點注意事項:
您不需要擁有*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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/528278.html
標籤:C 功能节点单链表
