Tuesday, January 4, 2011

XOR list example

#include <stdio.h>
#include <conio.h>
#include

struct xnode
{
int data;
unsigned long direction;
};

struct xnode *add_data(int data, struct xnode* list);
void walk_list(struct xnode *list);

int main(void)
{

struct xnode *l2 = add_data(2, NULL);
struct xnode *l1 = add_data(1, l2);
struct xnode *l3 = add_data(3, l2);
struct xnode *l4 = add_data(4, l3);

printf("front -> back....\n");
walk_list(l1);
printf("back -> front....\n");
walk_list(l4);

return 0;
}

struct xnode *add_data(int data, struct xnode *list)
{
struct xnode *newxnode = malloc(sizeof(struct xnode));

assert(newxnode);
newxnode->direction = (unsigned long)list;
newxnode->data = data;

if(list != NULL)
list->direction ^= (unsigned long)newxnode;

return newxnode;
}

void walk_list(struct xnode *list)
{
unsigned long prev = 0;

while(list != NULL) {
unsigned long next = prev ^ list->direction;
printf("%d ", list->data);
prev = (unsigned long)list;
list = (struct xnode *)next;
}

printf("\n");
}

SWAP NUMBERS

WAP TO SWAP THE THREE DIGIT NUMBER
void main ()
{
int b,r,n,r1,r2;
clrscr ();
printf ("Enter the Value: ");
scanf ("%d",&n);
r=n%10;
n=n/10;
r1=n%10;
n=n/10;
r2=n%10;
n=n/10;
b=(r*100)*(r2*10)+(r2);
printf ("%d%d%d",r,r1,r2);
getch ();
}

SUM OF ARRAY

WAP TO SUM OF FIVE ELEMENTS OF AN ARRAY
void main ()
{
int no[5],i,sum;
clrscr ();
for (i=0;i<=4;i++)
{
printf ("Enter Element: ");
scanf ("%d",&no[i]);
}
sum=no[0]+no[1]+no[2]+no[3]+no[4];
printf ("\nSum of five Elements: %d",sum);
getch ();
}

SUM,SUB,PRODUCT,DIVISION

WAP TO SUM, SUBTRACT, MULTIPLY & DIVISION OF TWO NUMBERS (5 VARIABLES)

#include
void main ()
{
int a,b,c,d,e,f;
clrscr();
printf ("Enter A: ");
scanf ("%d",&a);
printf ("Enter B: ");
scanf ("%d",&b);
c=a+b;
d=a-b;
e=a*b;
f=a/b;
printf ("\nSum is : %d",c);
printf ("\nSubtraction is : %d",d);
printf ("\nMultiplication is : %d",e);
printf ("\nDivision is : %d",f);
getch ();
}

Output



Method #2
WAP TO SUM, SUBTRACT, MULTIPLY & DIVISION OF TWO NUMBERS (3 VARIABLES)

#include
void main ()
{
int a,b,c;
clrscr();
printf ("Enter A: ");
scanf ("%d",&a);
printf ("Enter B: ");
scanf ("%d",&b);
c=a+b;
printf ("\nSum is %d",c);
c=a-b;
printf ("\nSubtraction is %d",c);
c=a*b;
printf ("\nMultiplication is %d",c);
c=a/b;
printf ("\nDivision is %d",c);
getch ();
}

Output



REVERSE NUMBER

WAP TO REVERSE OF ANY NUMBER USING WHILE LOOP
void main ()
{
int no,r,res;
clrscr ();
printf ("Enter any value: ");
scanf ("%d",&no);
r=res=0;
while (no>0)
{
r=no%10;
no=no/10;
res=(res*10)+r;
}
printf ("\nReverse is %d",res);
getch ();
}

VALUE OF DATA TYPE

WAP TO PRINT VALUE OF MULTIPLE DATA TYPES
#include
#include
void main ()
{
int a=10;
float d=40.50;
char ch='A';
double dbl=78.9786;
long lng=7897711;
char nm [10]="JIMMY";
clrscr ();
printf ("\nInteger value is %d",a);
printf ("\nFloat value is %.2f",d);
printf ("\nCharacter value is %c",ch);
printf ("\nDouble value is %.4lf",dbl);
printf ("\nLong value is %ld",lng);
printf ("\nString value is %s",nm);
getch ();
}

PASSWORD VERIFICATION

WAP TO PRINT THE DETAIL OF THE PROGRAMMER 
IF THE GIVEN NUMBER IS 464
void main ()
{
int pass;
clrscr();
do
{
printf ("Enter Password to see the detail of programmer:\n");
scanf ("%d",&pass);
}
while (pass!=464);
printf ("\nJagjeet Singh");
printf ("\nB.Sc. (I.T.)\nPunjab Technical University");
getch ();
}