How do I go through a list chained from the last position?

0

I have a question about the list chained in C. The exercise is a function that receives as a parameter the initial node of the linked list and the position x (counting backwards). With this, I have to go through and find the position, return the value of the corresponding node.

I've done this so far:

struct Node
{
    int data;
    struct Node *next;
}
/*-------------------------------------------------*/

int GetNode(Node *head,int positionFromTail)
{
    struct Node* fimNodo;
    while(head->next!=NULL){
        head->next=next;
        if(head->next==NULL){

            fimNodo->data=head->data;
            fimNodo->next=head->next;
        }

    }
    while(positionFromTail!=0){
        fimNodo->next=*next;
        positionFromTail--;
        if(positionFromTail==0){
            return fimNodo->data;
        }
    }
}

You are giving this feature an error. can anybody help me?

Note: Exercise is just this function and does not require the main function ...

The errors that are occurring are:

cannot resolve overloaded function ‘next’ based on conversion to type ‘Node*’


cannot convert ‘Node’ to ‘Node*’ in assignment
    
asked by anonymous 18.04.2018 / 15:38

2 answers

1

The comment error is basically that you declared struct Node fimNodo; as a static variable, not a pointer.

Use:

struct Node* fimNodo;

In order to use fimNodo->next out fimNodo->data .

    
18.04.2018 / 16:17
0

I got it!

The code looks like this:

int GetNode(struct Node *head, int positionFromTail)
{
    int x = 0, y = 2;
    struct Node *fimNodo = head;

    if (fimNodo == NULL) {
        return -1;
    }

    while (fimNodo != NULL) {
        fimNodo = fimNodo->next;
        x++;
    }

    x = x - positionFromTail;
    fimNodo = head;

    while (y <= x) {
        fimNodo = fimNodo->next;
        y++;
    }   

    return fimNodo->data;
}

 Briefly added 2 counters (x and y). One to cycle through the chained list to see the total number of positions (x) and another to find the required position. I made the comparison to see if the list was empty, etc.

    
20.04.2018 / 04:53