brostone51
brostone51

Reputation: 125

Linked list structures initialized in functions not being assigned properly

I'm sure this has been asked before, but I cannot find anything on this website or any other about it after searching for quite awhile.

I''m having trouble getting the values of structures that I'm creating and modifying in a function. The code looks something like:

struct node {
  char name[35];
  int employeeID;
  struct node *next;
}

typedef struct node employee;

void insertInOrder(employee *head, employee *curr) {
  if (head == NULL) {
    *curr->next = *head;
    *head = *curr;
  } else {
    if ((head->employeeID<curr->employeeID)&&(curr->employeeID <head->next->employeeID)) {
      *curr->next = *head->next;
      *head->next = *curr;
    } else {
      insertInOrder(head->next, curr);
    }
  }
}

void addEmployee(char name[], employee *head, employee *curr) {
  int id;
  scanf("%d", &id);
  curr = malloc(sizeof(employee));
  strcpy(curr->name, name);
  curr->employeeID = id;
  insertInOrder(head, curr);
}

int main(void) {
  char name[35];
  int quit = 1;
  employee *head, *curr;
  head = NULL;
  printf("Enter data about the books: \n");
  while (quit) {
    scanf("%[^\n]%*c", title);
    if (title[0] != '#') {
        addBook(name, head, curr);
    } else {
        quit = 0;
    }
}

During my debugging, my code iterates into all of my functions, but once I get back to main after adding all of the data I want, all of the variables are empty. I know it has something to do with the way I'm using or passing pointers, but when I look at the code I keep coming to the logical conclusion that what I have should do what I want. Please, someone point out where my algorithm is flawed.

Upvotes: 0

Views: 62

Answers (2)

rohit89
rohit89

Reputation: 5773

addBook takes pointers of type Book but you are passing pointers of type Employee

Edit:

So, first you don't need to do things like *curr->next = *head. It should be curr->next = head. Also, head->next can be null which is not being checked. Finally, head needs to always be pointed at the start of the list.

Edit 2:

Following code should work. head always points to the start of the list. In order to do that, we must pass the address of the head pointer. We need to do this because we will be modifying the address of head.

I've also cleaned up a few things.

void insertInOrder(employee **head, employee *curr) {

  if (*head == NULL) {
    // We are inserting the first element
    *head = curr;
  }
  else if ((*head)->next == NULL) {
    // This is the second element. We either insert it in front of head or before head.
    if ((*head)->employeeID < curr->employeeID) {
      (*head)->next = curr;
    }
    else {
      curr->next = *head;
      *head = curr;
      (*head)->next = NULL;
    }
  }

  else {
    // We iterate through the list trying to find the best spot to insert curr.
    employee *temp = *head;
    while (temp->next != NULL) {
        if ((temp->employeeID < curr->employeeID) && (curr->employeeID < temp->next->employeeID))     {
            curr->next = temp->next;
            temp->next = curr;
            break;
        }
        temp = temp->next;
    }
    // curr has the greatest id so it is inserted at the end
    if (temp->next == NULL)
      temp->next = curr;  
  }
}

void addEmployee(char name[], employee **head) {
  int id;
  printf("Enter id\n");
  scanf("%d", &id);
  employee *curr = malloc(sizeof(employee));
  strcpy(curr->name, name);
  curr->employeeID = id;
  curr->next = NULL;
  insertInOrder(head, curr);
}

int main(void) {
  int quit = 1;
  employee *head = NULL;
  char title[100];
  printf("Enter data about the employees: \n");
  while (quit) {
    scanf("%s", title);
    if (title[0] != '#') 
        addEmployee(title, &head);
    else break;
  }
  return 0;
}

Upvotes: 1

inside the function no need to use *head or *curr ..because -> is made up for pointers only instead directly use head->left & curr->next

thanks

Upvotes: 0

Related Questions