infinite loop while trying to insert node before a node in singly linked list

I tried to insert a node before a given node by specifying the position of the node before which I want to insert the newnode. I got the data present inside that node’s position and using a while loop, compared this data with each node’s data till I reached the point where I was supposed to insert the node.

But when I tried displaying the elements using a while statement my program went into an infinte loop.I checked where the head node was pointing to and its to pointing to the first node of the singly list only.
Could someone please help me out?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
#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;
}
</code>
<code> #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; } </code>

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

1

  • Once you find ptr1, you set preptr and ptr2 to ptr1.
  • Then you get c = ptr2->data, which happens to be the same as ptr1->data.
  • And you go on and check while (ptr1->data != c), which is always false. So that bottom while loop does nothing
  • And you get to the last lines with preptr and ptr1 pointing to the same node n.
  • Now you make n->next (preptr->next) point to newnode, and newnode->next point to ptr1 (n): an infinite loop.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> n newnode
-------- ---------
preptr ---> | c | | value |
ptr1 ---> -------- ---------
| next | ---> | |
| | <--- | next |
-------- ---------
</code>
<code> n newnode -------- --------- preptr ---> | c | | value | ptr1 ---> -------- --------- | next | ---> | | | | <--- | next | -------- --------- </code>
               n           newnode
            --------      ---------
preptr ---> |  c   |      | value |
ptr1   ---> --------      ---------
            | next | ---> |       |
            |      | <--- |  next |
            --------      ---------
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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);
}
</code>
<code>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); } </code>
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);
}

Here is a possible solution. to be honest your code was a bit messy and it was easier to just rewrite it. I am not sure what is the problem with your but you could compare with the code below and try to figure it out.
A couple of notes:
You do not need to have *ptr and *head as global variables it is better to keep them in the main.
The way you navigate over the list with the while loop to get to location is a bit unclear and you can do it with a for loop in a more readable way (in my opinion).
There is no error prevention/handling which makes me very nervous but if this is just for toying with, you should be fine

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// 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;
}
</code>
<code>// 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; } </code>
// 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;
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật