ENQUEUE

March 27, 2008

ENQUEUE

HOME

/*
PROGRAM FOR ENQUEUEING THE ELEMENTS  

        
USING SELF  REFERENTIAL CLASSES   */


#include”iostream.h”

class
queue //NAME OF THE
CLASS

{
private:  //DATA MEMBERS
  
 int data;

  
 queue *next;

public:       
 

  
 void enqueue(int);
//MEMBER FUNCTIONS

   
void display();

}
*head;  
//OBJECT ACTS AS NODE

//FUNCTION
DECLARATION

void
queue::enqueue(int ele)   

{
  
 queue *n,*temp;

   
n=new(queue);//MEMORY
ALLOCATION

  
 n->data=ele;

  
 n->next=NULL;

/*IF
THE LIST IS EMPTY THEN ADD 1st NODE AS HEADER NODE*/

       
if(head==NULL)  

  
     head=n;

  
 else

  
 {

  
 temp=head;

  
 while(temp->next!=NULL)

  
 temp=temp->next;

  
   temp->next=n;

  
 }

}

 //DISPLAY
THE RESULT

void
queue::display()         &

nbsp; 
 
{
queue 
*temp;

  
 

  
 if(head==NULL)

  
 cout<<“LIST IS EMPTY\n”;

  
 else

  
 {

  
 temp=head;

  
 while(temp!=NULL)

  
 {

  
         

  
 cout<<temp->data<<endl;

  
 temp=temp->next;

  
 }

  
 }

}

 //main()
FUNCTION STARTS

void
main()          &nbsp

;           
 
{
head=NULL;//INITIALIZATION OF HEADER
NODE

queue
l;  //OBJECT
OF THE CLASS List

  
 int x,a[20];

cout<<“ENTER
THE NO OF ELEMENTS TO BE ENQUEUED\n”;

  
 cin>>x;

  
cout<<“ENTER
“<<x<<“ELEMENTS\n”;

  
 for(int i=0;i<x;i++)

  
 {

  
     cin>>a[i];

/*PASING
THE ELEMENTS TO THE FUNCTION THROUGH OBJECT*/

  
 l.enqueue(a[i]);        &

nbsp;  
 
  
 }

  
 cout<<“THE LIST IS\n”;

  
 l.display();        &nbsp

;        
 
}

//PROGRAM
ENDS

/*
OUTPUT:

ENTER
THE NO OF ELEMENTS

     
TO BE ENQUEUED

5
ENTER
5ELEMENTS

34
56
78
12
30
THE
LIST IS

34
56
78
12
30
Press
any key to continue

*/

HOME

Advertisements

EVALUATION_POSTFIX

March 27, 2008

EVALUATIO OF POSTFIX EXPRESSION

HOME
/*….PROGRAM
TO FIND THE VALUE OF THE POSTFIX EXPRESSION.*/


#include<iostream.h>
#include<ctype.h>
#include<math.h>
#include<stdlib.h>
class
post//CLASS
DECLARATION

{
private:
  
 int res,x;

   
char data,*exp;

  
 post *next;

public:
  
 void evaluate(char *);

  
 void push(int);

   
int pop();

   
void show();

}
*top;

/*FUNCTION
FOR EVALUATION OF POSTFIX EXPRESSION*/

void
post::evaluate(char *c)

{
  
 exp=c;

  
 int n1,n2,res,i;

for(i=0;exp[i]!=”;i++)
  
 {

  
   if( isdigit(exp[i]) )

  
   {

  
   int item=exp[i]-‘0’;

  
   push(item);

  
    }

     
else

  
   {

  
 n1=pop();

  
 n2=pop();

  
     
     

       
switch(exp[i])

  
      {

   
     case ‘+’:

  
       
 res=n1+n2;

  
      
     break;

  
  case’-‘:

  
      res=n2-n1;

  
          
break;

  
     case ‘*’:

  
       
 res=n1*n2;

  
       
 break;

       
case ‘/’:

  
       
 res=n2/n1;

  
       
 break;

  
  case ‘$’:

  
       
 res=pow(n2,n1);

  
        
 break;

      
default:

  
cout<<“ENTER CORRECT EXPRESION”;

  
    exit(0);

  
      }  
 

    
push(res);

  
   }

  
 }

}
  
 

//FUNCTOIN
FOR PUSHING DATA

void
post::push(int item)

{
  
 post *n,*temp;

  
 n=new(post);

  
 n->data=item;

  
 n->next=NULL;

  
 if(top==NULL)

  
     top=n;

  
 else

  
 {

  
     temp=top;

  
     n->next=top;

  
     top=n;

  
 }

}

//FUNCTION
FOR SHOWING DATA

