“Triez la liste liée C” Réponses codées

Triez la liste liée C

void BubbledSort_linked_list(struct Node **head)
{
    Node * curr = *head;
    Node * next;
    int temp;

    while (curr && curr->next)
    {

        Node * next = curr->next;
        while (next)
        {
            if (curr->data > next->data)
            {
                std::swap(next->data, curr->data);
            }
            next = next->next;
        }
        curr = curr->next;
    }
}
Spotless Shark

Triez la liste liée C

// Using array
    for(int i=0;i<ar.length;i++){
        for(int j=0;j<ar.length-1;j++){
            if(ar[j]>ar[j+1]){
                int temp = ar[j];
                ar[j]=ar[j+1];
                ar[j+1] = temp;
            }
        }
    }

// Using linkedlist
    void bubblesortlinkedlist(Node head){
        Node i= head,j=head;
        while(i!=null){
            while(j.next!=null){
                if(j.data>j.next.data){
                    int temp = j.data;
                    j.data = j.next.data;
                    j.next.data = temp;
                }
                j=j.next;
            }
            j=head;
            i=i.next;
        }
    }
Spotless Shark

C Listes triées liées

#include <stdio.h>
#include <stdlib.h>
 
typedef struct node{
    int data;
    struct node *ptr;
} node;
 
node* insert(node* head, int num) {
    node *temp, *prev, *next;
    temp = (node*)malloc(sizeof(node));
    temp->data = num;
    temp->ptr = NULL;
    if(!head){
        head=temp;
    } else{
        prev = NULL;
        next = head;
        while(next && next->data<=num){
            prev = next;
            next = next->ptr;
        }
        if(!next){
            prev->ptr = temp;
        } else{
            if(prev) {
                temp->ptr = prev->ptr;
                prev-> ptr = temp;
            } else {
                temp->ptr = head;
                head = temp;
            }            
        }   
    }
    return head;
}
 
void free_list(node *head) {
    node *prev = head;
    node *cur = head;
    while(cur) {
        prev = cur;
        cur = prev->ptr;
        free(prev);
    }       
}
 
int main(){
    int num;
    node *head, *p;
    head = NULL;
    do {
        printf("Enter a number");
        scanf("%d",&num);
        if(num) {
            head = insert(head, num);
        }
    } while(num);
    p = head;
    printf("\nThe numbers are:\n");
    while(p) {
        printf("%d ", p->data);
        p = p->ptr;
    }
    free_list(head);
    return 0;
}
Average Anteater

Réponses similaires à “Triez la liste liée C”

Questions similaires à “Triez la liste liée C”

Plus de réponses similaires à “Triez la liste liée C” dans C

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code