Sunday, May 27, 2012

sorting programs and algorithms


/*Source code of simple quick sort implementation using array ascending order in c programming language*/
#include<stdio.h>
#include<conio.h>

void quicksort(int [10],int,int);

void main()
{
  int size,i;
  int a[]={5,9,7,2,4};
  n=5;
  clrscr();
/*  printf("Enter size of the array: ");
  scanf("%d",&n);

  printf("Enter %d elements: ",size);
  for(i=0;i<n;i++)
    scanf("%d",&a[i]);
  */
  quicksort(x,0,n-1);

  printf("Sorted elements: ");
  for(i=0;i<n;i++)
    printf(" %d",a[i]);
    getch();
}
void swap(int *x, int *y)
{
 int temp;
 temp=*x;
 *x=*y;
 *y=temp;
}

void quicksort(int x[10],int f,int l)
{
 int pivot,j,temp,i;
 if(f<last)
 {
   p=f;
   i=f;
   j=l;
   while(i<j)
   {
     while(x[i]<=x[p]&&i<l)
i++;
     while(x[j]>x[p])
j--;
     if(i<j)
     {
       swap(&x[i],&x[j]);
     }
    }//while
    swap(&x[p],&x[j]);
    quicksort(x,f,j-1);
    quicksort(x,j+1,l);
 }//if
}
/*
Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8*/

Friday, May 25, 2012

Linked List


//WAP TO DEMONSTRATE THE OPERATIONS ON Single Linked List
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct sl_node
{
 int m;
 struct sl_node *link;
}*start=NULL;
typedef struct sl_node node;
void create()
{
 int n,i,marks;
 node *temp,*r;
 printf("enter the no of elements of linked list\n");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
  printf("enter the marks\n");
  scanf("%d",&marks);
  temp=(node*)malloc(sizeof(node));
  temp->m=marks;
  //temp=temp->link;
 //}
  if(start==NULL)
  {
   temp->link=NULL;
   start=temp;
  }
  else
  {
   r=start;
   while(r->link!=NULL)
   {
    r=r->link;
   }
    temp->link=r->link;
    r->link=temp;
  // }//while
  } //else
 }//create
 }
void addbeg(int marks)
{
  node* temp;
  temp=(node*)malloc(sizeof(node));
  temp->m=marks;
  temp->link=start;
  start=temp;
}
void addend(int marks)
{
 node *temp,*r;
 temp=start;
 while(temp->link!=NULL)
 {
  temp=temp->link;
  r=(node*)malloc(sizeof(node));
  r->m=marks;
  r->link=temp->link;
  temp->link=r;
 }
}
//add after specified node number
void addafter(int marks,int pos)
{
 int i;
 node* temp,*r;
 temp=start;
 for(i=1;i<pos;i++)
 temp=temp->link;
 r=(node*)malloc(sizeof(node));
 r->m=marks;
 r->link=temp->link;
 temp->link=r;
 }


void display()
{
 node *temp=start;
 if(temp==NULL)

  printf("it is empty\n");
 else
 {

  printf("sll follows\n");

  while(temp!=NULL)
  {
   printf("%d->",temp->m);
   temp=temp->link;
  }//while
 }//else
}//disp()
void del(int marks)
{
 node *temp,*old;
 temp=start;
 while(temp!=NULL)
 {
  if(temp->m==marks)
  {
   if(temp==start)
    start=start->link;
   else
    old->link=temp->link;
   printf("%d is deleted from s.l\n",marks);
   free(temp);
  }
  else
  {
   old=temp;
   temp=temp->link;
  } //else
 } //while
 printf("%d is not found in the s.l\n",marks);
}//del()
void destroy()
{
 node *temp;
 while(start!=NULL)
 {
  temp=start->link;
  free(start);
  start=temp;
 }
 printf("linked list is destroyed\n");
}
void menu()
{
 printf("1.insert at the begining of ll\n");
 printf("2.insert at the end\n");
 printf("3.displaying \n");
 printf("4.delete node\n");
 printf("5.destroy the s.l\n");
 printf("6.exit\n");
}
void main()
{
 int ch,pos,marks,c,count;
 node *start=NULL;
 void create();
 void addbeg(int);
// void addafter(int,int);
 void addend(int);
 void display();
 void del(int);
 void destroy();
 clrscr();
 create();
 menu();

 do
 {
  printf("enter ur choice\n");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:{
printf("enter the marks\n");
scanf("%d",&marks);
addbeg(marks);
break;
       }
   case 2:{
printf("enter the marks\n");
scanf("%d",&marks);
addend(marks);
break;
       }
/*   case 3:{
printf("enter the marks\n");
scanf("%d",&marks);
addafter(marks);
break;
       }
*/
    case 3:{
display();
break;
}
    case 4:{
printf("enter the marks\n");
scanf("%d",&marks);
del(marks);
break;
       }
    case 5:{
destroy();
break;
}
    default :
      exit();
  }//switch()

}while(ch!=6);
}


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct ll
{
 int data;
 struct ll* link;
};
typedef struct ll node_type;

//creation of a linked list
node_type* sl_create()//this fun has no input but output first
{
 node_type *temp,*t,*k,*first;
 int x;
 first=(node_type*)malloc(sizeof(node_type));
 temp=first;
 printf("enter the data fields of all the nodes\n");
 while(scanf("%d",&x)!=EOF)
 //EOF MEANS CONTROL +Z
 {
  t=(node_type*)malloc(sizeof(node_type));
  t->data=x;//filing data field
  temp->link=t;
  temp=temp->link; //or t;//movin temp to latest node
 }
 temp->link=NULL;//fillin the link field of last node
 k=first;//put k pointer to dummy node to delete it by free
 first=first->link;//first come to action first node
 free(k);//
 return(first);
}
//displaying linked list
void ll_print(node_type *first)
{
 node_type *temp;
 for(temp=first;temp!=NULL;temp=temp->link)
 {
  printf("%d\t %u\n",temp->data,temp->link);//%u=address or position
 }
}


//concatination two linked lists
node_type* concatinate(node_type* first1,node_type* first2)
{
 node_type *temp;
 if((first1==NULL)&&(first2==NULL))
 {
  return(NULL);
 }
 else
 if((first1==NULL)&&(first2==NULL))
  return(first2);
 else if((first1!=NULL)&&(first2==NULL))
  return(first1);
 else
 { //putin a pointer temp &moving temp until temp link!=null
  for(temp=first1;temp->link!=NULL;temp=temp->link)
  {
   ;
  }
  temp->link=first2;
  return(first1);
 }
}

void main()
{
 node_type *first1,*first2,*first3;
 node_type* sl_create();
 node_type* concatinate(node_type* first1,node_type* first2);
 first1=sl_create();
 first2=sl_create();
 first3=concatinate(first1,first2);
 ll_print(first3);
}
*******************************************************************

//count a linked list
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct ll
{
 int data;
 struct ll* link;
};
typedef struct ll node_type;

//creation of a linked list
node_type* sl_create()//this fun has no input but output first
{
 node_type *temp,*t,*k,*first;
 int x;
 first=(node_type*)malloc(sizeof(node_type));
 temp=first;
 printf("enter the data fields of all the nodes\n");
 while(scanf("%d",&x)!=EOF)
 //EOF MEANS CONTROL +Z
 {
  t=(node_type*)malloc(sizeof(node_type));
  t->data=x;//filing data field
  temp->link=t;
  temp=temp->link; //or t;//movin temp to latest node
 }
 temp->link=NULL;//fillin the link field of last node
 k=first;//put k pointer to dummy node to delete it by free
 first=first->link;//first come to action first node
 free(k);//
 return(first);
}
//write a c function to find the no of nodes (ie. lenght of link.list
int count(node_type *first)
{
 node_type *temp;
 int ctr;
 ctr=0;
 for(temp=first;temp!=NULL;temp=temp->link)
 {
  ctr++;
 }
 return(ctr);
}


