Guest User

Untitled

a guest
May 27th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. struct Lista *InsertOrd(struct Lista *head, const char *data) {
  2. struct Lista *newp;
  3. struct Lista *tmp;
  4. char *new_data;
  5.  
  6. /* aloca o novo item */
  7. newp = ((struct Lista*)malloc(sizeof(struct Lista)));
  8. new_data = strdup(data);
  9. //strdup aloca memória na pilha.
  10. if (newp == NULL || new_data == NULL) {
  11. fprintf(stderr, "out of memory");
  12. return NULL;
  13. }
  14. newp->data = new_data;
  15. newp->next = NULL;
  16. newp->prev=NULL;
  17. /* check if element should be inserted at the head */
  18. if (head == NULL || strcmp(new_data, head->data) < 0) {
  19. newp->next = head;
  20. newp->prev=head;
  21. head = newp;
  22. } else {
  23. /* otherwise find the point of insertion */
  24. tmp = head;
  25. while (tmp->next && strcmp(new_data, tmp->next->data) >= 0) {
  26. tmp = tmp->next;
  27. }
  28. newp->next = tmp->next;
  29. tmp->next = newp;
  30. newp->prev=tmp;
  31. }
  32. return head;
  33. }
Add Comment
Please, Sign In to add comment