Monday, April 25, 2011

HAV C PROGRAMS

Program for getting the disk status
#include"stdio.h"
#include"conio.h"
#include"dos.h"
void main(void)
{
clrscr();
union REGS regs;
regs.h.ah = 1;
regs.h.dl = 0x80;
int86(0x13,®s,®s);
printf("If successful operation then AH & AL register resets.");
printf("AH register - %d",regs.h.ah);
printf("AL register - %d",regs.h.al);
printf("Successful Operation.");
getch();
}
Factorial series-e^x
#include "stdio.h"
#include "conio.h"
#include "math.h"
long int factorial(int n);
void main()
{
int x,i;
float s,r;
char c;
clrscr();
printf("You have this series:-1+x/1! + x^2/2! + x^3/3! + x^4/4!..x^x/x!");
printf("To which term you want its sum? ");
scanf("%d",&x);
s=0;
for (i=1;i<=x;i++)
{ s=s+((float)pow(x,i)/(float)factorial(i)); }
printf("The sum of %d terms is %f",x,1+s);
fflush(stdin);
getch();
}
long int factorial(int n)
{
if (n<=1)
return(1);
else
n=n*factorial(n-1);
return(n);
}
Circular Linked List in C
#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
#include"alloc.h"
#define null 0
struct node
{
int info;
struct node *link;
}*start;
void main()
{
int ch,n,m,position,i;
last=null;
while(1)
{
printf("1.create
2.addat
3.addbt
4.del
5.disp
6.exit
");
printf("er ur ch");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("er no of itc");
scanf("%d",&n);
for(i=0;i
{
printf("er the element");
scanf("%d",&m);
create(m);
}break;
case 2:
printf("er the element");
scanf("%d",&m);
addat(m);
break;
case 3:
printf("er the element");
scanf("%d",&m);
printf("er the position");
scanf("%d",&position);
addbt(m,position);
break;
case 4:
if(last==null)
{
printf("list is empty");
continue;
}
printf("er the element for delete");
scanf("%d",&m);
del(m);
break;
case 5:
disp();
break;
case 6:
exit(0);
break;
default:
printf("wrong choice");
}
}
}
create(int data)
{
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=null;
if(last==null)
{
last=tmp;
tmp->link=last;
}
else
{
tmp->link=last->link;
last->link=tmp;
last=tmp;
}}
addat(int data)
{
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=last->link;
last->link=tmp;
}
addbt(int data,int pos)
{
struct node *tmp,*q;
int i;
q=last->link;;
for(i=0;i
{
q=q->link;
if(q==last->link)
{
printf("there r lessthan %d elements",pos);
return;
}
}
tmp=(struct node *)malloc(sizeof(struct node));
tmp->link=q->link;
tmp->info=data;
q->link=tmp;
if(q==last)
last=tmp;
}
del(int data)
{
struct node *tmp,*q;
if(last->link==last&&last->info==data)
{
tmp=last;
last=null;
free(tmp);
return;
}
q=last->link;
if(q->info==data)
{
tmp=q;
last->link=q->link;
free(tmp);
return;
}
while(q->link!=last)
{
if(q->link->info==data)
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
printf("element %d is deleted",data);
}
if(q->link->info=data)
{
tmp=q->link;
q->link=last->link;
free(tmp);
last=q;
return;}
printf("element%d is not found",data);
}
disp()
{
struct node *q;
if(last==null)
{
printf("list isdempty");
return;
}q=last->link;
while(q!=last)
{
printf("%d",q->info);
q=q->link;
}
printf("%d",last->info);
}



Linked list stack example
#include"stdio.h"
#include"string.h"
#include"stdlib.h"

typedef struct node {
char *str;
struct node *next;
STACKNODE;
void push(char *key, STACKNODE **stack);
char *pop(STACKNODE **stack);
int isempty(STACKNODE *stack);
char top(STACKNODE *stack);
int main() {
char line[1024];
char *key;
STACKNODE *stack;
stack = NULL;
while((fgets(line, 1024, stdin)) != NULL)
key = line, push(key, &stack);
while(!isempty(stack))
printf("%s", pop(&stack));
return 0;
}
char top(STACKNODE *stack) {
return *stack->str;
}
void push(char *line, STACKNODE **stack) {
STACKNODE *newnode;
newnode = (STACKNODE *)malloc(sizeof(STACKNODE));
newnode->str = strdup(line);
newnode->next = (*stack);
(*stack) = newnode;
}
char *pop(STACKNODE **stack) {
STACKNODE *oldnode;
char *key;
char *retval;

oldnode = (*stack);
key = (*stack)->str;
(*stack) = (*stack)->next;
free(oldnode);

retval = calloc(strlen(key)+1, sizeof(char));
strcpy(retval, key);
return retval;
free(retval);
}
int isempty(STACKNODE *stack) {
return stack == NULL;
}

No comments:

Post a Comment