//displaying linked list
void ll_print(node_type *first)
{
 node_type *temp;
 for(temp=first;temp!=NULL;temp=temp->link)
 {
  printf("%d\t %u\n",temp->data,temp->link);//%u=address or position
 }
}


node_type * reverse(node_type *first)
{
 node_type *p,*q,*r;
 p=first;
 q=NULL;
 r=NULL;
 while(p!=NULL)
 {
  r=q;//a pointer moved one step right
  q=p;
  p=p->link;
  q->link=r;//addres of previous store NULL stored in r
   //bcoz of this only links are changed
 }
 first=q;
 return(first);
}

void main()
{
 node_type *first;
 node_type* sl_create();
 int ans,count();
 void ll_print();
 first=sl_create();//linked list created
 ans=count(first);
 printf("no of nodes is %d\n",ans);
}
 ll_print(first);//displaying
}
*********************************************************************************

//reversing a linked list
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct ll
{
 int data;
 struct ll* link;
};
typedef struct ll node_type;
//creation of a linked list
node_type* sl_create()//this fun has no input but output first
{
 node_type *temp,*t,*k,*first;
 int x;
 first=(node_type*)malloc(sizeof(node_type));
 temp=first;
 printf("enter the data fields of all the nodes\n");
 while(scanf("%d",&x)!=EOF)
 //EOF MEANS CONTROL +Z
 {
  t=(node_type*)malloc(sizeof(node_type));
  t->data=x;//filing data field
  temp->link=t;
  temp=temp->link; //or t;//movin temp to latest node
 }
 temp->link=NULL;//fillin the link field of last node
 k=first;//put k pointer to dummy node to delete it by free
 first=first->link;//first come to action first node
 free(k);//
 return(first);
}
//displaying linked list
void ll_print(node_type *first)
{
 node_type *temp;
 for(temp=first;temp!=NULL;temp=temp->link)
 {
  printf("%d\t %u\n",temp->data,temp->link);//%u=address or position
 }
}


node_type * reverse(node_type *first)
{
 node_type *p,*q,*r;
 p=first;
 q=NULL;
 r=NULL;
 while(p!=NULL)
 {
  r=q;//a pointer moved one step right
  q=p;
  p=p->link;
  q->link=r;//addres of previous store NULL stored in r
   //bcoz of this only links are changed
 }
 first=q;
 return(first);
}

void main()
{
 node_type *first,*reverse();
 node_type* sl_create();
 void ll_print();
 first=sl_create();//linked list created
 first=reverse(first);//function call
 ll_print(first);//displaying
}







Tuesday, June 8, 2010

Binary trees

Binary trees

A Binary Tree is a type of data structure in which each node has at most two children (left child and right child). Binary trees are used to implement binary search trees and binary heaps, and are used for efficient searching and sorting. A binary tree is a special case of a K-ary tree, where k is 2. Common operations for binary trees include insertion, deletion, and traversal. The difficulty of performing these operations varies if the tree is balanced and also whether the nodes are leaf nodes or branch nodes. For balanced trees the depth of the left and right subtrees ov every node differ by 1 or less. This allows for a predictable depth also known as height. This is the measure of a node from root to leaf, where root is 0 and sebsequent nodes are (1,2..n). This can be expressed by the integer part of log2(n) where n is the number of nodes in the tree.


The operations performed on trees requires searching in one of two main ways: Depth First Search and Breadth-first search. Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the root and explores as far as possible along each branch before backtracking. There are three types of depth first search traversal: pre-order visit, left, right, in-order left, visit, right, post-order left, right, visit. Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph structures. In level-order, where we visit every node on a level before going to a lower level.

Below is an implementation of a binary tree that has insertion and printing capabilities. This tree is ordered but not balanced. This example maintains its ordering at insertion time.