Skip to main content

Posts

Showing posts with the label Data Structure

Radix Sort in C Language

Program to implement Radix Sort in C  #include<stdio.h> #include<conio.h> typedef struct queue {    int front,rear;    int item[10]; }q; void enqueue(q* qp,int val) {    (qp)->rear++;    (qp)->item[(qp)->rear]=val;    return; } int dequeue(q* qp) {    int val;    val=(qp)->item[(qp)->front];    (qp)->front++;    return val; } int empty(q* qp) {    if((qp)->rear<(qp)->front)  return 1;    return 0; } void radix(int*a,int m,int n) {    q qu[10];    int i,j,r,d=1,k;    for(i=1;i<=m;i++)    {  for(j=0;j<10;j++)  { qu[j].rear=-1; qu[j].front=0;  }  for(j=0;j<n;j++)  { r=a[j]%(d*10); r=r/d; enqueue(&qu[r],a[j]);  }  d*=10;  k=0;  for(j=0;j<10;j++) ...

Difference between Spanning tree and Minimal Spanning Tree

Spanning Tree A spanning tree of a connected, undirected graph  G  can also be defined as a maximal set of edges of  G  that contains no cycle, or as a minimal set of edges that connect all vertices. A sub-graph T of a connected graph G(V,E) is called a Spanning Tree if T is a tree and if T includes every vertex of G i.e. V(T)=V(G). If |V|=n and |E|=m, then the spanning tree of G must have n vertices and hence n-1 edges. The resultant spanning ensure that the graph remain connected and further there is no circuit in it. Two algorithms for finding a spanning tree are BFS ( Breadth First Search ) and DFS ( Depth First Search ). Minimum Spanning Tree A  minimum spanning tree  ( MST ) or  minimum weight spanning tree  is then a spanning tree with weight less than or equal to the weight of every other spanning tree. Two algorithms commonly used,  Prim's algorithm  and  Kruskal's algorithm . Note :- Differen...

To Implement Graph using Adjacency List in C

Program to implement Graph using Adjacency List  and traversing using DFS and BFS in C  #include<stdio.h>  #include<conio.h>  #include<alloc.h>  #define max 10  struct node  {  int vertex;  struct node *next;  };  typedef struct node* nodeptr;  typedef struct queue  { int front,rear; int arr[max];  };  typedef struct stack  { int top; int arr[max];  };  nodeptr getnode()  { nodeptr p; p=(nodeptr)malloc(sizeof(struct node)); p->next=NULL; return p;  }   int empty(struct stack *s)   {  if(s->top==-1)  { return 1;  }  else return 0;   }   void push(struct stack *s,int x)   { if(s->top==max-1) printf("\n Queue Overflow"); else { s->top++; s->arr[s->top]=x; }   }   int pop(struct stack *s)   { in...

Sparse Matrix using Array in C

Program to implement Sparse Matrix using Array in C language #include<stdio.h> #include<conio.h>  int spa[10][10];  void spmatrix(int,int,int);  void main()  { int  i,j,n,m,num,elem=0;    clrscr();    printf("\nenter no. of rows: ");    scanf("%d",&n);    printf("\nenter no. of column: ");    scanf("%d",&m);    printf("\nenter the elements of array\n");    for(i=0;i<n;i++)    { for(j=0;j<m;j++)      { scanf("%d",&spa[i][j]);      }    }    printf("\nmatrix:\n");    for(i=0;i<n;i++)    {for(j=0;j<m;j++)     { printf(" %d",spa[i][j]);     }     printf("\n");    }      for(i=0;i<n;i++)    { for(j=0;j<m;j++)      { if(spa[i][j]!=0)        elem++; ...

Program to implement Circular Header Linked List in C

#include<stdio.h> #include<conio.h> int item,c=0; struct node {  int num;  struct node *next;  }*start=NULL,*last=NULL;  typedef struct node node;  typedef struct node* nptr;  int count();  void ins_beg();  void ins_bet();  void ins_end();  void del_beg();  void del_bet();  void del_end();  void traverse();  void main()  {  int num;  clrscr();  while(1)  { clrscr();  printf("\nÛÛÛÛÛ MENU ÛÛÛÛÛ");  printf("\n1.INSERTION_BEG");  printf("\n2.INSERTION_BET");  printf("\n3.INSERTION_END");  printf("\n4.DELETION_BEG");  printf("\n5.DELETION_BET");  printf("\n6.DELETION_END");  printf("\n7.TRAVERSE");  printf("\n8.EXIT");  printf("\nENTER YOUR CHOICE: ");  scanf("%d",&num);  switch(num)  {   case 1:ins_beg(); break;   case 2:ins_bet(); break;   case 3:ins_en...

