Search
 
SCRIPT & CODE EXAMPLE
 

C

Sort linked list 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;
    }
}
Comment

C linked sorted lists

#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("
The numbers are:
");
    while(p) {
        printf("%d ", p->data);
        p = p->ptr;
    }
    free_list(head);
    return 0;
}
Comment

Sort linked list 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;
        }
    }
Comment

PREVIOUS NEXT
Code Example
C :: c arrays 
C :: linked list in c 
C :: printf("%d", 10 ? 0 ? 5:1:1:12) what will print 
C :: hello world in c language 
C :: concate string in c 
C :: c get string 
C :: print name of file argv c 
C :: array length in c++ 
Dart :: flutter remove debug badge 
Dart :: How to attach a FloatingActionButton to the AppBar 
Dart :: change color of drawer icon flutter 
Dart :: datetime dart format print 
Dart :: flutter random pick in list 
Dart :: how to change color in container flutter 
Dart :: flutter appbar trailing icon 
Dart :: regex numbers only dart 
Dart :: dart shuffle list 
Dart :: flutter check if key exists 
Dart :: dart async vs async* 
Dart :: underline text flutter color 
Dart :: flutter AnimatedOpacity 
Dart :: flutter firestore update 
Dart :: flutter listtile 
Dart :: dash border style flutter 
Dart :: how to validate textformfield on text change flutter 
Dart :: flutter widget for space 
Dart :: bottomsheet shape flutter 
Dart :: shape property of card in flutter 
Dart :: dart create singleton with parameters 
Dart :: dart null aware operators 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =