Wednesday, December 30, 2009

PUZZLE USING C

#include"dos.h"
#include"math.h"
#include"stdio.h''
#include"conio.h"
#include"stdlib.h"
#include"graphics.h"

#define N 500

int function[35];
int strobe[5] = { 0,0,0,0,0 };
int store[16] = { 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 };
int inputs[16];
int output=0,count=0,decieq,choice;

int driver=DETECT,mode;

/***********FUNCTIONS FOR BEGINING GUI***************/

struct stars
{

int x,y,x1,y1,z,c;

}STARS[N];

void init()
{

int i,t;

for(i=0;i
{

STARS[i].c=2;
STARS[i].x = random(640);
STARS[i].y = random(460);
STARS[i].x1= random(320);
STARS[i].y1= random(220);

t=random(325);
if(t==0)
STARS[i].z=t+random(25)+1;//TO AVOID ERROR(division by 0)

else
STARS[i].z=t;

}

}

void draw()
{

int i;

for(i=0;i
{

if(STARS[i].y<480)
putpixel(STARS[i].x,STARS[i].y,STARS[i].c);
}

}

void erase()
{

int i;

for(i=0;i
putpixel(STARS[i].x,STARS[i].y,0); //putting black
//color at stars position

}
/* if you use z+=2 stars will go away from you , but remember to
change the condition :-
if(STARS[i].z<1) z=""> to if(STARS[i].z>325) {STARS[i].z=1;} */
void move()
{

int i;
for(i=0;i
{

STARS[i].x=((STARS[i].x1 * 640)/STARS[i].z)+320;//320 x-origin
STARS[i].y=((STARS[i].y1 * 300)/STARS[i].z) +220;//220 y-origin
STARS[i].z-=2;

if(STARS[i].z<1)
STARS[i].z=325;

else if(STARS[i].z<162)
STARS[i].c=15;

else STARS[i].c=7;

}

}

void showstars()
{

int i;

init();
while(!kbhit())
{

setcolor(3); //3 for cyan
settextstyle(1,0,3);
outtextxy(230,5,"PROJECT ON");
settextstyle(10,0,3);
setcolor(i);
outtextxy(170,30,"MULTIPLEXERS");

setcolor(5);
settextstyle(1,0,3);
outtextxy(350,320,"Made by:-");
setcolor(i);
outtextxy(420,345,"Himanshu Sardana");
move();
draw();
delay(15);
erase();
i++;

}

closegraph();
restorecrtmode();

}

void welcome()
{

int i, j, x, y, color;
int MaxColors;
struct palettetype palette;
struct viewporttype vp;
int height, width;

initgraph(&driver,&mode,"f: cgi");

MaxColors = getmaxcolor() + 1;

textcolor(4+BLINK);
printf(" WELCOME");
textcolor(15);

getpalette( &palette );
setviewport(0,50,630,470,1);
getviewsettings( &vp );

width = (vp.right - vp.left) / 15; //width
height = (vp.bottom - vp.top) / 10; // height

x = y = 0; // Start in upper corner
color = 1; // Begin at 1st color

for( j=0 ; j<10>
{ // 10 rows of boxes

for( i=0 ; i<15>
{ // 15 columns

setfillstyle( SOLID_FILL, color++ );//Set the color
bar( x, y, x+width, y+height ); // Draw the box
x += width + 1; // Advance to next col
color = 1 + (color % (MaxColors - 2));// Set new color
} // End of i loop

x = 0; // Goto 1st column
y += height + 1; // Goto next row

} // End of j loop

while( !kbhit() ) // Until a key is hit
setpalette( 1+random(MaxColors - 2), random( 65 ) );


setallpalette( &palette );
getch();
closegraph();
restorecrtmode();
initgraph(&driver,&mode,"f: cgi");
showstars();

}

/*********Begining GUI ends.Text mode begins*********/

/**************MULTIPLEXER FUNCTIONS*****************/

void message(int num) //gives a message to user
{ //everytime select input is entered

printf("Only first %d select inputs considered.rest rejected(if
any)",num);

}

int check(int num) //For checking that a particular no.
{ //is present in function or not

int ctr2;

for(ctr2=0;ctr2
if(function[ctr2]==num)
return (1); //true

return(0); //false

}

void check_invalid_strobe(int ctr2) //for checking invalid select
input
{

if(strobe[ctr2]>1 || strobe[ctr2]<0)
{

printf("Invalid select input");
exit(2);

}

}

void decide_output(int count) //decides whether output is 1 or 0
{ //for normal mux implementation

int ctr2;

for(ctr2=0;ctr2
{

if(decieq==function[ctr2])
{

output = 1;
break;

}

}

}

void decide_complex_inputs(int num) //decides inputs of a Mux which
{ //implements a function of a
//higher
//level Mux
int ctr2;

for(ctr2=0;ctr2
{

if(check(ctr2))
{

if(check(ctr2+num))
inputs[ctr2]=1;

else
{

inputs[ctr2]=!(strobe[0]);
store[ctr2] = 0;

}

}

else
{

if(check(ctr2+num))
{

inputs[ctr2]=strobe[0];
store[ctr2] = 1;

}

else
inputs[ctr2]=0;

}

}

}

void display_complex_input(int num) //displays inputs of a Mux which
{ //implements a function of ahigher
//level Mux
int i;

for(i=0;i
{

if(store[i]==0)
printf("Input %d = %s(%d)",i,"a bar",inputs[i]);

else if(store[i]==1)
printf("Input %d = %c(%d)",i,'a',inputs[i]);

else
printf("Input %d = %d",i,inputs[i]);

}

}

void take_input(int num) //for taking select input in normal Mux
{

int ctr2;

printf("Enter the select input: ");
for(ctr2=0;ctr2
{

scanf("%d",&strobe[ctr2]);
check_invalid_strobe(ctr2);

}

}

void take_input_a(int num) //for taking input a in case of complex
{ //implementation of a function

printf("Enter the value of a(MSB of select input for %d:1) : ",num);
scanf("%d",&strobe[0]);
check_invalid_strobe(0);
printf("Only one value of MSB considered.rest rejected(if any)");

}

void take_complex_select_input(int num)
{

int ctr2;

printf("Enter the select input: ");
for(ctr2=1;ctr2
{

scanf("%d",&strobe[ctr2]);
check_invalid_strobe(ctr2);

}
printf("Only first %d select inputs considered.rest
rejected(ifany)",num-1);
printf("Press any key to continue....");
getch();

}

void display_output()
{

printf("Output is : %d",output);
printf("Input %d is selected as output.",decieq);
printf("Press any key to continue.....");
getch();

}

void display_complex_output()
{

output = inputs[decieq];
printf("Output is: %d",output);
printf("Input %d is selected as output.",decieq);
printf("Press any key to continue....");
getch();

}

void two_is_to_one()
{

take_input(1);
message(1);

decieq = strobe[0]*1;

decide_output(count);
display_output();

}

void four_is_to_one()
{

int ctr2;

take_input(2);
message(2);

decieq = strobe[0]*2 + strobe[1]*1;

decide_output(count);
display_output();

}

void eight_is_to_one()
{

int ctr2;

take_input(3);
message(3);

decieq = strobe[0]*4 + strobe[1]*2 + strobe[2]*1;

decide_output(count);
display_output();

}

void sixteen_is_to_one()
{

int ctr2;

take_input(4);
message(4);

decieq = strobe[0]*8 + strobe[1]*4 + strobe[2]*2 + strobe[3]*1;

decide_output(count);
display_output();

}

void thirtytwo_is_to_one()
{

int ctr2;

take_input(5);
message(5);

decieq = strobe[0]*16 + strobe[1]*8 + strobe[2]*4 + strobe[3]*2 +
strobe[4]*1;

decide_output(count);
display_output();

}

void complex_two_is_to_one()
{

int ctr2;

take_input_a(4);
take_complex_select_input(2);
message(1);
decide_complex_inputs(2);

decieq = strobe[1]*1;

clrscr();
display_complex_input(2);
display_complex_output();

}

void complex_four_is_to_one()
{

int ctr2;

take_input_a(8);
take_complex_select_input(3);
message(2);
decide_complex_inputs(4);

decieq = strobe[1]*2 + strobe[2]*1;

clrscr();
display_complex_input(4);
display_complex_output();

}

void complex_eight_is_to_one()
{

int ctr2;

take_input_a(16);
take_complex_select_input(4);
message(3);
decide_complex_inputs(8);

decieq = strobe[1]*4 + strobe[2]*2 + strobe[3]*1;

clrscr();
display_complex_input(8);
display_complex_output();

}

void complex_sixteen_is_to_one()
{

int ctr2;
take_input_a(32);
take_complex_select_input(5);
message(4);
decide_complex_inputs(16);

decieq = strobe[1]*8 + strobe[2]*4 + strobe[3]*2 + strobe[4]*1;

clrscr();
display_complex_input(16);
display_complex_output();

}

/**********MULTIPLEXING FUNCTIONS END*******************/

/****************CIRCUIT GUI****************************/


void common_gui(int num)
{

int y[32],i,i2,k;
double j=log(num)/log(2);
char str[2];
int stepsize=((280+num*3)-180)/(j+1);

for(i=0;i<32;i++)
y[i]=80 + (i+1)*10;

initgraph(&driver,&mode,"f: cgi");

outtextxy(20,5,"RED: logic level 1");
outtextxy(250,5,"Press any key to continue.....");
outtextxy(20,15,"GREEN:logic level 0");

for(i=0;i
{

if(check(i))
{

setcolor(WHITE);
circle(150,y[i],2);
setfillstyle(SOLID_FILL,RED);
floodfill(150,y[i],WHITE);

}

else
{

setcolor(15);
circle(150,y[i],2);
setfillstyle(SOLID_FILL,GREEN);
floodfill(150,y[i],WHITE);

}

line(154,y[i],180,y[i]);

}

rectangle(180,40,280+num*3,y[i-1]+40);
line(280+num*3,(y[i-1]-40)/2 + 60,280+3*num+40,(y[i-1]-40)/2 + 60);

if(output==1)
{

setcolor(15);
circle(280+num*3+44,(y[i-1]-40)/2 + 60,4);
setfillstyle(SOLID_FILL,RED);
floodfill(280+num*3+44,(y[i-1]-40)/2 + 60,WHITE);

}

else
{

setcolor(15);
circle(280+num*3+44,(y[i-1]-40)/2 + 60,4);
setfillstyle(SOLID_FILL,GREEN);
floodfill(280+num*3+44,(y[i-1]-40)/2 +60,WHITE);

}
outtextxy(280+num*3+50,(y[i-1]-40)/2 + 60," Y(Output = )");
itoa(output,str,10);
outtextxy(280+num*3+150,(y[i-1]-40)/2 + 60,str);
itoa(num,str,10);

outtextxy(140,y[i-1]+72," Block Diagram of ");
outtextxy(290,y[i-1]+72,str);
outtextxy(293,y[i-1]+72," : 1 Mux");

outtextxy((280+num*3-180)/2 + 150,y[(i-1)/2],str);
outtextxy((280+num*3-180)/2 + 160,y[(i-1)/2]," : 1");
outtextxy((280+num*3-180)/2 + 150,y[(i-1)/2+2]," MUX");

outtextxy(280+num*3+170,(y[i-1]-40)/2 + 60,"(I ");
itoa(decieq,str,10);
outtextxy(280+num*3+185,(y[i-1]-40)/2 + 60,str);
outtextxy(280+num*3+192,(y[i-1]-40)/2 + 60," )");

outtextxy(130,y[0]-5,"I0");
itoa(num-1,str,10);
outtextxy(126,y[i-1],"I");
outtextxy(133,y[i-1],str);

for(i2=0;i2
for(k=0;k<20;k++)
{
line(180+(i2+1)*stepsize,y[i-1]+40,180+(i2+1)*stepsize,y[i-1]+60);
if(strobe[i2]==0)
{

setcolor(15);
circle(180+(i2+1)*stepsize,y[i-1]+62,2);
setfillstyle(SOLID_FILL,2);
floodfill(180+(i2+1)*stepsize,y[i-1]+62,15);

}

else
{

setcolor(15);
circle(180+(i2+1)*stepsize,y[i-1]+62,2);
setfillstyle(SOLID_FILL,4);
floodfill(180+(i2+1)*stepsize,y[i-1]+62,15);

}

}

outtextxy(185+(i2)*stepsize,y[i-1]+62,"S0");
itoa(j-1,str,10);
outtextxy(162+stepsize,y[i-1]+62,"S ");
outtextxy(170+stepsize,y[i-1]+62,str);

if(num==2)
{
setcolor(0);
outtextxy(162+stepsize,y[i-1]+62,"S0");
setcolor(15);

}

getch();

}

void common_complex_gui(int num)
{

int y[32],i,i2,k;
double j=log(num)/log(2);
char str[2];
int stepsize=((280+num*3)-180)/(j+1);

for(i=0;i<32;i++)
y[i]=80 + (i+1)*10;

initgraph(&driver,&mode,"f: cgi");

outtextxy(20,5,"RED: logic level 1");
outtextxy(250,5,"Press anu key to continue.....");
outtextxy(20,15,"GREEN:logic level zero");

for(i=0;i
{

if(inputs[i]==1)
{

setcolor(WHITE);
circle(150,y[i],2);
setfillstyle(SOLID_FILL,RED);
floodfill(150,y[i],WHITE);

}

else
{

setcolor(15);
circle(150,y[i],2);
setfillstyle(SOLID_FILL,GREEN);
floodfill(150,y[i],WHITE);

}

line(154,y[i],180,y[i]);

}

rectangle(180,40,280+num*3,y[i-1]+40);
line(280+num*3,(y[i-1]-40)/2 + 60,280+3*num+40,(y[i-1]-40)/2 + 60);

if(output==1)
{

setcolor(15);
circle(280+num*3+44,(y[i-1]-40)/2 + 60,4);
setfillstyle(SOLID_FILL,RED);
floodfill(280+num*3+44,(y[i-1]-40)/2 + 60,WHITE);

}

else
{

setcolor(15);
circle(280+num*3+44,(y[i-1]-40)/2 + 60,4);
setfillstyle(SOLID_FILL,GREEN);
floodfill(280+num*3+44,(y[i-1]-40)/2 +60,WHITE);

}

outtextxy(280+num*3+50,(y[i-1]-40)/2 + 60," Y(Output = )");
itoa(output,str,10);
outtextxy(280+num*3+150,(y[i-1]-40)/2 + 60,str);
itoa(num,str,10);

outtextxy(140,y[i-1]+72," Block Diagram of ");
outtextxy(290,y[i-1]+72,str);
outtextxy(293,y[i-1]+72," : 1 Mux");

outtextxy((280+num*3-180)/2 + 150,y[(i-1)/2],str);
outtextxy((280+num*3-180)/2 + 160,y[(i-1)/2]," : 1");
outtextxy((280+num*3-180)/2 + 150,y[(i-1)/2+2]," MUX");

outtextxy(280+num*3+170,(y[i-1]-40)/2 + 60,"(I ");
itoa(decieq,str,10);
outtextxy(280+num*3+185,(y[i-1]-40)/2 + 60,str);
outtextxy(280+num*3+192,(y[i-1]-40)/2 + 60," )");

outtextxy(130,y[0]-5,"I0");
itoa(num-1,str,10);
outtextxy(126,y[i-1],"I");
outtextxy(133,y[i-1],str);

for(i2=1;i2<=j;i2++)
for(k=0;k<20;k++)
{
line(180+(i2)*stepsize,y[i-1]+40,180+(i2)*stepsize,y[i-1]+60);
if(strobe[i2]==0)
{

setcolor(15);
circle(180+(i2)*stepsize,y[i-1]+62,2);
setfillstyle(SOLID_FILL,2);
floodfill(180+(i2)*stepsize,y[i-1]+62,15);

}

else
{

setcolor(15);
circle(180+(i2)*stepsize,y[i-1]+62,2);
setfillstyle(SOLID_FILL,4);
floodfill(180+(i2)*stepsize,y[i-1]+62,15);

}

}

outtextxy(185+(i2-1)*stepsize,y[i-1]+62,"S0");
itoa(j-1,str,10);
outtextxy(162+stepsize,y[i-1]+62,"S ");
outtextxy(170+stepsize,y[i-1]+62,str);

if(num==2)
{
setcolor(0);
outtextxy(162+stepsize,y[i-1]+62,"S0");
setcolor(15);

}

getch();

}

/************CIRCUIT GUI ENDS**********************/

/********************MAIN FUNCTION*****************/

void main()
{

int ctr=0;

welcome();

clrscr();
printf("Prog to implement a function using a multiplexer logic:-");

printf("Enter the function in minterms only.");
printf("Enter the function in form a b c d where a
printf("Press enter or provide spaces after each input.");
printf("Any value in input function cannot be repeated.");
printf("Enter the select input in form of 1's & 0's.");
printf("All the entered inputs are assumed to be high.");
printf("Maximun value of the minterm that can be accepted :31");

textcolor(4+BLINK);
textbackground(3);
cprintf("Press any key to continue.......");
textcolor(15);
textbackground(0);

getch();
printf("How many input will you provide: ");
scanf("%d",&count);

if(count>32)
{

printf("Invalid function input.inputs can't be greater than 32");
exit(1);

}

clrscr();
printf("Enter the function to be implemented : ");
for(ctr=0;ctr
{

scanf("%d",&function[ctr]);

if(function[ctr]>31)
{

printf("Invalid function input");
exit(1);

}

}

printf("Only first %d inputs considered.rest rejected(if any)",count);

fflush(stdin);

if(function[count-1] <= 1)
{
two_is_to_one();
common_gui(2);

}

else if(function[count-1] <= 3)
{

label1:

printf("Implement using:-1.)4:1 Mux2.)2:1 MuxEnter your choice?");
scanf("%d",&choice);

switch(choice)
{

case 1: four_is_to_one();
common_gui(4);
break;

case 2: complex_two_is_to_one();
common_complex_gui(2);
break;

default : printf("Invalid choice");
goto label1;

}

}

else if(function[count-1] <= 7)
{

label2:

printf("Implement using:-1.)8:1 Mux 2.)4:1 Mux Enter your choice?");
scanf("%d",&choice);

switch(choice)
{

case 1: eight_is_to_one();
common_gui(8);
break;

case 2: complex_four_is_to_one();
common_complex_gui(4);
break;

default : printf("Invalid choice");
goto label2;

}

}

else if(function[count-1] <= 15)
{

label3:

printf("Implement using:-1.)16:1 Mux2.)8:1 MuxEnter your choice?");
scanf("%d",&choice);

switch(choice)
{

case 1: sixteen_is_to_one();
common_gui(16);
break;

case 2: complex_eight_is_to_one();
common_complex_gui(8);
break;

default : printf("Invalid choice");
goto label3;

}

}

else if(function[count-1] <= 31)
{

label4:

printf("Implement using:-1.)32:1 Mux2.)16:1 MuxEnter your choice?");
scanf("%d",&choice);

switch(choice)
{

case 1: thirtytwo_is_to_one();
common_gui(32);
break;

case 2: complex_sixteen_is_to_one();
common_complex_gui(16);
break;

default : printf("Invalid choice");
goto label4;

}

}

getch();

}

/**************MAIN FUNCTION
ENDS****************************************/

Monday, November 2, 2009

PROGRAM FOR IMPLEMENTING IO STREAMS IN C++

#include"iostream.h"
#include"conio.h"
#include"math.h"
int main()
{
int count=0;
char c;
char name[20];
clrscr();
cout<< "\n\n-------------Get() & put() Function--------\n\n";
cout<< "INPUT Text\n";
cin.get(c);
while(c!='\n')
{
cout.put(c);
count++;
cin.get(c);
}
cout<< "\n\n No. of characters="<< count <<"\n";

cout<<"\n\n-------Getline() & Write Function()-------\n\n";
cout<<"Enter name\n";

cin.getline(name,10);
cout<<"Printing name with Write() size 5 characters \n\n";
cout.write(name,5);

cout<< "\n\n------------Width() Dunction--------\n\n";
cout<< "ITEMS";
cout.width(8);
cout<< "COST";
cout.width(15);
cout<<"Total Value"<<"\n";

cout<<"\n\n-------Precision() Function--------\n";
cout.precision(5);
cout<< sqrt(3) << "\n";

cout<< "\n\n------Fill() Function \n";
cout.fill('$');
cout.width(15);
cout<< "SAI";

getch();
return 0;
}

TEMPLATES FOR CONSTRUCTORS...VECTOR PROGRAM



include
#include
const size=3;
template <>
class vector
{
T *v;
public:
vector()
{
v=new T[size];
for(int i=0;i < i="0;iv[i]*y.v[i];
return sum;
}};
int main()
{
clrscr();
float x[3]={1.1,2.2,3.3};
float y[3]={4.4,5.5,6.6};
vector <> v1;
vector <> v2;
v1=x;
v2=y;
float r=v1*v2;
cout<<"r="<<<"\n";
getch();
return 0;
}

//virtual functions usin c++

#include"iostream.h"
class Base
{
public:
void display()
{
cout<<"\n Display base";
}
virtual void show()
{
cout<<"\n show base";
}
};
class Derived : public Base
{
public:
void display()
{
cout<<"\n Display derived";
}
void show()
{
cout<<"\n show derived";
}
};
int main()
{
Base B;
Derived D;
Base *bptr;
cout<<"\n bptr points to Base \n";
bptr=&B;
bptr->display(); //calls Base version
bptr->show(); //calls Base version

cout<<"\n\n bptr points to Derived \n";
bptr=&D;
bptr->display(); //calls Base version
bptr->show(); //calls Derived version

return 0;
}

// function overloading usin c++

#include"iostream.h"

//Declarations (prototypes)

int volume(int);
double volume(double, int);
long volume(long, int, int);

int main()
{
cout<< volume(10) <<"\n";
cout<< volume(2.5,8) <<"\n";
cout<< volume(100L,75,15) <<"\n";

return 0;
}

//Function definitions

int volume(int s) //cube
{
return(s*s*s);
}
double volume(double r, int h) //cylinder
{
return(3.14519*r*h);
}
long volume(long l, int b, int h) //rectangular box
{
return(l*b*h);
}

// Virtual Base class

#include"iostream.h"

class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number=a;
}
void put_number(void)
{
cout<<"Roll No:"< }
};

class test:virtual public student
{
protected:
float part1,part2;
public:
void get_marks(float x, float y)
{
part1=x;
part2=y;
}
void put_marks(void)
{
cout<<"Marks obtained:"<<"\n"<<"Part1="< }
};

class sports : public virtual student
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"Sports wt:"< }
};

class result:public test, public sports
{
float total;
public:
void display(void);
};

void result::display(void)
{
total=part1+part2+score;

put_number();
put_marks();
put_score();

cout<<"Total Score:"<}

int main()
{
result student_1;
student_1.get_number(678);
student_1.get_marks(30.5,25.5);
student_1.get_score(7.0);
student_1.display();
return 0;
}

//Runtime polymorphism c++

#include"iostream.h"
#include"string.h"
#include"conio.h"
class media
{
protected:
char title[50];
float price;
public:
media(char *s,float a)
{
strcpy(title,s);
price=a;
}
virtual void display() { } //empty vitual function
};

class book:public media
{
int pages;
public:
book(char *s, float a, int p):media(s,a)
{
pages=p;
}
void display();
};
class tape:public media
{
float time;
public:
tape(char *s, float a, float t):media(s,a)
{
time=t;
}
void display();
};
void book::display()
{
cout<<"\n Title:"< cout<<"\n Pages:"< cout<<"\n Price:"<}
void tape::display()
{
cout<<"\n Title:"< cout<<"\n Play time:"< cout<<"\n price:"<}
int main()
{
char *title=new char[30];
float price, time;
int pages;

clrscr();

//Books details
cout<<"\n Enter Book Details\n";
cout<<"Title:";
cin>>title;
cout<<"Price :";
cin>>price;
cout<<"Pages:";
cin>>pages;

book book1(title,price,pages);

//tape Details
cout<<"\n Enter Tape Details\n";
cout<<"Title:";
cin>>title;
cout<<"Price:";
cin>>price;
cout<<"Play time (mins):";
cin>>time;

tape tape1(title,price,time);

media*list[2];
list[0]=&book1;
list[1]=&tape1;
cout<<"\n Media Details:";
cout<<"\n.........BOOK........";
list[0]->display();

cout<<"\n.....TAPE.....";
list[1]->display();
getch();
return 0;
}

// constructos in Derived class c++


#include"iostream.h"

class alpha
{
int x;
public:
alpha(int i)
{
x=i;
cout<<"alpha initialized \n";
}
void show_x(void)
{
cout<<"x="< }
};

class beta
{
float y;
public:
beta(float j)
{
y=j;
cout<<"beta initialized \n";
}
void show_y(void)
{
cout<<"y="< }
};

class gamma:public beta,public alpha
{
int m,n;
public:
gamma(int a, float b, int c, int d):
alpha(a),beta(b)
{
m=c;
n=d;
cout<<"gamma initialized \n";
}
void show_mn(void)
{
cout<<"m="< }
};

int main()
{
gamma g(5,10.75,20,30);
cout<<"\n";
g.show_x();
g.show_y();
g.show_mn();
return 0;
}

//OverLoading Unary Minus c++


#include"iostream.h"
class space
{
int x;
int y;
int z;
public:
void getdata(int a, int b, int c);
void display(void);
void operator-(); //overloaded unary minus
};

void space::getdata(int a, int b, int c)
{
x=a;
y=b;
z=c;
}
void space::display(void)
{
cout< cout< cout<}
void space:: operator-()
{
x=-x;
y=-y;
z=-z;
}
int main()
{
space S;
S.getdata(10,-20, 30);
cout<<"S:";
S.display();
-S; //activates operator-() function

cout<<"S:";
S.display();
return 0;
}

//static member function usin c++


#include"iostream.h"

class test
{
int code;
static int count; //static member variable

public:

void setcode(void)
{
code=++count;
}
void showcode(void)
{
cout<<"object number:"< }
static void showcount(void) //static member funcion
{
cout<<"count:"< }
};

int test::count;

int main()
{
test t1,t2;
t1.setcode();
t2.setcode();

test::showcount(); //accessing static function

test t3;
t3.setcode();

test::showcount();

t1.showcode();
t2.showcode();
t3.showcode();

return 0;
}

// Objects as Arguments usin c++


#include "iostream.h"
#include"conio.h"

class time
{
int hours;
int minutes;
public:
void gettime(int h, int m)
{
hours=h;
minutes=m;
}
void puttime(void)
{
cout< cout< }

void sum(time,time); //declaration with objects as arguments
};

void time::sum(time t1, time t2) //t1,t2 are objects
{
minutes=t1.minutes+t2.minutes;
hours=minutes/60;
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
}

int main()
{
time T1,T2,T3;

T1.gettime(2,45); //get T1

T2.gettime(3,40); //get T2

T3.sum(T1,T2); //T3=T1+T2

cout<<"T1=";
T1.puttime(); //display T1

cout<<"T2=";
T2.puttime(); //display T2

cout<<"T3=";
T3.puttime(); //display T3

getch();

return 0;
}

friend function using c++

#include"iostream.h"

class sample
{
int a;
int b;
public:
void setvalue() {a=50; b=40; }

friend float mean(sample s);
};


float mean(sample s)
{
return float(s.a+s.b)/2.0;
}


int main()
{
sample X; //object X
X.setvalue();

cout<< "Mean value="<
return 0;
}

// OverLoading + Operator c++

#include"iostream.h"
#include"conio.h"
class complex
{
float x; //real part
float y; // imaginarypart
public:
complex(){ } //constructor 1
complex(float real, float imag) //constructor 2
{
x=real;
y=imag;
}
complex operator+(complex);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp; //temporary
temp.x=x+c.x;
temp.y=y+c.y; // these are float additions
return(temp);
}
void complex::display(void)
{
cout<}
int main()
{
complex C1,C2,C3;
clrscr(); //invokes constructor 1
C1=complex(2.5, 3.5);
C2=complex(1.6,2.7); //invokes constructor 2
C3=C1+C2;
cout<<"C1="; C1.display();
cout<<"C2="; C2.display();
cout<<"C3'"; C3.display();
return 0;
}

Arrays of Objects usin c++


#include "iostream.h"
#include"conio.h"

class employee
{
char name[30]; //string as class member
float age;
public:
void getdata(void);
void putdata(void);
};

void employee::getdata(void)
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter age:";
cin>>age;
}

void employee::putdata(void)
{
cout<<"Name:"< cout<<"Age:"<
}
const int size=3;

int main()
{
employee manager[size];
clrscr();
for(int i=0;i {
cout<<"\n Details of manager"< manager[i].getdata();
}

cout<<"\n";

for(i=0;i {
cout<<"\n Manager"< manager[i].putdata();
}

getch();

return 0;

}

Thursday, October 22, 2009

//stacks using Arrays

#include "iostream.h"
#include "conio.h"
const int SIZE = 10;

template
class stack
{
StackType stck[SIZE];
int tos;

public:
stack() {
tos = 0;
}
void push(StackType ob)
{
if(tos==SIZE) {
cout << "Stack is full.\n";
return;
}
stck[tos] = ob;
tos++;
}

StackType pop()
{
if(tos==0) {
cout << "Stack is empty.\n";
return 0; // return null on empty stack
}
tos--;
return stck[tos];
}
};

int main()
{
int i,j;
stack s1, s2;
clrscr();

s1.push('a');
s2.push('x');
s1.push('b');
s2.push('y');
s1.push('c');
s2.push('z');

for(i=0; i<3; i++)
cout << "Pop s1: " << s1.pop() << "\n";
for(j=0; j<3; j++)
cout << "Pop s2: " << s2.pop() << "\n";

stack ds1, ds2;

ds1.push(1.1);
ds2.push(2.2);
ds1.push(3.3);
ds2.push(4.4);
ds1.push(5.5);
ds2.push(6.6);

for(i=0; i<3; i++)
cout << "Pop ds1: " << ds1.pop() << "\n";
for(j=0; j<3; j++)
cout << "Pop ds2: " << ds2.pop() << "\n";
getch();
return 0;
}

// Insertion & Deletion of element in Queue (using Array)

#include "iostream.h"
#include "conio.h"
#include "process.h"


int ins(int[], int);
int del(int[]);

void display(int[], int, int);

const int size=20;

int q[size], f=-1, r=-1;

int main()
{
int item, res;
char ch='y';
clrscr();
while(ch=='y' ||ch=='Y')
{
cout<<"\nEnter Item for insertion: ";
cin>>item;
res=ins(q,item);
if (res==-1)
{
cout<<"\nOver flow\n ";
exit(1);
}
cout<<"\nQueue: ";
display(q, f, r);
cout <<"\nInsert more Y/N :- ";
ch=getch();
}

cout << "\n\nDeletion of element begins... \n";
ch='y';

while (ch=='y'||ch=='Y')
{
res=del(q);
if(res==-1)
{
cout<< "\nUnder flow";
exit(1);
}
else
{
cout<<"\nElement deleted is : "< cout<<"\nQueue: ";
display(q, f, r);
}
cout<<"\nWant to delete more element Y/N -";
cin >> ch;
if(ch=='n'||ch=='N')
cout<<"\nQueue: ";
display(q,f,r);
}
getch();
return 0;
}


int ins(int q[], int ele)
{
if (r==size-1)
return -1;
else if (r==-1)
{
f=r=0;
q[r]=ele;
}
else
{
r++;
q[r]=ele;
}
return 0;
}


int del (int q[])
{
int ret;
if(f==-1)
return -1;
else
{
ret = q[f];
if (f==r)
f=r=-1;
else
f++;
}
return ret;
}


void display(int q[], int f, int r)
{
if (f== -1) return;
cout<< "F-";
for(int i = f;i<=r;i++)
cout< cout<<"R";
}



/* Output::::::::::::::::::::

Enter Item for insertion: 10
Queue: F-10-R
Insert more Y/N :-

Enter Item for insertion: 20
Queue: F-10-20-R
Insert more Y/N :-

Enter Item for insertion: 30
Queue: F-10-20-30-R
Insert more Y/N :-

Deletion of element begins...
Element deleted is : 10
Queue: F-20-30-R
Want to delete more element Y/N -

*/

priority queue

#include "iostream.h"
#include "conio.h"
#include "iomanip.h"
#define MAX 10
class pqueue
{
int pq[MAX];
int mh;
public:
pqueue();

int size(void){ return mh;}
int isempty(void) {
if(mh == 0)
return 1;
return 0;}
int pop(void);
void push(int);
void display(void);
int top(void){
if(!isempty()) return pq[1];
else cout<<"\n PQ empty"; return 0;}
};
pqueue::pqueue()
{
for(int i = 0; i < MAX; i++,pq[i] = 0);
mh = 0;
}
void pqueue::push(int ele) {
int cpos = ++mh;
while(cpos != 1 && pq[cpos/2] < ele) {
pq[cpos] = pq[cpos/2];
cpos /= 2; }
pq[cpos] = ele;

}
int pqueue::pop(void) {
if(!isempty()) {
int le = pq[mh--];
pq[0] = pq[1];
int cnode = 1;
int child = 2;
while(child<= mh) {
if(child < mh && pq[child] child++;
if(le >= pq[child])
break;
pq[cnode] = pq[child];
cnode = child;
child *= 2;
}
pq[cnode] = le;
return pq[0];
}
else {
cout<<"\n Queue is Empty ";
return 0;
}

}

void pqueue::display(void) {
if(!isempty()) {
cout<<"\n The elements in the queue :"< for(int i = 1; i <= mh; i++)
cout< else cout<<"\n Queue is empty";
}
int menu(void) {
cout<<"\n\tMENU"< cout<<"\n 1 -> push \n 2 -> pop \n ";
cout<<"3 ->isempty \n 4 -> Display";
cout<<"\n 5 ->topelement \n 6 -> size \n ";
cout<<"\n 7 -> Heap Sort \n 8 -> exit ";
cout<<"\n Enter your choice :";
int ch;
cin>>ch;
return ch;
}
void heapsort(void) {
int n,ele;
pqueue q;
cout<<"\n Enter the no.of elements :";
cin>>n;
cout<<"\n Enter the elements : \n";
for(int i = 0; i < n; i++) {
cin>>ele;
q.push(ele);
}
cout<<"\n The elements in the sorted order is :\n";
for(i = 0 ; i < n; i ++)
cout<}
void main() {
int ch,ele;
pqueue p;
do {
clrscr();
ch = menu();
switch(ch){
case 1: cout<<"\n Enter the element to push : ";
cin>>ele;
p.push(ele); break;
case 2: ele = p.pop();
if(ele)
cout<<"\n The popped element is :"< break;
case 3: if(p.isempty())
cout<<"\n queue is empty" ;
else
cout<<"\n queue is not empty ";
break;
case 4: p.display(); break;
case 5: ele = p.top();
cout<<"\n The top element is :"< break;
case 6: cout<<"\n No.of elements is :"< break;
case 7: heapsort(); break;
case 8: break;
Default:cout<<"\n Invalid Choice. Try again ";
} getch();

}while (ch != 8);
}

// Binary Search Tree Program

#include "iostream.h"
#include "conio.h"
#include "iomanip.h"
class bnode
{
public:
bnode *left;
bnode *right;
int data;
bnode(){}
bnode(int e)
{
left = NULL;
right = NULL;
data = e;
}

};
class bst
{
bnode *root;
public:
bst(){ root = NULL;}
int find(int);
int insert(int);
int remove(int);
void display(void);
void inorder(bnode*);

};

int bst::find(int ele)
{
bnode *p=root;
while(p)
{
if(p->data == ele) return 1;
else if(p->data < p =" p"> right;
else p = p -> left;
} return 0;
}
int bst::insert(int ele)
{
bnode *p = root;
bnode *q = new bnode(ele);
bnode *pp;
if(root)
{
while(p)
{
pp = p;
if(p -> data < p =" p"> right;
else if(p -> data > ele) p = p -> left;
else return 1;
}
if(pp -> data < ele)
pp -> right = q;
else pp -> left = q;
}
else root = q;

return 0;
}

int bst::remove(int ele){
bnode *p = root; bnode *pp = NULL;
while(p && p ->data != ele){
pp = p;
if(p -> data > ele) p = p -> left;
else p = p -> right;
}if(!p) return 1;
if(p -> left && p -> right) {
bnode *s = p -> left;
bnode *ps = p;
while(s -> right) {
ps = s; s = s -> right;
}bnode *q = new bnode(s->data);
q -> left = p -> left;
q -> right = p ->right;
if(pp == NULL) root = q;
else if(pp -> left == p)
pp -> left = q;
else pp -> right = q;
if(ps == p) pp = q;
else pp = ps;
p = s;
}bnode *c;
if(p -> left) c = p -> left;
else c = p ->right;
if( p == root) root = c;
else {
if(p == pp -> left)
pp -> left = c;
else
pp -> right = c;
} delete p; return 0; }


void bst::display(void)
{
inorder(root);
}
void bst::inorder(bnode* p)
{
if(p)
{
inorder(p -> left);
cout< data;
inorder(p -> right);
}

}

int menu(void)
{
cout<<"\n Binary Search Tree Menu \n";
cout<<"\n 1 -> insert\n 2 -> Delete \n 3 -> find ";
cout<<"\n 4 -> display \n 5 -> exit ";
cout<<"\n Enter your choice :";
int ch;
cin>>ch;
return ch;

}

void main()
{
bst b; int ch,ele;
do{
clrscr();
ch = menu();
switch(ch) {
case 1: cout<<"\n Enter the element to insert :";
cin>>ele;
if(b.insert(ele))
cout<<"\n Duplicate element ";
break;
case 2: cout<<"\n Enter the element to remove :";
cin>>ele;
if(b.remove(ele))
cout<<"\n Element not in the list:";
break;
case 3: cout<<"\n Enter the element to find:";
cin>>ele;
if(b.find(ele))
cout<<"\n Element found";
else
cout<<"\n Element not found :";
break;
case 4: cout<<"\n The elements in the list :";
b.display();
break;
case 5: break;
default:
cout<<"\n Invalid choice . Try again"; } getch(); }while(ch != 5);
}


Monday, May 11, 2009

C-PROGRAM FOR BINARY SEARCH USING RECURSIVE FUNCTION

#include
#include
int key;
void main()
{
int a[50],i,n,loc;
int bin();
printf("\nenter the size of the array");
scanf("%d",&n);
printf("%d\n",n);
printf("\narray elements?\n\n");
for(i=0;i
scanf("%d",&a[i]);
for(i=0;i
printf("%5d",a[i]);
printf("\n");
printf("\nelement to be searched");
scanf("%d",&key);
printf("\n%d",key);
loc=bin(a,0,n);
printf("\n\n");
if(loc==0)
printf("\nunsuccessful search %d not found\n",key);
else
{
printf("\nsuccessful search");
printf("\n%d found at position %d \n",key,loc);
}
getch();
}

C-PROGRAM FOR BINARY SEARCH

#include
#include
void main()
{
int a[100],i,n,low,high,mid,term,flag=1;
printf("\nnumber of elements in the array?");
scanf("%d",&n);
printf("%d",n);
printf("\n\nenter elements in ascending order:\n");
for(i=0;i<=high) { mid=(low+high)/2; if(terma[mid])
low=mid+1;
else
if(term==a[mid])
{
printf("\nsearch successful");
printf("\n%d found at location %d\n",term,mid+1);
flag=0;
break;
}
}
if(flag==1)
printf("\nsearch unsuccessful");
getch();
}

C-PROGRAM FOR QUICK SORT

#include
void quick(int a[],int,int);
void main()
{
int a[20],i,j,n,l,u;
printf("\nenter size of the array");
scanf("%d",&n);
printf("\nenter elements of the array");
for(i=0;ip)
j--;
if(i<=j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
temp=a[j];
a[j]=a[l];
a[l]=temp;
}
quick(a,l,j-1);
quick(a,j+1,u);
}
}
}

C-PROGRAM FOR MERGE SORT

#include
#include
void main(void)
{
int i,j,k,l,m,n,a[20],b[20],c[20];
printf("\nenter the array1 size");
scanf("%d",&m);
printf("\nenter %d elements",m);
for(i=0;i<=m;i++)
scanf("%d",&a[i]);
sort_array(a,m-1);
printf("\nsorted arra1:");
for(i=0;i<=m;i++)
printf("\t%d",a[i]);
printf("\nenter the array2 size:");
scanf("%d",&n);
printf("\nenter %d elements:",n);
for(i=0;i<=n;i++)
printf("\t%d",&b[i]);
sort_array(b,n-1);
printf("\nsorted array 2:");
for(i=0;i<=n;i++)
printf("\t%d",b[i]);
i=j=k=0;
while((i if(a[i] < b[j])
{
c[k]=a[i];
i++;
k++;
}
else
{
if(a[i]>b[j])
{
c[k]=b[j];
j++;
k++;
}
else
{
c[k]=a[i];
i++;
j++;
k++;
}
}
if(i {
for(i=1;i {
c[k]=a[i];
i++;
k++;
}
}
else
if(j {
for(i=j;i {
c[k]=b[j];
j++;
k++;
}
}
printf("\nmerged array is:");
for(i=0;i printf("\t %d",c[i]);
}

void sort_array(int sa[20],int x)
{
int i,flag,temp;
flag=0;
while(flag==0)
{
flag=1;
for(i=0;i {
if(sa[i]>sa[i+1])
{
temp=sa[i];
sa[i]=sa[i+1];
sa[i+1]=temp;
flag=0;
}
}
}
}

C-PROGRAM FOR BUBBLE SORT

/*bubble sort*/
#include
#include
void main()
{
int i,j,k,n,flag,limit,a[50];
printf("number of array elements?");
scanf("%d",&n);
printf("%d",n);
printf("\nenter array elements");
for(i=0;ia[j+1])
{
k=a[j];
a[j]=a[j+1];
a[j+1]=k;
flag=0;
}
}
if(flag)
break;
else
flag=1;
}
printf("\nsorted array:\n");
for(i=0;i
printf("\n%5d",a[i]);
printf("\n");
getch();
}

Saturday, May 9, 2009

BEST WEBSITES USEFULL TO EVERY ONE

http://ecmrocks.synthasite.com/resources/Best_Websites_list.xls

PROGRAM FOR IMPLEMENTING QUEUE USING LINKED LIST

#include
#include
#include
#define NULL 0
struct queue
{
int info;
struct queue *next;
};
typedef struct queue node;
node *first,*list,*front,*rear;
void add();
void del();
void print();
void main()
{
int ch;
front=rear=NULL;
do
{
printf("\n 1->add");
printf("\n 2->del");
printf("\n 3->exit");
printf("\n Enter choice");
scanf("%d",&ch);
switch(ch)
{
case 1: add();
print();
break;
case 2: del();
print();
break;
case 3: exit(0);
break;
default: printf("Invalid choice");
}
}while(ch!=3);
}
void add()
{
first=(node *)malloc(sizeof(node));
printf("Enter element");
scanf("%d",&first->info);
first->next=NULL;
if(rear==NULL)
{
rear=first;
front=first;
}
else
rear->next=first;
rear=first;
}

void del()
{
if(front==NULL)
{
printf("\nList is empty\n");
getch();
}
else
{
printf("The popped element is %d \n ",front->info);
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
front=front->next;
}
}

void print()
{
list=front;
if(list==NULL)
{
printf("\nList is empty\n");
}
else
{
printf("\nfront->");
while(list!=rear)
{
printf("%d->",list->info);
list=list->next;
}
printf("%d->",list->info);
printf("rear\n");
}
}

PROGRAM FOR IMPLEMENTING STACK USING LINKED LIST

#include
#include
#include
#define NULL 0
struct stack
{
int info;
struct stack *next;
};
typedef struct stack node;
node *first,*list,*root;
void push();
void pop();
void print();
void main()
{
int ch;
root=NULL;
do
{
printf("\n 1->push");
printf("\n 2->pop");
printf("\n 3->print");
printf("\n 4->exit");
printf("\n Enter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: print();
break;
case 4: exit(0);
break;
}
}while(ch!=4);
}
void push()
{
first=(node *)malloc(sizeof(node));
printf("Enter element");
scanf("%d",&first->info);
first->next=root;
root=first;
}
void pop()
{
if(root==NULL)
printf("\n List is empty\n");
else
{
printf("The popped element is %d\n",root->info);
root=root->next;
}
}
void print()
{
list=root;
if(list==NULL)
printf("\nList is empty\n");
else
{
printf("\n The stack elements are");
while(list!=NULL)
{
printf("\n %d",list->info);
list=list->next;
}
}
}

PROGRAM FOR IMPLEMENTING CIRCULAR LINKED LIST

#include
#include
#include
struct cir
{
int info;
struct cir *next;
};
typedef struct cir node;
node *first,*temp,*new1,*list,*prev,*cur,*p;
void creat()
{
int ele;
printf("Type -1 to stop");
printf("\n Enter ele \t");
scanf("%d",&ele);
while(ele!=-1)
{
new1=(node *)malloc(sizeof(node));
new1->info=ele;
if(first==NULL)
{
new1->next=new1;
first=new1;
}
else
{
new1->next=first->next;
first->next=new1;
}
printf("enter ele\t");
scanf("%d",&ele);
}
}
void count()
{
int c=0;
temp=first;
first=first->next;
while(first!=temp)
{
++c;
first=first->next;
}
++c;
printf("No of nodes in circular list=%d",c);
}
void print()
{
node *start;
temp=first;
start=first->next;
printf("the circulat list is");
while(start!=temp)
{
printf("%d->",start->info);
start=start->next;
}
printf("%d",start->info);
}

PROGRAM FOR IMPLEMENTING DOUBLE LINKED LIST

#include
#include
#include
#define NULL 0
typedef struct dlist
{
int info;
struct dlist *left;
struct dlist *right;
}node;
node *root,*first,*prev,*list,*start,*temp,*end,*list1,*last;
int ele;
void create()
{
printf("Type -1 to stop");
printf("\n Enter element");
scanf("%d",&ele);
while(ele!=-1)
{
first=(node *)malloc(sizeof(node));
first->info=ele;
first->left=NULL;
first->right=NULL;
if(root==NULL)
root=first;
else
{
prev->right=first;
first->left=prev;
}
prev=first;
end=first;
scanf("%d",&ele);
}
}
void print()
{
list=root;
list1=end;
if(root==NULL)
printf("\n List is empty");
else
{
printf("\n contents of list in forward direction\n");
printf("root->");
while(list!=NULL)
{
printf("%d->",list->info);
list=list->right;
}
printf("NULL\n");
}
if(end==NULL)
printf("\n List is empty");
else
{
printf("\n contents of list in backward direction\n");
printf("end->");
while(list1!=NULL)
{
printf("%d->",list1->info);
list1=list1->left;
}
printf("NULL\n");
}
}
void delet()
{
int item;
temp=start=root;
if(root==NULL)
printf("Empty list");
else
{
printf("Enter the element to be deleted\n");
scanf("%d",&item);
if(root->info==item)
{
root=root->right;
root->left=NULL;
}
else
{
while((item!=temp->info)&&(temp!=NULL))
{
start=temp;
temp=temp->right;
}
start->right=temp->right;
temp->right->left=start;
}
}
}
void main()
{
int ch;
root=NULL;
clrscr();
do
{
printf("\n 1->create");
printf("\n 2->deletion");
printf("\n 3->exit");
printf("Enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
print();
break;
case 2: delet();
print();
break;
case 3: exit(0);
break;
}
}while(ch!=3);
getch();
}

PROGRAM FOR IMPLEMENTING SINGLR LINKED LIST

#include
#include
#include
#define NULL 0
struct slist
{
int info;
struct slist *next;
};
typedef struct slist node;
node *root,*new1,*prev,*list,*start,*temp;
int ele;
void create()
{
printf("Type -999 to stop");
scanf("%d",&ele);
while(ele!=-999)
{
new1=(node *)malloc(sizeof(node));
new1->info=ele;
new1->next=NULL;
if(root==NULL)
root=new1;
else
prev->next=new1;
prev=new1;
scanf("%d",&ele);
}
}
void print()
{
list=root;
if(root==NULL)
printf("list is empty");
else
{
printf("\n contents of list");
printf("root->");
while(list!=NULL)
{
printf("%d->",list->info);
list=list->next;
}
printf("NULL");
}
}

void delet()
{
int item;
node *p;
printf("Enter the element to be deleted");
scanf("%d",&item);
if(root->info==item)
{
p=root;
root=root->next;
free(p);
}
else
{
temp=root;
while((item!=temp->info)&&(temp!=NULL))
{
prev=temp;
temp=temp->next;
}
if(item==temp->info)
{
prev->next=temp->next;
free(temp);
}
}
}



void main()
{
int ch;
root=NULL;
do
{
printf("\n 1->creation");
printf("\n 2->deletion");
printf("\n 3->exit");
printf("\n Enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
print();
break;
case 2: delet();
print();
break;
case 3: exit(0);
break;
default: printf("Invalid choice");
}
} while(ch!=3);
getch();
}

Wednesday, May 6, 2009

PROGRAM FOR EVELUATING SINGLE LINKED LIST

#include
#include
#include
#define NULL 0
struct slist
{
int info;
struct slist *next;
};
typedef struct slist node;
node *root,*nu,*prev,*list,*start,*temp;
int ele;
void create()
{
printf("Enter -1 to stop\n");
scanf("%d",&ele);
while(ele!=-1)
{
nu=(node*)malloc(sizeof(node));
nu->info=ele;
nu->next=NULL;
if(root==NULL)
{
root=nu;
prev=nu;
}
else
{
prev->next=nu;
prev=nu;
}
scanf("%d",&ele);
}
}
void display()
{
list=root;
if(root==NULL)
printf("The list is empty\n");
else
{
printf("\n Contents of list");
while(list!=NULL)
{
printf("%d,",list->info);
list=list->next;
}
printf("NULL\n");
}
}
void deletion()
{
int item;
node *p;
printf("Enter the element to be deleted\n");
scanf("%d",&item);
if(root->info==item)
{
p=root;
root=root->next;
free(p);
}
else
{
temp=root;
while((item!=temp->info)&&(temp!=NULL))
{
prev=temp;
temp=temp->next;
}
if(item==temp->info)
{
prev->next=temp->next;
free(temp);
}
}
}
void main()
{
int ch;
root=NULL;
clrscr();
do
{
printf("\n Press 1 for entering elements into list\n Press 2 to delete an element from the list\n Press 3 to exit\n");
printf(" Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
display();
break;
case 2:deletion();
display();
break;
case 3:exit(0);
break;
}
}while(ch!=3);
}