Delete Alternate Nodes in Linked List using C program

/* To delete alternative nodes in the linked list */ #include<stdio.h> #include<conio.h>  void inst_end();  void del_beg();  void traverse();  struct node  { int num;    struct node *next;  }*start=NULL;  typedef struct node node;  typedef struct node* nptr;  void main()  { char choice;    while(1)    {    clrscr();    printf("-------LINKED LIST-------\n\n");    printf(" 1.Insertion\n");    printf(" 2.Deletion\n");    printf(" 3.Traverse\n");    printf(" 4.Exit\n\n");    printf(" Enter your choice: ");    fflush(stdin);    scanf("%c",&choice);    switch(choice)    { case '1': inst_end();     break;      case '2': del_beg();     break;      case '3': traverse();     break;   ...

Circular Linked List using C

#include<stdio.h> #include<conio.h> #include<stdlib.h> struct node {  int num;  struct node *next;  }*start=NULL,*last=NULL;  typedef struct node node;  typedef struct node* nptr;  int count()  {  int c=1;  nptr p;  p=start;  while(p->next!=start)  {   p=p->next;   c++;   }   return c++;   }  void ins_beg() { int item; nptr p,q; p=(nptr)malloc(sizeof(node)); printf("\nenter the item: "); scanf("%d",&item); p->num=item; if(start==NULL) { last=start=p; p->next=start; } else { q=start; while(q->next!=start) q=q->next; p->next=start; start=p; q->next=start; } } void ins_end() { int item; nptr p,q; p=(nptr)malloc(sizeof(node)); printf("\nenter the item: "); scanf("%d",&item); p->num=item; if(start==NULL) { start=p; p->next=start; } else { last->next=p; p->next=start; last=p; } }...

Quick Sort of an Array List using C

#include <stdio.h> #include <conio.h> int split ( int*, int, int ) ; void main( ) {     int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ;     int i ;     void quicksort ( int *, int, int ) ;     clrscr( ) ;     printf ( "Quick sort.\n" ) ;     printf ( "\nArray before sorting:\n") ;     for ( i = 0 ; i <= 9 ; i++ )         printf ( "%d\t", arr[i] ) ;     quicksort ( arr, 0, 9 ) ;     printf ( "\nArray after sorting:\n") ;     for ( i = 0 ; i <= 9 ; i++ )         printf ( "%d\t", arr[i] ) ;     getch( ) ; } void quicksort ( int a[ ], int lower, int upper ) {     int i ;     if ( upper > lower )     {         i = split ( a, lower, upper ) ;         quicksort ( a, lower, i - 1 ) ;         qu...

Heap Sort implementation using C

#include <stdio.h> #include <conio.h> void makeheap ( int [ ], int ) ; void heapsort ( int [ ], int ) ; void main( ) {     int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ;     int i ;     clrscr( ) ;     printf ( "Heap Sort.\n" ) ;     makeheap ( arr, 10 ) ;     printf ( "\nBefore Sorting:\n" ) ;     for ( i = 0 ; i <= 9 ; i++ )         printf ( "%d\t", arr[i] ) ;     heapsort ( arr, 10 ) ;     printf ( "\nAfter Sorting:\n" ) ;     for ( i = 0 ; i <= 9 ; i++ )         printf ( "%d\t", arr[i] ) ;     getch( ); } void makeheap ( int x[ ], int n ) {     int i, val, s, f ;     for ( i = 1 ; i < n ; i++ )     {         val = x[i] ;         s = i ;         f = ( s - 1 ) / 2 ;     ...

Sorting in Linked List

#include<stdio.h> #include<conio.h>  void inst_end();  void traverse();  void sorting();  struct node  { int num;    struct node *next;  }*start=NULL;  typedef struct node node;  typedef struct node* nptr;  void main()  { char choice;    while(1)    {    clrscr();    printf("-------LINKED LIST-------\n\n");    printf(" 1.Insertion \n");    printf(" 2.Sorting\n");    printf(" 3.Traverse\n");    printf(" 4.Exit\n\n");    printf(" Enter your choice: ");    fflush(stdin);    scanf("%c",&choice);    switch(choice)    { case '1': inst_end();       break;      case '2': sorting();       break;      case '3': traverse();       break;      case '4': exit(0); ...

