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 :: what is the difference between algorithm and flowchart in c program 
C :: logarithmus c math.h 
C :: WAP to create Database using array of structure & display it in C 
C :: write the data in perticulare memmory loaction in C 
C :: how to know a type of a numbe in c 
C :: taking input and converting it to a string in c 
C :: c check if character is a punctuation 
C :: how much larger integer i can input in c language? 
C :: search and then change string -- strstr and strcpy 
C :: too many arg 
C :: /usr/bin/mandb: fopen /var/cache/man/7935: Permission denied 
C :: inline function in c example 
C :: arma 3 nearest terrain objects 
C :: scanf autotrash c 
C :: else if statement in c 
C :: difference between int main() and int main(void) 
C :: C Create struct Variables 
Dart :: round corner of alertdialog flutter 
Dart :: How to change OutlinedButton border color? 
Dart :: const text style flutter 
Dart :: text field make screen goes white flutter 
Dart :: type check of variable dart 
Dart :: How to add a circular dot as an indicator in Flutter Tabs? 
Dart :: trailing flutter with 2 icons flutter 
Dart :: how to repeatedly call a function flutter 
Dart :: type convertion string to double 
Dart :: flutter display widget based on device orientation 
Dart :: flutter container height 100 percent 
Dart :: add border color to one side and rounded border container flutter 
Dart :: add a clickable link in flutter 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =