0

I don't have any clue why the function insert_pos doesn't work. The error is:

member reference base type 'list' (aka 'struct le*') is not a structure or union.

#include <stdio.h>
#include "stdlib.h"

struct le{
  int value;
  struct le *next;
};
typedef struct le listenelement;
typedef listenelement *list;

int insert_pos(int v, int pos, list *l){
    listenelement * new;
    new = malloc(sizeof(listenelement));
    new->value = v;

    for(int i = 0; i < pos - 3; i++){
        *l = l->next;
    }
    new->next = l->next;
    l->next = new;
}

int delete elem(int e, list * l){

}
void sort(int m, list * l){

}

int main() {

    return 0;
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Kay R.
  • 19
  • 4
  • 2
    type of `l` is `struct le**`, not `struct le*` – BLUEPIXY May 28 '17 at 20:50
  • and why is it le** because i want like a next pointer which shows to the next node – Kay R. May 28 '17 at 20:51
  • 1
    @KayR. For a function to change a pointer, it needs a pointer to it. A pointer-to-pointer ! – wildplasser May 28 '17 at 20:59
  • 2
    This is why people recommend not typdef'ing pointers. It causes confusion. `list *l` expands to `struct le **l` – kaylum May 28 '17 at 21:02
  • fix like [this](http://ideone.com/gEzVFH) – BLUEPIXY May 28 '17 at 21:14
  • the problem is that the first code was set and i need to do the 3 last functions – Kay R. May 28 '17 at 21:16
  • Well: do them then! – wildplasser May 28 '17 at 21:18
  • thank you very much you are a hero and do you have an idea for delete_elem and maybe sort :S you would help me so much and i would also pay 10 euros for that – Kay R. May 28 '17 at 21:19
  • i dont have a clue how @wildplasser – Kay R. May 28 '17 at 21:21
  • @KayR.Welcome to Stack Overflow, you should be more careful with code indentation and also try to explain your goal a little bit better. Also, we are not here to solve your homework. you should try a bit harder not just dump your exercises here and wait people to solve it. – José Fonte May 28 '17 at 21:40
  • @JoséFonte yea thats true im sry that was a mistake i had time pressure i had only 1 hr left you know and it was totaly my bad and my fault i dont want that people solve my problems... :( – Kay R. May 28 '17 at 21:50
  • @KayR. We like to help but you must show more effort and 1h before deadline is begging for miracles :) If you are willing to learn maybe people will try to give you some guidelines or try to decompose your problem in smaller parts. Good Luck and Welcome! – José Fonte May 28 '17 at 21:54
  • @JoséFonte ty :D you mean like divide and conquer?:) – Kay R. May 28 '17 at 21:58
  • @JoséFonte in the lecture hall :) – Kay R. May 28 '17 at 22:01
  • Your code has many mistakes, I've edited it but is in review. You will need a pointer to the head, and keep it with you so that you have a reference to the starting node. The end you can always get because the last element .next pointer will point to NULL. Then, inside the functions always copy the head pointer to another pointer that will iterate the list. After you get this, linked lists will be easy. – José Fonte May 28 '17 at 22:05
  • @JoséFonte but isnt it like if you define an array you automatically have a pointer to the first element so to the head yes i have struct le *next; for the next element – Kay R. May 28 '17 at 22:12
  • @JoséFonte how do you mean it wen you say copy the head pointer to another pointer? – Kay R. May 28 '17 at 22:13
  • @KayR. You must hold a copy of the head so that you know where the beginning of your list is, Arrays are another thing. check [here](http://www.learn-c.org/en/Linked_lists) – José Fonte May 28 '17 at 22:14
  • ok i understand ty very much for your help and next time i try first and explain more and ask exactly what i want to know. have a nice evening@JoséFonte – Kay R. May 28 '17 at 22:24
  • 1
    Note [Is it a good idea to typedef pointers?](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) — the short answer is "No". – Jonathan Leffler May 28 '17 at 23:03
  • @JonathanLeffler - `Is it a good idea to typedef pointers?` - Windows API does this, but uses Hungarian notation prefixes on the typedefs to make it clear they are pointers in most cases, such as P.... for pointer to, such as PCHAR, PINT, PLONG, ... . – rcgldr May 28 '17 at 23:41
  • @rcgldr Better not look for WinAPI as coding style guideline: *Systems Hungarian* notation has been heavily criticised. – user694733 May 29 '17 at 12:15

2 Answers2

0

Why does insert_pos use pos-3 ?

To fix the syntax error, the line should be:

        l = &((*l)->next);

insert_pos should update the passed list pointer if and only if the new node is inserted to the front of the list.

As an alternative, insert_pos could return a pointer to the list:

listenelement * insert_pos(int v, int pos, listenelement *l)
rcgldr
  • 27,407
  • 3
  • 36
  • 61
0

I will hope if you can use it. It create some elements of linked list and insert a new node after the position

#include <stdio.h>
#include <stdlib.h>

typedef struct le{
    int value;
    struct le *next;
}listenelement;

// this can create a new element of the linked list

listenelement *insert(int v,int pos, listenelement *first)
{
    int i;
    listenelement *move, *new;
    new = (listenelement*)malloc(sizeof(listenelement));
    // adatok bszuras
    new->value = v;
    //data insert end
    new->next = NULL;
    if (first == NULL)//emtpy list ? 
        return new;
    for (move = first; move->next != NULL; move = move->next);
    // emtpy cycle can find the last element of linked list
    move->next = new;
    return first;
}


listenelement* insert_pos(int v, int pos, listenelement *l){
    listenelement * new;

        new = (listenelement*)malloc(sizeof(listenelement));
        new->value = v;

        for (int i = 0; i < pos; i++){
            l = l->next;
        }
        new->next = l->next;
        l->next = new;

}

int delete_elem(int e, listenelement * l){

}
void sort(int m, listenelement * l){

}

int main() {
    int i;
    listenelement *first = NULL;
    listenelement *act;

    act = insert(1, 0, first);
    insert(5, 0, act);
    insert(6, 0, act);
    insert(7, 0, act);
    //insert after second element
    insert_pos(11, 2, act);
    insert(7, 0, act);
    insert(7, 0, act);
// print the list elements
    while (act != NULL){
        printf(" content: %d\n", act->value);
        act = act->next;
    }

    return 0;
}
gr3g0ry
  • 11
  • 1
  • insert_pos isn't returning a pointer, and isn't handling the case where the new element becomes the first element of the list. – rcgldr May 29 '17 at 13:51
  • Yes,theese are true. But in this example code should work. – gr3g0ry May 29 '17 at 14:44
  • insert_pos should insert before the position, in which case, insert_pos(value, 0, list), should put the new element at the front of the list and return a pointer to the new element (which in turn would point to the rest of the list). – rcgldr May 29 '17 at 19:30
  • ty very much my homework is over but I will use it to improve my skills thank you! – Kay R. May 30 '17 at 17:56