Jiolet Vuice
Jiolet Vuice

Reputation: 55

How to sort structure

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;
}

Upvotes: 1

Views: 95

Answers (2)

inderjeet
inderjeet

Reputation: 79

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 }

Upvotes: 0

Scott Hunter
Scott Hunter

Reputation: 49813

  • 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.

Upvotes: 1

Related Questions