void
post::show()

  
 {

  
 cout<<“THE RESULT IS”;

  
 res=pop();

  
 cout<<res;  
     

  
 }

//FUNCTION
FOR POPPING DATA

int
post::pop()

{

  
 post *temp ;

  
 temp=top;

  
 x=temp->data;

  
 top=temp->next;

  
 return(x);

  
 delete temp;

 
 

}

void
main()//MAIN()
FUNCTION STARTS

{
  
 char s[22];

  
 top=NULL;

  
 post k;//OBJECT
CREATION

  
 cout<<“enter the POSTFIX expresion”;

  
 cin>>s;

 
    k.evaluate(s);

  
k.show();

}

/*
OUTPUT:
CASE
1:

enter
the POSTFIX

      
expresion 623+-382/+*2$3+

THE
RESULT IS 52

CASE
2:

enter
the POSTFIX

       
expresion 234*+

THE
RESULT IS 14

 Press
any key to continue

CASE
3:

enter
the POSTFIX expresion 12S+

 ENTER
CORRECT EXPRESION

 
Press any key to continue

*/

HOME

EVALUATION_PREFIX

March 27, 2008

EVALUATION OF PREFIX EXPRESSION

HOME
/*…PROGRAM
FOR EVALUATION OF PREFIX

 EXPRESION
SUING STACK AND SELF REFERENTIAL CALSSES..*/

#include<iostream.h>
#include<math.h>
#include<ctype.h>
#include<string.h>

//DECLARATION
OF THE SELFREFEENTIAL CLASSES

class
prefix

{
  
 int data;

  
 prefix *next;

public:
       //MEMBER FUCTIONS

DECLARATION
  
 void evaluate(char []);

  
 void push(int );

  
 int pop();

  
 void show();

}*top;

/*FUNCTION
FOR EVALUATION OF PREFIX EXPRESION*/

void
prefix::evaluate(char *exp)

{
int
res;

for(int
i=0;exp[i]!=”;i++)

{
  
 if(isdigit(exp[i]))

  
 {

  
 int item=exp[i]-‘0’;

  
 push(item);

  
 }

  
 else

  
 {

  
 int n1,n2;

  
 n1=pop();

  
 n2=pop();

  
 switch(exp[i])

  
 {

  
 case ‘+’:

  
     res=n1+n2;

  
     break;

  
 case ‘-‘:

  
     res=n2-n1;

  
     break;

  
 case ‘*’:

  
     res=n2*n1;

      
     break;

  
 case ‘$’:

  
     res=pow(n2,n1);

  
     break;

  
 case ‘/’:

  
     res=n2/n1;

  
     break;

  
 default:

  
     cout<<“ENTER
CORRECT EXPRESION”;

  
       
 }

  
 push(res);

  
     }

  
 }

}
//FUNCTION
FOR PUSHING DIGITS

void
prefix::push(int ele)

{
  
 prefix *n,*temp;

  
 n=new(prefix);

  
 n->data=ele;

  
 n->next=NULL;

  
 temp=top;

  
 if(top==NULL)

  
     top=n;

  
 else

  
 {

  
     temp=top;

  
     n->next=temp;

  
     top=n;

  
 }

}

//FUNCTION
FOR POPPING DIGITS

int
prefix::pop()

{
  
 int t;

  
 prefix *temp;

  
 

  
 if(top==NULL)

cout<<“LIST
IS EMPTY:”;

  
 else

  
 {

  
 temp=top;

  
 t=top->data;

  
 top=temp->next;

  
 delete temp;

  
 }

  
 return(t);

}

//FUNCTION
FOR SHOWING OUTPUT

void
prefix::show()

{
  
 int m=pop();

cout<<“AFTER
EVALUATION THE

       
VALUE OF THE EXPRESION  ->”;

  
 cout<<m;

}
void
main()//MAIN()
FUNCTION STARTS

{
  
 char x[22],*z;

  
 top=NULL;

prefix
p;//OBJECT

CREATION
  
cout<<“ENTER THE PREFIX EXPRESION”;

   
cin>>x;

z=strrev(x);//REVERSING OF THE

STRING
  
 p.evaluate(z);

  
 p.show();

}//END OF MAIN()
//END
OF PROGRAM

/*OUTPUT:
ENTER
THE PREFIX EXPRESION+1*23

AFTER
EVALUATION THE

 
VALUE OF THE EXPRESION  ->7

Press
any key to continue

ENTER
THE PREFIX EXPRESION+2-$24*52

AFTER
EVALUATION THE VALUE

    
OF THE EXPRESION  ->-4

Press any key to

continue*/

HOME

CONVERSION_INFIX_TO_POSTFIX

March 27, 2008

