0

I am trying to insert numbers which are a result of deduction of two neighbors in already made up list.

        #include<iostream>

        using namespace std;

        struct Element{
            int x;
            Element* next;
        };

        Element* createList(){
            int i,n;
            Element *head=NULL,*p=NULL;
            cout<<"How many elements: ";
            cin>>n;
            for(i=0;i<n;i++){
                if(i==0) {
                    head=new Element();
                    p=head;
                }
                else{
                    p->next=new Element();
                    p=p->next;
                }
                cout<<"Value: ";
                cin>>p->x;
            }
            p->next=NULL;
            return head;
        }

        void printList(Element* head){
            Element* p=head;
            cout<<"List values: "<<endl;
            while(p!=NULL){
                cout<<p->x<<"   ";
                p=p->next;
            }
            cout<<endl;
        }

        Element* createElement(int x){
            Element* element=new Element();
            element->x=x;
            element->next=NULL;
            return element;
        }


        Element* insert(Element* head){
            Element *p=head,*temp=NULL;
            while(p->next!=NULL){
                temp=createElement(p->next->x - p->x);
                temp->next=p->next;
                p->next=temp;
                p=p->next;
            }
            return head;
        }


        int main(){
            Element* head=NULL;
            head=createList();
            printList(head);
            head=insert(head);
            printList(head);
            return 0;
        }

I expected that my updated list will contain these numbers which should be placed between every two numbers in the original list but when my program encounters the insert function it is just running and never finishes. Example: Original list: 1 5 8 12 30 Updated list: 1 4 5 3 8 4 12 18 30

7
  • 1
    Please provide a minimal reproducible example. Commented Aug 22, 2019 at 11:33
  • head=insert(head); doesn't make sense at all. What is createList()? Commented Aug 22, 2019 at 11:33
  • I dont think you have posted the complete code, for instance definition for createList is missing. Please add complete code to get help and also I suggest you to do some debugging before posting. Commented Aug 22, 2019 at 11:34
  • 1
    you know that there is std::list ? If this is an assignment then imho you should mention it in the question, becuase typically they come with all sorts of strange requirements Commented Aug 22, 2019 at 11:42
  • 1
    OT.: All the member assignments you do in createElement() should actually be part of the initialization in Element::Element(). Commented Aug 22, 2019 at 11:45

1 Answer 1

2

Draw it (pencil and paper is better than ASCII but is hard to post here):

After temp->next = p->next:

      head
       |
       v
     +---+     +---+
p -->|  ------>|  -----> ...
     +---+     +---+
                 ^
        +---+    |
temp -->|  ------+
        +---+     

p->next=temp;

      head
       |
       v
     +---+     +---+
p -->|  ---+   |  -----> ...
     +---+ |   +---+
           v     ^
        +---+    |
temp -->|  ------+
        +---+     

p=p->next;

      head
       |
       v
     +---+     +---+
     |  ---+   |  -----> ...
     +---+ |   +---+
           v     ^
        +---+    |
temp -->|  ------+
        +---+     
          ^
          |
          p

... and repeat until you see why p->next!=NULL will never become false.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.