Binary Search Tree using C

BST (Binary Search Tree) implementation using C language Operations: - Create tree, Preorder traversal, Postoreder treaversal, and Inoreder traversal /* BINARY SEARCH TREE */ #include<stdio.h> #include<conio.h>   struct node   { int info;     struct node *left_child;     struct node *right_child;   }*p=NULL;   typedef struct node node;   typedef struct node * nptr;   //nptr p=NULL;   nptr create_tree(nptr,int);   void postorder(nptr);   void preorder(nptr);   void inorder(nptr);     void main()   { int ch,num;          while(1)     { clrscr();       printf("\nBINARY SEARCH TREE \n\n");       printf(" 1.Create tree\n");       printf(" 2.Inorder traversal\n");       printf(" 3.Preorder traversal\n");       printf(" 4.Postorder traversal\n");       printf(" 5.Exit\n"...

Dynamic Queue Implementation using Linked List

#include<stdio.h> #include<conio.h>  void insertion();  void deletion();  void traverse();  struct node  { int num;    struct node *next;  }*front=NULL,*rear=NULL;  typedef struct node node;  typedef struct node* nptr;  void main()  { char choice;    while(1)    {    clrscr();    printf("DYNAMIC QUEUE\n\n");    printf(" 1.Insertion\n");    printf(" 2.Deletion\n");    printf(" 3.Traverse\n");    printf(" 4.EXIT\n");    printf(" Enter your choice: ");    fflush(stdin);    scanf("%c",&choice);    switch(choice)    { case '1': insertion();       break;      case '2': deletion();       break;      case '3': traverse();       break;      case '4': exit(0); ...

Dynamic Stack Implementation using Linked List

#include<stdio.h> #include<conio.h>  void push();  void pop();  void traverse();  struct node  { int num;    struct node *next;  }*top=NULL;  typedef struct node node;  typedef struct node* nptr;  void main()  { char choice;    while(1)    {    clrscr();    printf("-----DYNAMIC STACK-----\n\n");    printf(" 1.PUSH\n");    printf(" 2.POP\n");    printf(" 3.TRAVERSE\n");    printf(" 4.EXIT\n");    printf(" Enter your choice: ");    fflush(stdin);    scanf("%c",&choice);    switch(choice)    { case '1': push();       break;      case '2': pop();       break;      case '3': traverse();       break;      case '4': exit(0);      default: printf(...

Doubly Linked List using C

Doubly Linked List program using C language Operations : - Create List, insertion (beg, mid, end) deletion (beg, mid, end), Traverse #include<stdio.h> #include<conio.h>  void inst_beg();  void inst_mid();  void inst_end();  void del_beg();  void del_mid();  void del_end();  void traverse();  void reverse();  int count();  struct node  { int num;    struct node *next;    struct node *prev;  }*start=NULL,*last=NULL;  typedef struct node node;  typedef struct node* nptr;  void main()  { char choice;    while(1)    {    clrscr();    printf("-------LINKED LIST-------\n\n");    printf(" 1.Insertion at begining\n");    printf(" 2.Insertion at mid\n");    printf(" 3.Insertion at end\n");    printf(" 4.Deletion from begining\n");    printf(" 5.Deletion from mid\n")...

Linked List using C

Linked List program using C language Operations :- Create list, insertion (beg, mid, end), deletion (beg, mid, end) #include<stdio.h> #include<conio.h>  void inst_beg();  void inst_mid();  void inst_end();  void del_beg();  void del_mid();  void del_end();  void traverse();  int count();  struct node  { int num;    struct node *next;  }*start=NULL;  typedef struct node node;  typedef struct node* nptr;  void main()  { char choice;    while(1)    {    clrscr();    printf("-------LINKED LIST-------\n\n");    printf(" 1.Insertion at begining\n");    printf(" 2.Insertion at mid\n");    printf(" 3.Insertion at end\n");    printf(" 4.Deletion from begining\n");    printf(" 5.Deletion from mid\n");    printf(" 6.Deletion from end\n");    printf(" 7.Traverse\n");    printf(" 8....