INFIX TO POSTFIX CONVERSION

HOME
/*..PROGRAM
FOR CONVERSION OF THE INFIX

EXPRESSION INTO POSTFIX
EXPRESSION..*/

#include<iostream.h>
#include<ctype.h>
#include<math.h>
#include<string.h>
int
j,q,m=0;

char
a,b=NULL;

class
postfix //CLASS
DECLARATION

{
private:
  
 

   
char data,*expr,R[22];

  
 postfix *next;

public:
  
 

  
 void evaluate(char *);

  
 void push(char);

  
     char pop();

  
 void show(int);

  
 int priority(char);

}
*top ;

//FUNCTION
FOR EXPRESSION CONVERSION

void
postfix::evaluate(char *c)

{
expr=c;
  
 

 
for(int i=0,j=-1;expr[i]!=”;i++)

 
{

 
if( isdigit(expr[i])||isalpha(expr[i]) )

  
{

   
R[++j]=expr[i];

  
}

 
else

  
{

   
if(top!=NULL)

   
{

  
 b=pop();

  
 push(b);

   
}

 if(expr[i]==’)’)
  
 push(expr[i]);

else
  
 if(expr[i]=='(‘)

  
 {

  
 R[++j]=pop();

  
 m++;

  
 }

  
 else if(expr[i]=='(‘)

  
 push(expr[i]);

  
 else

  
 {

  
 int n1=priority(b);

  
 int n2=priority(expr[i]);

  
  if(n1<=n2)

  
 push(expr[i]);

  
 else

  
 {

  
  R[++j]=pop();

  
  push(expr[i]);

  
 }

  
 }

  
 }

  
 }

 while(top!=NULL)

 R[++j]=pop();
 
        

}
char
postfix::pop()//FUNCTION
FOR POPPING

{
  
 char x=NULL;

  
 postfix *temp;

  
 temp=top;

  
 x=temp->data;

  
 if(top==NULL)

  
     return(‘0’);

  
 else

  
 {

  
  top=temp->next;

  
  delete temp;

  
 }

  
 return(x);    

}
  
 

//FUNCTION
FOR CHECKING PRIORITY

int
postfix::priority(char y)

{
  
 if(y==’$’)

  
     return(3);

  
 if(y==’*’||y==’/’)

  
     return(2);

  
 if(y==’+’||y==’-‘)

  
 return(1);

  
 else

  
     return(0);

  
 

}

//FUNCTION
FOR PUSHING ELEMENTS

void
postfix::push(char ch)

{
 
postfix *n,*temp;

 
n=new(postfix);

 
n->data=ch;

 
n->next=NULL;

 
if(top==NULL)

  
   top=n;

 
else

 
{

 
temp=top;

 
n->next=temp;

 
top=n;

 
}

 

}
//FUNCTION
FOR DISLAYING OUTPUT

void
postfix::show(int q)

{

   
for(int i=0;i<q-m;i++)

  
 {

  
 if(R[i]=='(‘||R[i]==’)’);

  
 else

  
     cout<<R[i];

  
 }

}

void
main()//MAIN()
FUNCTION STARTS

{
  
 char s[22];

  
 top=NULL;

  
 postfix k;//OBJECT
CREATION

  
 cout<<“enter the INFIX expresion”;

  
 cin>>s;

  
 int q=strlen(s);

  
 k.evaluate(s);

  
 cout<<“THE POSTFIX EXPRESION IS –>”;

   
k.show(q);

}//END OF MAIN()
//END
OF PROGRAM

/*
OUTPUT:

enter
the INFIX expresiona$b*c-d+e/f/(g+h)

THE
POSTFIX EXPRESION IS –>ab$c*d-ef/gh+/+

Press
any key to continue

*/

HOME

CONVERSION_INFIX_TO_PREFIX

March 27, 2008

INFIX TO PREFIX CONVERSION

HOME

/*PROGRAM
FOR THE CONVERSTION FROM

THE INFIX EXPRESSION TO
PREFIX EXPRESSION..*/

#include<iostream.h>
#include<ctype.h>
#include<math.h>
#include<string.h>
int
j,q;

char
a,b=NULL;

class
stack //CLASS
DECLARATION

{
private:
   
char data,*expr,R[22];

  
 stack *next;

public:
  
 

  
 void evaluate(char *);

  
 void push(char);

  
 void pushexp(int); //MEMBER
FUNCTIONS

   
char pop();

  
 void show(int);

  
 int prio(char);

}
*top ;

void
stack::evaluate(char *c)

{
expr=c;
  
 

 
for(int i=0,j=-1;expr[i]!=”;i++)

 
{

 if(
isdigit(expr[i])||isalpha(expr[i]) )

 {
 R[++j]=expr[i];

 }
  
 

 else
  
     

{
  
         

if(top!=NULL)
  
         

{
  
       
   

b=pop();
push(b);
  
       
   

}
  
         

int
n1=prio(b);

int
n2=prio(expr[i]);

if(n1<=n2)
 push(expr[i]);
else
 {
 R[++j]=pop();
 push(expr[i]);
 }
 }
 }
 while(top!=NULL)

 R[++j]=pop();
    
    

}
char
stack::pop()

{
  
 char x=NULL;

  
 stack *temp;

  
 temp=top;

  
 x=temp->data;

  
 if(top==NULL)

  
     return(‘0’);

  
 else

  
 {


  
 top=temp->next;

 
  
 }

  
 return(x);    

}
  
 

int
stack::prio(char y)

{
  
 if(y==’$’)

  
     return(3);

  
 if(y==’*’||y==’/’)

  
     return(2);

  
 if(y==’+’||y==’-‘)

  
 return(1);

  
 else

  
     return(0);

  
 

}

void
stack::push(char ch)

{
 
stack *n,*temp;

 
n=new(stack);

 
n->data=ch;

 
n->next=NULL;

 
if(top==NULL)

  
   top=n;

 
else

 
{

  
   temp=top;

  
   n->next=temp;

  
   top=n;

 
}

 

}
void
stack::show(int q)

{
   
for(int i=q-1;i>=0;i–)

  
 {

  
     cout<<R[i];

  
 }

}

void
main()

{
  
 char s[22];

  
 top=NULL;

  
 stack k;

cout<<“enter
the INFIX expresion\n”;

  
 cin.getline(s,22);

  
 int q=strlen(s);

  
 char *e;

  
 e=strrev(s);

  
 k.evaluate(e);

cout<<“THE
PREFIX EXPRESION IS –>”;

   
k.show(q);

}
//PROGRAM
ENDS

/*
OUTPUT:
enter
the INFIX expresion

A+B*C-D/E
THE
PREFIX EXPRESION IS –>-+A*BC/DE

Press
any key to continue

*/

HOME

STACK_PUSH_POP

March 27, 2008

STACK OPERATION PUSH & POP

HOME
/*..PROGRAM
TO REVERSE THE STRING

 USING
STACK WITH LINKED REPRESENATION

  
AND SELF REFERENTION CLASSES… ..*/


#include<iostream.h>
class
stack //CLASS
DECLARATION

{
private:
  
 char data;

  
 stack *next;

public:
  
 void push(char);

  
 void pop();

}
*top;

//FUNCTION
FOR PUSHING DATA

void
stack::push(char ch)

{
  
 stack *n,*temp;

  
 n=new(stack);

  
 n->data=ch;

  
 n->next=NULL;

  
 if(top==NULL)

  
     top=n;

  
 else

  
 {

  
 temp=top;

  
  n->next=temp;

  
 top=n;

  
 }

}
//FUNCTION
FOR POPPING DATA

void
stack::pop()

{
  
 stack *temp,*t;

  
 temp=top;

  
 while(temp!=NULL)

  
 {

  
 cout<<temp->data<<” “;

  
 t=top;

  
 top=temp->next;

  
 temp=temp->next;

  
 delete t;

  
     

  
 }

}
void
main()//MAIN()
FUNCTION STARTS

{
  
 char x[22];

  
 top=NULL;

  
 stack s;//OBJECT
CREATION

cout<<“ENTER
THE STRING

       
TO BE PUSHED INTO THE STACK\n”;

  
 cin>>x;

  
 for(int i=0;x[i]!=”;i++)

  
     s.push(x[i]);

  
 

cout<<“AFTER
POPPING THE STRING IS\n”;

  
       
 s.pop();

  
 

}//END OF MAIN()

//PROGRAM
ENDS

/*
OUTPUT:

ENTER
THE STRING   BHANU

THE
STRING IN THE REVERSE ORDER

UNAHB
Press
any key to continue*/

HOME

POLYNOMIAL

March 27, 2008

HOME
/*PROGRAM
FOR ADDING,SUBTRACTION,

 
MULTIPLYCATION OF 2 POLINOMIALS*/

#include<iostream.h>
#include<stdlib.h>
int
d,i,y;

static
int z=0;//GLOBAL
VARIABLES

class
poly//CLASS
DECLARATION

{
private:
  
float  coeff;

  
int degree;

  
poly *next;

public:
  
void  getdata(float ,int );

  
void add();

  
void  show(poly *);

  
void list_add(float ,poly *);

  
void sub();

  
void list_sub(float ,poly *);

  
void mul();

  
void list_mul(float,int);

 

}
*head1,*head2,*head3,

 *head,*head4,*head5,*head6;

//FUNCTION
FOR READING DATA

void  
poly::getdata(float c,int d)

{
poly
*n,*temp;

n=new(poly);
n->degree=d;
n->coeff=c;
n->next=NULL;
if(head==NULL)
head=n;
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=n;
}
if(i==0&&z==0)
{
head1=head;
head=NULL;
temp=NULL;
z++;
}
else
{
head2=head;
}
  
     

}
void
poly::add()

//FUNCTION
FOR FINDING THE ADDITION

{
poly
*t1,*t2;

float
x;

t1=head1;
t2=head2;
while(t1!=NULL||t2!=NULL)
{
  
 

if(t1->degree>t2->degree)
{
x=t1->coeff;
list_add(x,t1);
t1=t1->next;
}

else
if(t1->degree<t2->degree)
{
x=t2->coeff;
list_add(x,t2);
t2=t2->next;
}
else
{
x=t1->coeff+t2->coeff;
list_add(x,t1);
t1=t1->next;
t2=t2->next;
}
}
}
void
poly::list_add(float ele,poly *t)

/*FUNCTION
FOR CREATING

 
LINKEDLIST FOR ADDED COEFFICIENTS*/

{
poly
*t3,*n1;

n1=new(poly);
n1->coeff=ele;
n1->degree=t->degree;
n1->next=NULL;
if(head3==NULL)
head3=n1;
  
         

else
{
t3=head3;
while(t3->next!=NULL)
t3=t3->next;
t3->next=n1;
}

}
//FUNCTION
FOR SUBTRACTION

void
poly::sub()

{
poly
*t1,*t2;

float
x;

t1=head1;
t2=head2;
while(t1!=NULL||t2!=NULL)
{
  
 

if(t1->degree>t2->degree)
{
x=t1->coeff;
list_sub(x,t1);
t1=t1->next;
}

else
if(t1->degree<t2->degree)
{
x=t2->coeff;
list_sub(x,t2);
t2=t2->next;
}
else
{
x=t1->coeff-t2->coeff;
list_sub(x,t1);
t1=t1->next;
t2=t2->next;
}
}
}

/*FUNCTION
FOR CREATING LIST  

  
FOR SUBTRACTED COEFFICIENTS*/

void
poly::list_sub(float ele,poly *t)

{  
 

poly
*t3,*n1;

n1=new(poly);
n1->coeff=ele;
n1->degree=t->degree;
n1->next=NULL;
if(head4==NULL)
head4=n1;
  
         

else
{
t3=head4;
while(t3->next!=NULL)
t3=t3->next;
t3->next=n1;
}

}
void
poly::mul()

//FUNCTOIN
FOR MULTIPLICATION

{
float
x;

poly
*t1,*t2;

t1=head1;
t2=head2;
int k;
k=t1->degree+t2->degree;
while(k>=0)
{
x=0;
 while(t1!=NULL)
{
while(t2!=NULL)
{
if((t1->degree+t2->degree)==k)
x=x+t1->coeff*t2->coeff;
t2=t2->next;
}
t1=t1->next;
t2=head2;
}
list_mul(x,k);  
 

 k–;
 t1=head1;
t2=head2;

}
  
     

}
void
poly::list_mul(float x,int k )

/*FUNCTION
FOR CREATION OF

 LIST
FOR MULTIPLIED COEFFICIENTS*/

{  
 

poly
*t,*n;

n=new(poly);
n->coeff=x;
n->next=NULL;
n->degree=k;
if(head5==NULL)
head5=n;
else
{
 t=head5;
 while(t->next!=NULL)
t=t->next;
 t->next=n;
}

}

  
       
     

void
poly::show(poly * head)

//FUNCTION
FOR DISPLAYING OUTPUT

{
poly
*temp=NULL;

if(head==NULL)
cout<<“LIST
EMPTY”;

else
{
temp=head;
while(temp!=NULL)
{
if(temp->coeff>0&&temp->degree>0)
cout<<temp->coeff
   
<<” x^
“<<temp->degree<<” + “;

else
if(temp->degree==0&&temp->coeff<0)
cout<<“(“<<temp->coeff<<“)”;
else
if(temp->degree==0)
cout<<temp->coeff;
else
if(temp->coeff==0);
else
cout<<“\b”<<“(“<<temp->coeff<<“)”
   
<<“x^”<<temp->degree<<“-“;

temp=temp->next;
}
}

}

void
main()//MAIN()
FUNCTION STARTS

{
head1=NULL;
head2=NULL;
head3=NULL;
head4=NULL;
head5=NULL;
head6=NULL;

float
c;

poly
p;//OBJECT CREATION

cout<<“PROGRAM
FOR THE

    
OPERATIONS ON THE POLYNOMIAL”;

cout<<“\n\nENTER
THE DEGREE

      
OF THE first POLYNOMIAL\n”;

cin>>d;
for(i=d;i>=0;i–)
{
  
     

if(i==0)
{
cout<<“\nENTER
THE CONSTANT\t”;

cin>>c;
}
else
{  
 

cout<<“\nENTER
THE

      
COEFFICIENT OF x^”<<i<<“\t”;

cin>>c;
}
p.getdata(c,i);
  
     

}
p.show(head1);
  
 

cout<<“\n\nENTER
THE DEGREE

      
OF THE second POLYNOMIL\n”;

cin>>d;
for(
i=d;i>=0;i–)

{
  
     

if(i==0)
{
cout<<“\nENTER
THE CONSTANT\t”;

cin>>c;
}
else
{
cout<<“\nENTER
THE

     
COEFFICIENT OF x^”<<i<<“\t”;

cin>>c;
}
p.getdata(c,i);
  
     

}
p.show(head2);
p.add();
cout<<“\nAFTER
ADDING \n\n”;

p.show(head3);
p.sub();
cout<<“\nAFTER
SUBTRACTNG \n\n”;

p.show(head4);
cout<<“\nAFTER
MULTIPLICATION \n\n”;

p.mul();
p.show(head5);
}

/*OUTPUT:
case
1:

ENTER
THE DEGREE OF THE first POLYNOMIAL

3

ENTER
THE COEFFICIENT OF x^3    1

ENTER
THE COEFFICIENT OF x^2    2

ENTER
THE COEFFICIENT OF x^1    -0.5

ENTER
THE CONSTANT      5.9

1
x^ 3 + 2 x^ 2 +(-0.5)x^1-5.9

ENTER
THE DEGREE OF THE second POLYNOMIL

2

ENTER
THE COEFFICIENT OF x^2    10.5

ENTER
THE COEFFICIENT OF x^1    7

ENTER
THE CONSTANT      0

10.5
x^ 2 + 7 x^ 1 + 0

AFTER
ADDING

1
x^ 3 + 12.5 x^ 2 + 6.5 x^ 1 + 5.9

AFTER
SUBTRACTNG

1
x^ 3 +(-8.5)x^2(-7.5)x^1-5.9

AFTER
MULTIPLICATION

10.5
x^ 5 + 28 x^ 4 + 8.75 x^ 3

 
+ 58.45 x^ 2 + 41.3 x^ 1 + 0

 

  
 Press any key to continue

*/

HOME

DLL_DELETION_ANYWHERE

March 27, 2008

DOUBLE LINKED LIST DELETION AT ANY WHERE


HOME

/*.PROGRAM
TO IMPLEMENT DELETION ON

 DOUBLE
LINKED LIST USING SELFREFERENTIAL CLASSES.*/


#include<iostream.h>
int 
count=0;

class
dll   //CLASS
DECLARATION

{
private:
  
char data[11];

  
dll *left,*right;//DATA
MEMBERS

public:
  
 void inser_end( char[] );

  
 void del_any();

  
 void show();

}
*head;

//FUNCTION
FOR INSERTING NODE AT THE END

void
dll::inser_end(char s[])

{
  
 dll *n,*temp;

  
 n=new(dll);

  
 for(int i=0;s[i]!=”;i++)

  
 n->data[i]=s[i];

  
 n->data[i]=”;

  
 n->left=NULL;

  
 n->right=NULL;

  
 if(head==NULL)

  
     head=n;

  
 else

  
 {

  
 temp=head;

  
 while(temp->right!=NULL)

  
 {

  
 temp=temp->right;

  
 }

  
 temp->right=n;

  
 n->left=temp;

  
 }

}

  
     

//FUNCTION
FOR DISPLAY OUTPUT

void
dll::show()

{
   
count=0;

  
 dll *temp;

  
 if(head==NULL)

cout<<“NO
NODES IN THE LIST  “;

  
 else

  
 {

  
 temp=head;

  
 while(temp!=NULL)

  
 {

  
 count++;

  
 cout<<temp->data<<endl;

  
 temp=temp->right;

  
 }

  
 }

cout<<“THE
NO OF NODES ->”<<count<<endl;

}

//FUNCTION
FOR DELETING NODE AT ANY POSITION

void
dll::del_any()

{
  
 int pos;

  
 dll *temp,*old,*t;

cout<<“ENTER
THE POSITION OF DELETION”;

  
 cin>>pos;

  
 old=temp=head;

  
 if(pos==1)

  
     

  
 {

  
 temp=temp->right;

  
 t=temp;

  
 head=t;

  
 delete t;

  
 }

  
 

else
{
  
 

  
 for(int i=1;i<pos;i++)

  
 {

  
     old=temp;

  
     temp=temp->right;

  
 }

  
 if(pos==count)

  
 {

  
     temp->left=NULL;

  
     old->right=NULL;

  
     delete temp;

  
 }

  
 else

  
 {

  
 old->right=temp->right;

  
 temp->right->left=old;

   
delete temp;

  
 }

}
}

void 
main()//MAIN()
FUNCTION STARTS

{
  
 char x[22];

  
 int p;

  
 head=NULL;

  
 dll d;//OBJECT CREATION

cout<<“ENTER
THE NO OF STRINGS

     
TO BE INSERTED INTO THE LIST”;

  
 cin>>p;

  
 

cout<<“ENTER
THE “<<p<<”  STRINGS”;

  
 for(int i=0;i<p;i++)

  
 {

  
 cin>>x;

  
 d.inser_end(x);

  
 }

cout<<“AFTER
INSERTING AT

       
THE END THE LIST IS\n”;

  
 d.show();

  
 d.del_any();

cout<<“AFTER
DELETION THE

      
NODES IN THE  LIST ARE\n”;

  
 d.show();

}//END OF MAIN()

//PROGRAM
ENDS

/*
OUTPUT:
 
ENTER
THE NO OF STRINGS

 
TO BE INSERTED INTO THE LIST  4

ENTER
THE 4  STRINGS

BHANU
SURENDRA
SRI
SUKE
AFTER
INSERTING AT

     
THE END THE LIST IS

BHANU
SURENDRA
SRI
SUKE
THE
NO OF NODES ->4

ENTER
THE POSITION OF DELETION4

AFTER
DELETION THE

     
NODES IN THE  LIST ARE

BHANU
SURENDRA
SRI
THE
NO OF NODES ->3

Press
any key to continue

*/

HOME

DLL_INSERTION_AT_ANYWHERE

March 27, 2008

DOUBLE LINKEDLIST INSERTION AT ANY POSITION

HOME
/*..PROGRAM
TO IMPLEMENT INSERSION AT ANYWHERE

 ON
DOUBLE LINKED LIST USING SELFREFERENTIAL CLASSES..*/


#include<iostream.h>
int 
count=0;

class
dll   //CLASS
DECLARATION

{
private:
  
char data[11];

  
dll *left,*right;//DATA
MEMBERS

public:
         //MEMBER FUNCTIONS
  
 void inser_end( char[] );

  
 void inser_beg(char[] );

       
void inser_any(char[]);

  
 void show();

}
*head;

//FUNCTION
FOR INSERTING NODE AT THE END

void
dll::inser_end(char s[])

{
  
 dll *n,*temp;

  
 n=new(dll);

  
 for(int i=0;s[i]!=”;i++)

  
 n->data[i]=s[i];

  
 n->data[i]=”;

  
 n->left=NULL;

  
 n->right=NULL;

  
 if(head==NULL)

  
     head=n;

  
 else

  
 {

  
 temp=head;

  
 while(temp->right!=NULL)

  
 {

  
 temp=temp->right;

  
 }

  
 temp->right=n;

  
 n->left=temp;

  
 }

}

/*FUNCTION
FOR INSERTING NODE
  AT THE BEGGNING*/

void
dll::inser_beg(char s1[ ])

{
  
 dll *n,*temp;

  
 n=new(dll);

  
 for(int i=0;s1[i]!=”;i++)

  
 n->data[i]=s1[i];

  
 n->data[i]=”;

  
 n->right=NULL;

  
 n->left=NULL;

  
 if(head==NULL)

  
     head=n;

  
 else

  
 {

  
 temp=head;

  
 n->right=temp;

  
 temp->left=n;

  
 head=n;

  
 }

}
  
 

//FUNCTION
FOR INSERTING NODE AT ANY POSITION

void
dll::inser_any(char s[ ])

{
  
 int pos;

  
 dll *n,*temp,*old;

  
 n=new(dll);

  
 for(int i=0;s[i]!=”;i++)

  
 n->data[i]=s[i];

  
 n->data[i]=s[i];

  
 n->right=NULL;

  
 n->left=NULL;

  
 cout<<“ENTER THE POSITON

          &nb

sp;     
OF INSERTION”;
  
 cin>>pos;

  
 if(pos>count+1)

  
 cout<<“INSUFFICIENT NODES TO

          &nb

sp;  
INSERT AT “<<pos<<” POSITION\n”;
  
 else

  
 {

   
temp=old=head;

  
 if(pos==1)

  
   inser_beg(s);

  
 else

  
 if(pos==count+1)

  
     inser_end(s);

  
 else

  
 {

  
 for(i=1;i<pos;i++)

  
 {  

  
     old=temp;

  
    temp=temp->right;

  
 }

  
 old->right=n;

  
 temp->left=n;

  
 n->right=temp;

  
 n->left=old;

  
 }

  
 }

}

  
     

//FUNCTION
FOR DISPLAY OUTPUT

void
dll::show()

{
   
count=0;

  
 dll *temp;

  
 if(head==NULL)

  
 cout<<“NO NODES IN THE LIST  “;

  
 else

  
 {

  
 temp=head;

  
 while(temp!=NULL)

  
 {

  
 count++;

  
 cout<<temp->data<<endl;

  
 temp=temp->right;

  
 }

  
 }

cout<<“THE
NO OF NODES ->”<<count<<endl;

}
void 
main()//MAIN()
FUNCTION STARTS

{
  
 char x[22];

  
 head=NULL;

  
 dll d;//OBJECT CREATION

cout<<“THE
STRINGS IN THE LIST ARE\n”;

  
 d.inser_end(“Bhanu”);

  
 d.inser_end(“Surendra”);

  
 d.inser_end(“Durga”);

  
 d.inser_end(“Kartheek”);

  
 d.show();

  
 

cout<<“ENTER
THE STRING TO BE

     
INSERTED AT SPECIED POSITION\n”;

  
 cin>>x;

  
 d.inser_any(x);

  
 d.show();

  
 

}//END OF MAIN()

//PROGRAM
ENDS

/*
OUTPUT:

THE
STRINGS IN THE LIST ARE

Bhanu
Surendra
Durga
Kartheek
THE
NO OF NODES ->4

ENTER
THE STRING TO BE INSERTED

           

  
AT SPECIED POSITION

PHANI
ENTER
THE POSITON OF INSERTION2

Bhanu
PHANI
Surendra
Durga
Kartheek
THE
NO OF NODES ->5

Press
any key to continue

*/

HOME

DLL_INSERTION_AT_REAREND

March 27, 2008

DOUBLE LINKED LIST INSERSION AT END

HOME

/*.PROGRAM
TO IMPLEMENT OPERATIONS
ON DOUBLE LINKED LIST USING SELFREFERENTIAL CLASSES.*/



#include<iostream.h>
int  count=0;
class
dll   //CLASS
DECLARATION

{
private:
   char
data[11];

   dll
*left,*right;//DATA
MEMBERS

public:
   
void inser_end( char[] );

   
void show();

} *head;

//FUNCTION
FOR INSERTING NODE AT THE END

void dll::inser_end(char
s[])

{
   
dll *n,*temp;

   
n=new(dll);

   
for(int i=0;s[i]!=”;i++)

   
n->data[i]=s[i];

   
n->data[i]=”;

   
n->left=NULL;

   
n->right=NULL;

   
if(head==NULL)

   
    head=n;

   
else

   
{

   
temp=head;

   
while(temp->right!=NULL)

   
{

   
temp=temp->right;

   
}

   
temp->right=n;

   
n->left=temp;

   
}

}

//FUNCTION FOR DISPLAY

OUTPUT   
   
void dll::show()
{
   
count=0;

   
dll *temp;

  if(head==NULL)
 
cout<<“NO NODES IN THE LIST  “;

  else
  {
 temp=head;
 while(temp!=NULL)
  {
  count++;
 
cout<<temp->data<<endl;

 
temp=temp->right;

   }
}
cout<<“THE
NO OF NODES ->”<<count<<endl;

}

void  main()//MAIN() FUNCTION

STARTS
{
   
char x[22];

   
int p;

   
head=NULL;

   
dll d;//OBJECT
CREATION

   
cout<<“ENTER THE NO OF STRINGS

          &nbs

p;
TO BE INSERTED AT THE END”;
   
cin>>p;

   

   
cout<<“ENTER THE
“<<p<<”  STRINGS”;

   
for(int i=0;i<p;i++)

   
{

   
cin>>x;

   
d.inser_end(x);

   
}

   
cout<<“AFTER INSERTING AT

          &nbs

p; 
THE END THE LIST IS\n”;

   
d.show();

}//END OF MAIN()

//PROGRAM
ENDS

/* OUTPUT:
ENTER
THE NO OF STRINGS TO

 BE
INSERTED AT THE END 5

ENTER
THE 5  STRINGS

BHANU
PHANISRI
SURENDRA
DBNRAO
BUJJI
AFTER
INSERTING AT THE

     
END THE LIST IS

BHANU
PHANISRI
SURENDRA
DBNRAO
BUJJI
THE
NO OF NODES ->5

Press
any key to continue

*/

HOME