Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Lista *InsertOrd(struct Lista *head, const char *data) {
- struct Lista *newp;
- struct Lista *tmp;
- char *new_data;
- /* aloca o novo item */
- newp = ((struct Lista*)malloc(sizeof(struct Lista)));
- new_data = strdup(data);
- //strdup aloca memória na pilha.
- if (newp == NULL || new_data == NULL) {
- fprintf(stderr, "out of memory");
- return NULL;
- }
- newp->data = new_data;
- newp->next = NULL;
- newp->prev=NULL;
- /* check if element should be inserted at the head */
- if (head == NULL || strcmp(new_data, head->data) < 0) {
- newp->next = head;
- newp->prev=head;
- head = newp;
- } else {
- /* otherwise find the point of insertion */
- tmp = head;
- while (tmp->next && strcmp(new_data, tmp->next->data) >= 0) {
- tmp = tmp->next;
- }
- newp->next = tmp->next;
- tmp->next = newp;
- newp->prev=tmp;
- }
- return head;
- }
Add Comment
Please, Sign In to add comment