1

I designed this code so that I can get a pointer of any position that user wants in a circular singly linked list, I am using cout to return pointer, I want such a mechanism that I can use it with my other function instead of re writing the whole code again, for that I need to do something with return type which is void right now

Here is the function ..

void pointer_to_node(int index){
    Node*temptr;
    temptr = new Node;
    temptr = firstptr;

    Node*temptr2;
    temptr2 = new Node;
    temptr2 = NULL;
    int count = 1;

    while (temptr!=temptr2){
        if(count==index){
            cout << "Required Pointer is : ";
            cout<< temptr;}

        count++;
        temptr2=firstptr;
        temptr=temptr->nextptr;
    }

    if (index>size_of_list())
    {
        temptr=NULL;
        cout<< "Can't You think in bounds. Take your NULL Pointer ";
        cout << temptr;
        delete temptr;
        delete temptr2;
    }
}
4
  • make it return the Node * that you end up on Dec 14, 2012 at 16:35
  • 1
    also granted I haven't read this at all, but what is the point of bounds checking in a circular LL? Dec 14, 2012 at 16:36
  • @AK4749 index %= size_of_list() :) Dec 14, 2012 at 16:42
  • @AK4749 actually i have given an option of adding the nodes to user, if he has added just 3 nodes and requesting a pointer of 4th node , than i am giving a NULL POINTER, okay now i changed it to Node* pointer_to_node(int index) but its still giving errors and not returning any thing \warning: control reaches end of non-void function| \
    – Asad Irfan
    Dec 14, 2012 at 16:43

1 Answer 1

1

You just need to return a Node *.

However, while you're doing that, you also really need to take out these: temptr = new Node; lines, as well as the deletes, as you're leaking memory there. You just immediately discard those new nodes by reassigning the pointers. The deletes at the end will delete the wrong nodes entirely, and aren't called in all cases anyway.

And if you pass an index of 0, your loop might take a very long time indeed.

I assume you have good reason for wanting to return NULL if you loop around the list.

Something like the following should suffice:

Node *pointer_to_node(int index)
{
    Node *temp = firstptr;
    while(index-- != 0) {
        temp = temp->nextPtr;
        if(temp == firstptr) return NULL;
    }
    return temp;
}
3
  • its stilll not returning ??? i changed the return type to Node pointer_to_node(int index) and the error i receive is |82|error: conversion from Node*' to non-scalar type Node' requested|
    – Asad Irfan
    Dec 14, 2012 at 16:53
  • The return type should be Node*, not Node.
    – JasonD
    Dec 14, 2012 at 16:54
  • i first changed it to NODE* and forgot to write cout .. done .. THanks... how should i vote your answer
    – Asad Irfan
    Dec 14, 2012 at 16:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.