• Streams & Subjects

    Streams & Subjects

    Get detailed description of each and every topic with proper examples including text, images and videos.

  • Latest Technologies

    Latest Technologies

    Know more about the latest and new emerging technologies and boost your knowledge.

  • Programming

    Programming

    Don't learn just codes and syntax, instead learn the best and efficient way of coding.

  • Technologies

    New Technologies

    Get instant and relevant updates of the latest emerging technologies of lots of streams including web, clouds, virtualization, etc.

  • Freewares

    Freewares

    Download free trials and freewares of various fields and check out their efficiency before buying.

  • Multimedia

    Multimedia

    Learn techniques to design amazing and creative multimedia designs and characters including 2D & 3D layout with rendering.

Showing posts with label Data Structure Programs. Show all posts
Showing posts with label Data Structure Programs. Show all posts

Program to demonstrate Treesort in string array

Write a program to demonstrate Treesort in string array

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

struct tnode
{
            char *str;
            struct tnode *left;
            struct tnode *right;
};

void insert(struct tnode **p, char *value);
void print(struct tnode *root);

int main(void)
{
            char line[1024];
            struct tnode *root;

            root = NULL;
            while((fgets(line, 1024, stdin)) != NULL)
            insert(&root, line);

            print(root);
            return 0;
}

/* call by reference .. ! */
void insert(struct tnode **p, char *value)
{
            if(!*p) {
                        *p = (struct tnode *)malloc(sizeof(struct tnode));
                        (*p)->left = (*p)->right = NULL;
                        (*p)->str = strdup(value);
                        return;
            }

            if(strcmp(value, (*p)->str) < 0)
                        insert(&(*p)->left, value);
            else
                        insert(&(*p)->right, value);
}

/* inorder binary tree print ... */
void print(struct tnode *root)
{
            if(root != NULL)
            {
                        print(root->left);
                        printf("%s", root->str);
                        print(root->right);
            }
}


Read more

Program in C to demonstrate selection sort linked listrogram in C to demonstrate selection sort linked list

Write a program in C to demonstrate selection sort linked list


#include "stdio.h"

void selection_sort(int a[], int size);

int main(void)
{
            int arr[10] = {10, 2, 4, 1, 6, 5, 8, 7, 3, 9};
            int i = 0;

            printf("before:\n");
            for(i = 0; i < 10; i++) printf("%d ", arr[i]);
                        printf("\n");
           
            selection_sort(arr, 10);
           
            printf("after:\n");
            for(i = 0; i < 10; i++) printf("%d ", arr[i]);
                        printf("\n");
           
            return 0;
}

void selection_sort(int a[], int size)
{
            int i = 0;
            int j = 0;
            int large = 0;
            int index = 0;
           
            for(i = size - 1; i > 0; i--)
            {
                        large = a[0];
                        index = 0;
                        for(j = 1; j <= i; j++)
                                    if(a[j] > large)
                                    {
                                                large = a[j];
                                                index = j;
                                    }
                        a[index] = a[i];
                        a[i] = large;
            }
}


Read more

Program in C to demonstrate selection sort linked list

Write a program in C to demonstrate selection sort linked list


#include "stdio.h"
#include "stdlib.h"

#define MAX 10

struct lnode
{
            int data;
            struct lnode *next;
} *head, *visit;

/* add a new entry to the linked list */
void llist_add(struct lnode **q, int num);

/* preform a selection sort on the linked list */
void llist_selection_sort(void);

/* print the entire linked list */
void llist_print(void);

int main(void)
{
            /* linked list */
            struct lnode *newnode = NULL;
            int i = 0; /* a general counter */

            /* load some random values into the linked list */
            for(i = 0; i < MAX; i++) {
                        llist_add(&newnode, (rand() % 100));
            }
           
            head = newnode;
            printf("Before selection sort:\n");
            llist_print();
            printf("After selection sort:\n");
            llist_selection_sort();
            llist_print();
           
            return 0;
}

/* adds a node at the end of a linked list */
void llist_add(struct lnode **q, int num)
{
            struct lnode *temp;

            temp = *q;

            /* if the list is empty, create first node */
            if(*q == NULL) {
                        *q = malloc(sizeof(struct lnode));
                        temp = *q;
            } else {
                        /* go to last node */
                        while(temp->next != NULL)
                        temp = temp->next;
           
                        /* add node at the end */
                        temp->next = malloc(sizeof(struct lnode));
                        temp = temp->next;
            }

            /* assign data to the last node */
            temp->data = num;
            temp->next = NULL;
}

/* print the entire linked list */
void llist_print(void)
{
            visit = head;
           
            /* traverse the entire linked list */
            while(visit != NULL)
            {
                        printf("%d ", visit->data);
                        visit = visit->next;
            }
            printf("\n");
}

void llist_selection_sort(void)
{
            struct lnode *a = NULL;
            struct lnode *b = NULL;
            struct lnode *c = NULL;
            struct lnode *d = NULL;
            struct lnode *tmp = NULL;
           
            a = c = head;
            while(a->next != NULL)
            {
                        d = b = a->next;
                        while(b != NULL)
                        {
                                    if(a->data > b->data)
                                    {
                                                /* neighboring linked list node */
                                                if(a->next == b)
                                                {
                                                            if(a == head)
                                                            {
                                                                        a->next = b->next;
                                                                        b->next = a;
                                                                        tmp = a;
                                                                        a = b;
                                                                        b = tmp;
                                                                        head = a;
                                                                        c = a;
                                                                        d = b;
                                                                        b = b->next;
                                                            } else {
                                                                        a->next = b->next;
                                                                        b->next = a;
                                                                        c->next = b;
                                                                        tmp = a;
                                                                        a = b;
                                                                        b = tmp;
                                                                        d = b;
                                                                        b = b->next;
                                                            }
                                                } else {
                                                            if(a == head)
                                                            {
                                                                        tmp = b->next;
                                                                        b->next = a->next;
                                                                        a->next = tmp;
                                                                        d->next = a;
                                                                        tmp = a;
                                                                        a = b;
                                                                        b = tmp;
                                                                        d = b;
                                                                        b = b->next;
                                                                        head = a;
                                                            } else {
                                                                        tmp = b->next;
                                                                        b->next = a->next;
                                                                        a->next = tmp;
                                                                        c->next = b;
                                                                        d->next = a;
                                                                        tmp = a;
                                                                        a = b;
                                                                        b = tmp;
                                                                        d = b;
                                                                        b = b->next;
                                                            }
                                                }
                                    } else {
                                                d = b;
                                                b = b->next;
                                    }
                        }
                        c = a;
                        a = a->next;
            }
}


Read more