1

why is the address of temp (in the while loop in main) the same everytime the loop runs i am trying to insert into a linked list and then display and then output the middle element but initially in displaying it ran an infinite loop only displayint the first element. On printing the address after inserting and llist.add_ele_to_beg(&temp); its priting the same address each time ! why is this happening ?

#include<iostream>
#include <unistd.h>

using namespace std;

class LinkedList;
class Node
{
    private:
    Node* next;
    int value;
    friend class LinkedList;
    public:
    Node(int ele) // constructor - declared in private section 
    // to prevent other classes creating objects of this class, 
    // only this class can create the object
    {
        next = nullptr;
        value = ele;
    }
};

class LinkedList
{
    private:
    Node* head;
    public:
    LinkedList()
    {
        head = nullptr;
    }
    void add_ele_to_beg(Node *temp)
    {
        // Node *temp = new Node(); // dynamically alloctg Node object
        // temp->value = x;
        temp->next = this->head;
        this->head = temp;
    }
    void display()
    {
        Node *h = this->head;
        while(h)
        {
            cout << h << endl;
            cout << h->value << endl;
            h = h->next; 
            cout << h << endl;
            cout << h->value << endl;
            exit(0);
        }
    }
    int findMiddle()
    {
        Node *fast, *slow = this->head;
        if(!slow)
        {
            return -1;
        }
        if(!slow->next)
        {
            return slow->value;
        }
        if(!slow->next->next)
        {
            return slow->value;
        }
        // n > 2
        fast = head->next->next;

        while(1)
        {
            slow = slow->next;
            if(!fast->next)
            {
                if(!fast->next->next)
                {
                    fast = fast->next->next;
                }
                else
                {
                    break;
                }   
            }
            else
            {
                break;
            }  
        }
        return slow->value;
    }
};

int main()
{
    LinkedList llist;
    int n;
    cout << "enter n" << endl;
    cin >> n;
    // create a dummy node
    cout << "enter elements to be inserted in the beg" << endl;
    int ele;
    while(n--)
    {
        cin >> ele;
        Node temp(ele); // obj node created and ctor initialises
        llist.add_ele_to_beg(&temp); // sending address of node to make change to 
        cout << &temp << endl;
        // node (passing by reference)
    }

    llist.display();

    cout << llist.findMiddle();
    cout << endl;
    return 0;
}
dagwood
  • 181
  • 1
  • 12

3 Answers3

5

why is the address of temp (in the while loop in main) the same everytime the loop runs

Because object you get address of has automatic storage duration. It means that object lifetime ends at the end of block it was created (end of loop in your case) and you have dangling pointer after that. As that memory considered free after lifetime of object ends compiler reuses the same memory again for practical purpose (it does not have to, but it can and it makes sense).

To make it work properly you should create object with dynamic storage duration, which means you control lifetime of the object. You can use operator new for that, but it is better to use smart pointer instead of raw one and let it manage object lifetime. In such case you should use std::make_unique or std::make_shared depends on what kind of ownership you want. You can find details on how to do it here C++ Linked list using smart pointers

Slava
  • 40,641
  • 1
  • 38
  • 81
2

On printing the address after inserting and llist.add_ele_to_beg(&temp); its priting the same address each time ! why is this happening ?

It happens because temp is a local variable, so it lives on the stack, and the same set of local variables gets created and destroyed in the same place each time through the loop:

while(n--)
{
    cin >> ele;
    Node temp(ele); // obj node created and ctor initialises
    llist.add_ele_to_beg(&temp); // sending address of node to make change to 
    cout << &temp << endl;
    // node (passing by reference)
}

So temp gets created on top of the stack, and then you do some things to it, and then it goes out of scope (so gets destroyed), and then the stack is in the same state that it was in before that iteration of the loop. And then the process repeats.

It looks like what you probably mean to be doing is allocating a new node using new, so that the object is created on the heap. You can then call list.add_ele_to_beg() to add it to the list, and the object will live beyond the end of the loop's body.

Caleb
  • 120,112
  • 19
  • 171
  • 259
0

You can create a new element using new Node() in each loop cycle.

KDD2020
  • 49
  • 6