1

I have structure in C which looks like:

typedef struct node {
  int id;
  int salary;
  struct node *next;
  char name[30];
  char phone[10];
  char position[30];
} List;

I also have two variables List type and I want to sort the structures by id (the largest id will be last). My idea how to resolve this:

  1. Do **pointer point on *head (*head = pointer on first member of List).
  2. Check if **pointer.id larger then **pointer.next.id If yes -> **pointer.next will point on *pointer.next.next **pointer.next.next will point on &pointer

Here is code of solving this problem (I will do "cosmetics" of code and bubble sort algorithm after. Firstable just want to build code which will be work):

void sort(List head) {
  int length = getListLength(head);
  List **currentNode = &head;

  for(int i = 0; i < length; i++) {
    **currentNode = &head;
    for(int j = 0; j < length; j++) {
      if(currentNode->id > currentNode->next->id) {
        currentNode->next = currentNode->next->next;
        currentNode->next->next = &currentNode;
      }
    }
  }
}

There is errors:

         ^             ~~~~~
list.c:92:19: error: assigning to 'List' (aka 'struct node') from incompatible type 'List *' (aka 'struct node *'); remove &
    **currentNode = &head;
                  ^ ~~~~~
list.c:94:21: error: member reference base type 'List *' (aka 'struct node *') is not a structure or union
      if(currentNode->id > currentNode->next->id) {
         ~~~~~~~~~~~^ ~~
list.c:94:39: error: member reference base type 'List *' (aka 'struct node *') is not a structure or union
      if(currentNode->id > currentNode->next->id) {
                           ~~~~~~~~~~~^ ~~~~
list.c:95:20: error: member reference base type 'List *' (aka 'struct node *') is not a structure or union
        currentNode->next = currentNode->next->next;
        ~~~~~~~~~~~^ ~~~~
list.c:95:40: error: member reference base type 'List *' (aka 'struct node *') is not a structure or union
        currentNode->next = currentNode->next->next;
                            ~~~~~~~~~~~^ ~~~~
list.c:96:20: error: member reference base type 'List *' (aka 'struct node *') is not a structure or union
        currentNode->next->next = &currentNode;
        ~~~~~~~~~~~^ ~~~~

Please give me some way to fix this (without solving the problem. Just want to know what is wrong in my code and then fix it by myself).

Main function:

int main() {
  List *head = (List *) malloc(sizeof(List));
  if(head == NULL) {
    return 1;
  }

  head -> id = 332513075;
  head -> salary = 1000;
  strcpy(head -> name, "Name Lastname");
  strcpy(head -> phone, "0587885238");
  strcpy(head -> position, "cleaner");

  head -> next = (List *) malloc(sizeof(List));

  head -> next->id = 2;
  head -> next->salary = 2000;
  strcpy(head -> next -> name, "Another name");
  strcpy(head -> next -> phone, "1234567890");
  strcpy(head -> next -> position, "CEO");
  head -> next -> next = NULL;

  sort(*head);
  print_list(*head);

  return 0;
}
cigien
  • 57,834
  • 11
  • 73
  • 112
  • Why double pointer ? **currentNode = &head – rootkonda Aug 03 '20 at 17:41
  • The immediate fix is `List *currentNode = &head;` but I can see lots of reasons why this code is not going to work. – john Aug 03 '20 at 17:42
  • @rootkonda Because head is pointer on first structure. – Jiolet Vuice Aug 03 '20 at 17:42
  • @BalmainVuitton `head` in not a pointer in the code you've posted. – john Aug 03 '20 at 17:43
  • @BalmainVuitton Now you've posted more code I'll clarify that. `head` is a pointer in the main function, but it is not a pointer in the `sort` function. – john Aug 03 '20 at 17:45
  • @john edited the post with main function which contains head as pointer. – Jiolet Vuice Aug 03 '20 at 17:45
  • 1
    @BalmainVuitton I can see that, why don't you pass head as a pointer to the `sort` and `printList` functions? That's the normal thing to do. – john Aug 03 '20 at 17:46
  • IMHO, a better sort method is to move the elements of the list into a new list, using an *insert sort*, only changing the links (no need to copy the nodes). – Thomas Matthews Aug 03 '20 at 17:54
  • @rootkonda believe it or not using a pointer-to-pointer to a node can make the book-keeping when altering a singly linked list far, far easier to handle. Asker might be using it wrong here, but it is a good strategy. Here's an example of this trick in action: https://stackoverflow.com/a/22122095/4581301 See how much simpler the pointer-to-pointer version in the community addition is? – user4581301 Aug 03 '20 at 18:00
  • @user4581301. I agree. Any feature or concept when used in appropriate situation yields efficient and simpler answer. My comments are specific in this context. When I saw it first time in the given example, it just hit me that something might be wrong here. Hence I asked my question. – rootkonda Aug 03 '20 at 18:04
  • Meanwhile I just did bubble sort which swaps values but not pointers. After all semester projects I will seat and try understand better all about pointers, linked lists etc and I'll try to realise the sort function. Thx for all – Jiolet Vuice Aug 04 '20 at 18:44

2 Answers2

1
  • The first error: head is a List, so &head would be a List*; but you are trying to assign it to a List**.

  • The others stem from treating currentNode as if it were a List*.

So declaring currentNode as a List* would solve the listed errors.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

I think you need following modifications:

  1. pass address of head as I suppose sort function needs to update head after sorting and pass head to print_list: sort(&head); print_list(head);

  2. sort algorithm seems incorrect. I am not sharing modifications in algorithm but sharing modifications to remove error and updating head pointer.:

    void sort(List **head) { int length = getListLength(*head); List *currentNode = *head; // kindly implement Algo here using currentNode as pointer to list *head = ... ; // update head pointer }

inderjeet
  • 69
  • 2