Friday, April 17, 2009

MERGE SORT

/* Write C program that implement the following sorting methods to sort a given list of integers in ascending order:
ii) Merge sort */

#include
#include
//#define MAX_ARY 10
void merge_sort(int x[], int end, int start);
int n;
int main(void)
{
int ary[20];
int j = 0,n;
printf("enter n value\n");
scanf("%d",&n);
printf("\n\nEnter the elements to be sorted: \n");
for(j=0;j scanf("%d",&ary[j]);
/* array before mergesort */
printf("Before :");
for(j = 0; j < n; j++)
printf(" %d", ary[j]);

printf("\n");

merge_sort(ary, 0, n - 1);

/* array after mergesort */
printf("After Merge Sort :");
for(j = 0; j < n; j++)
printf(" %d", ary[j]);

printf("\n");
getch();
}
/* Method to implement Merge Sort*/
void merge_sort(int x[], int end, int start) {
int j = 0;
const int size = start - end + 1;
int mid = 0;
int mrg1 = 0;
int mrg2 = 0;
int executing[20];
if(end == start)
return;
mid = (end + start) / 2;
merge_sort(x, end, mid);
merge_sort(x, mid + 1, start);

for(j = 0; j < size; j++)
executing[j] = x[end + j];

mrg1 = 0;
mrg2 = mid - end + 1;

for(j = 0; j < size; j++) {
if(mrg2 <= start - end)
if(mrg1 <= mid - end)
if(executing[mrg1] > executing[mrg2])
x[j + end] = executing[mrg2++];
else
x[j + end] = executing[mrg1++];
else
x[j + end] = executing[mrg2++];
else
x[j + end] = executing[mrg1++];
}
}

INFIX TO POST FIX

/*Infix To Postfix*/

#include
#include
struct stack
{
int top;
char item[30];
};

void push(struct stack *ps,char n)
{
ps->top++;
ps->item[ps->top]=n;
}

void pop(struct stack *ps)
{
ps->top--;
}

int isempty(struct stack *ps)
{
if(ps->top==-1)
{
return(1);
}
else
{
return(0);
}
}

int prcd(char stktop,char op)
{
if(((stktop=='+')||(stktop=='-'))&&((op=='*')||(op=='/')))
{
return(0);
}
if(stktop=='(')
{
return(0);
}
if((stktop!=')')&&(op=='('))
{
return(0);
}
if((stktop!='(')&&(op==')'))
{
return(1);
}
}
void main()
{
struct stack s;
char input[30],output[30];
char optop,ans;
int i=0,j=0,k=0;
s.top=-1;
clrscr();
printf("enter an expression in infix form:\n");
scanf("%s",input);
while(input[i]!='\0')
{
if(isalpha(input[i]))
{
output[j]=input[i];
j++;
}
else
{
while(!isempty(&s)&&(prcd(s.item[s.top],input[i])))
{
optop=s.item[s.top];
pop(&s);
output[j]=optop;
j++;
}
if(isempty(&s)||input[i]!=')')
{
push(&s,input[i]);
}
else
{
pop(&s);
}
}

i++;
}

while(!isempty(&s))
{
optop=s.item[s.top];
pop(&s);
output[j]=optop;
j++;
}
printf("\npostfix form:\t");
output[j]='\0';

while(output[k]!='\0')
{
printf("%c",output[k]);
k++;
}
scanf("%c",&ans);
getch();
}

DOUBLE LINKED LIST

#include
#include
#include
#include
struct node
{
int data;
struct node *ptr;
};
typedef struct node NODE;
NODE *first=NULL,*last=NULL;
NODE *nn,*temp,*temp1,*temp2;
main()
{
int ch,r=1;
clrscr();
while(r)
{
printf("enter the choice\n");
printf("1.entering from the front\n");
printf("2.entering from the end\n");
printf("3.deleting from the front\n");
printf("4.deleting from the end\n");
printf("5.display\n");
printf("6.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:enterf();
break;
case 2: entere();
break;
case 3:delf();
break;
case 4:dele();
break;
case 5:display();
break;
case 6:return;
}
printf("do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&r);
}
}
enterf()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->ptr=nn;
if(first==NULL)
{
first=last=nn;
}
else
{
last->ptr=nn;
nn->ptr=first;
first=nn;
}
}

display()
{
if(first==NULL)
printf("empty list\n");
else if(first->ptr==first)
printf("%d->",first->data);
else
{
temp=first;
while(temp!=last)
{
printf("%d->",temp->data);
temp=temp->ptr;
}
printf("%d->",last->data);
}
}

entere()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->ptr=nn;
if(first ==NULL)
{
first=last=nn;
}
else
{
last->ptr=nn;
nn->ptr=first;
last=nn;
}
}
delf()
{
if(first==NULL)
printf("empty list\n");
else if(first->ptr==first)
{
printf("deleted element =%d",first->data);
free(first);
first=last=NULL;
}
else
{
temp=first;
first=first->ptr;
printf("deleted element=%d",temp->data);
free(temp);
last->ptr=first;
}
}
dele()
{
if(first==NULL)
printf("empty list\n");
else if(last->ptr==last)
{
printf("deleted element=%d",first->data);
free(first);
first=last=NULL;
}
else
{
temp1=first;
while(temp1->ptr!=last)
{
temp1=temp1->ptr;
}
printf("deleted element=%d",last->data);
free(last);
last=temp1;
last->ptr=first;
}
}

USING CQUEUE

#include
#include
#define max 5
int front=-1,rear=-1;
int cq[max];
void main()
{
int rpt=1;
int ch;
clrscr();
while(rpt)
{
printf("1.cqinsert\n");
printf("2.cqdelete\n");
printf("3.cqdisplay\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:cqinsert();
break;
case 2:cqdelete();
break;
case 3:cqdisplay();
break;
case 4:return;
}
printf("Do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}

cqinsert()
{
int num;
if(front==(rear+1)%max)
{
printf("queue is full\n");
return;
}
else
{
printf("enter the element\n");
scanf("%d",&num);
rear=rear+1%max;
cq[rear]=num;
if(front==-1)
front=0;
}
}

cqdelete()
{
int num;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else
{
num=cq[front];
printf("Deleted number is %d",num);
if(front==rear)
front=rear=-1;
else
front=(front+1)%max;
}
}

cqdisplay()
{
int i;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else if(front {
for(i=front;i<=rear;i++)
printf("%d\n",cq[i]);
}
else
{
for(i=front;i printf("%d\n",cq[i]);
for(i=0;i<=rear;i++)
printf("%d\n",cq[i]);
}
}

COMPLEX NUMBERS

#include
void main()
{
int i,a[10],rem,bin=0,base=1,num,j,k,l;
clrscr();
printf(" enter any number\n");
scanf("%d",&num);
i=0;
while(num>0)
{
rem=num%2;
bin=bin+rem*base;
base=base*10;
num=num/2;
a[i]=rem;
i++;
l=j=i;
}

for(i--;i>=0;i--)
printf("%d",a[i]);
for(j--;j>=0;j--)
{
if(a[j]==0a[j]==1)
continue;
else if(a[j]==0)
a[j]=1;
else
a[j]=0;
}
printf("The 2's complement of the given number %d is",num);
for(l--;l>=0;l--)
printf("%d",a[l]);
getch();
}

C-PROGRAMS/CIRCLE LINKED LIST

#include
#include
#include
#include
struct node
{
int data;
struct node *ptr;
};
typedef struct node NODE;
NODE *first=NULL,*last=NULL;
NODE *nn,*temp,*temp1,*temp2;
main()
{
int ch,r=1;
clrscr();
while(r)
{
printf("enter the choice\n");
printf("1.entering from the front\n");
printf("2.entering from the end\n");
printf("3.deleting from the front\n");
printf("4.deleting from the end\n");
printf("5.display\n");
printf("6.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:enterf();
break;
case 2: entere();
break;
case 3:delf();
break;
case 4:dele();
break;
case 5:display();
break;
case 6:return;
}
printf("do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&r);
}
}

Friday, March 20, 2009

FRESHERS PARTY #############


Waitin 4r dis day 4rm many daysssssssssssssssssssssssssssss.so frns letz rock dis ocassion with gr8 ecstacy .

place : TOUCH,banjara hills,road no .3
date: 22/3/09

Wednesday, March 18, 2009