Blogger news

Followers

Wednesday, November 13, 2013
Design,Develop and execute a program in C to implement a doubly linked list where each node consists of integers.The program should support the following operations:
 1.Create a doubly linked list by adding each node at the front.
2.Insert a new node to the left of the node whose key value is read as an input.
3.Delete the node of a given data if it’s found, otherwise display appropriate message
4.Display the contents of the list.


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

 typedef struct node *NODE;
 struct node
 {
   int info;
   NODE left,right;
 };

 NODE first;
 int empty();
 void create();
 void insert();
 void delete();
 void display();

 NODE getnode()
 {
   return((struct node*)malloc(sizeof(struct node)));
 }

 int empty()
 {
  return(first==NULL?1:0);
 }

 void create()
 {
  NODE new;
  int ch;
  first=NULL;

  do
  {
  new=getnode();
  printf("\n enter element to be inserted:");
  scanf("%d",&new->info);
  new->left=new->right=NULL;
  if(first==NULL)
  {
    first=new;
  }
  else
  {
    new->right=first;
    first->left=new;
    first=new;
  }
  printf("\n continue?1\0");
  scanf("%d",&ch);
 }
 while(ch!=0);
}

void insert()
{
 NODE new,curr,prev;
 int x,f=0;
 printf("\n enter the key value of a node\n");
 scanf("%d",&x);
 if(x==first->info)
 {
  new=getnode();
  printf("\n enter a value for new node to be inserted:\n");
  scanf("%d",&new->info);
  new->left=new->right=NULL;
  new->right=first;
  first=new;
 }
 else
 {
  curr=first->right;
  while(curr!=NULL)
  {
   if(x==curr->info)
   {
    f=1;
    break;
   }
   curr=curr->right;
 }
 if(f)
 {
  new=getnode();
  printf("\n enter a value to be inserted \n");
  scanf("%d",&new->info);
  new->left=new->right=NULL;
  prev=curr->left;
  new->right=curr;
  curr->left=new;
  prev->right=new;
  new->left=prev;
 }
 else
 {
 printf("\n invalid key value \n");
}
}
}
void delete()
{
 NODE curr,prev;
 int f=0,x;
 printf("\n enter info value of node you want to delete \n");
 scanf("%d",&x);
 if(x==first->info)
 {
  curr=first;
  first=first->right;
  first->left=NULL;
  printf("\n deleted item %d",curr->info);
  free(curr);
 }
 else
 {
  curr=first->right;
  while(curr!=NULL)
  {
   if(x==curr->info)
   {
    f=1;
    break;
  }
  curr=curr->right;
  }
  if(f)
  {
  prev=curr->left;
  prev->right=curr->right;
  curr->right->left=prev;
  free(curr);
 }
 else
 printf("\n not valid node!! not found \n");
}
}


void display()
{
 NODE curr;
 printf("\n elements of the list \n");
 printf("\n NULL<-");
 for(curr=first;curr!=NULL;curr=curr->right)
 {
  printf("%d<-->",curr->info);
 }
 printf("NULL \n");
}
void main()
{
 int ch;
 clrscr();
 printf("\n \n doubly linked list will be created \n");
 create();
 display();
 printf("\n\n doubly linked list will be created \n");
 printf("\t1->insert\n");
 printf("\t2->delete\n");
 printf("\t3->display\n");
 printf("\t4->exit\n");
 while(1)
 {
  printf("enter your choice \n");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
     if(first==NULL)
     {
     printf("empty list \n");
     printf("insert left not possible \n");
   }
   else
   {
   insert();
   display();
   }
   break;
  case 2:
    if(empty())
      printf("\n empty list \n");
    else
    {
     delete();
     if(!empty())
     display();
    }
    break;
    case 3:
    if(empty())
    printf("\n empty list \n");
    else
    {
    display();
    }
    break;
    case 4:
      exit(0);
    default:
    printf("\n invalid choice!!");
  }
 }
  getch();
 }



Program end






Thursday, October 31, 2013
Write a C++ program to create a class called BIN_TREE(Binary Tree) with member function to perform inorder,preorder and postorder traversals.Create a BIN_TREE object and demonstrate the traversals
-----
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int info;
node *llink;
node *rlink;
};
class tree
{
  node *root;
 public:
  tree()
  {
   root=NULL;
   }
   void inorder(node *);
   void preorder(node *);
   void postorder(node *);
   void display(int);
   void create(int);
  };
void tree::display(int choice)
{
  switch(choice)
  {
    case 2:
                if(root==NULL)
                  cout<<"tree is empty"<<endl;
                else
                 {
                   cout<<"preorder traversal is "<<endl;
                   preorder(root);
                 }
                 break;
    case 3:
                if(root==NULL)
                  cout<<"tree is empty"<<endl;
                else
                {
                  cout<<"inorder traversal is"<<endl;
                  inorder(root);
                }
                break;
    case 4:
       if(root==NULL)
                cout<<"tree is empty"<<endl;
       else
       {
                 cout<<"Postorder traversal is"<<endl;
                 postorder(root);
       }
       break;
    }
  }

  void tree::inorder(node *root)
  {
    if(root!=NULL)
    {
      inorder(root->llink);
      cout<<root->info<<endl;
      inorder(root->rlink);
    }
  }

  void tree::preorder(node *root)
  {
    if(root!=NULL)
    {
      cout<<root->info<<endl;
      preorder(root->llink);
      preorder(root->rlink);
    }
  }

  void tree::postorder(node *root)
  {
    if(root!=NULL)
    {
      postorder(root->llink);
      postorder(root->rlink);
      cout<<root->info<<endl;
    }
 }

 void tree::create(int item)
 {
   node *temp,*cur,*prev;
   temp=new node;
   temp->info=item;
   temp->llink=NULL;
   temp->rlink=NULL;

   if(root==NULL)
   {
     root=temp;
     return;
   }
   cur=root;
   prev=NULL;
   while(cur!=NULL)
   {
     prev=cur;
     if(item<cur->info)
       cur=cur->llink;
     else
       cur=cur->rlink;
   }
   if(item<prev->info)
      prev->llink=temp;
   else
      prev->rlink=temp;

 }
 void main()
 {
   tree ob;
   int item,choice;
   clrscr();

     cout<<"1:insert 2:preoder"<<endl;
     cout<<"3:inoder 4:postorder"<<endl;
     cout<<"5: exit \n";
      for(;;)
   {
     cout<<"enter the choice \n";
     cin>>choice;

     switch(choice)
     {
       case 1:
                      cout<<"enter the item";
                      cin>>item;
                      ob.create(item);
                      break;
      case 2:
      case 3:
      case 4:
                     ob.display(choice);
                     break;
      default:exit(0);
   }
 }
}
End programs

<![if !vml]><![endif]><![if !vml]><![endif]>Screenshots:
Saturday, October 26, 2013
//Courtesy : Avinash G, Computer Science Dept.,Srinivas Institute of Technology,Mangalore

----
#include<iostream.h>
#include<conio.h>
#include<process.h>

class list
{
                struct node
                {
                int info;
                struct node *next;
                };
                struct node *plist;
public:
list()
{
plist=NULL;
}
void insert(int);
void del();
void display();
};
void main()
{
 list a;
 int choice,item;
 clrscr();
 while(1)
 {
  cout<<"option are 1.insert 2.delete 3.display 4.exit\n";
  cout<<"enter your choice\n";
  cin>>choice;
                switch(choice)
                {
                case 1: cout<<"enter the element to be inseted\n";
                                cin>>item;
                                a.insert(item);
                                break;
                case 2: a.del();
                                break;
                case 3: a.display();
                                break;
                case 4:exit(0);
                }
  }
 }
void list::insert(int item)
{
                struct node *p;
                p=new (node);
                p->info=item;
                p->next=plist;
                plist=p;
}
void list::del()
{
struct node *t;
int item;
if(plist==NULL)
                cout<<"list is empty\n";
else
 {
                t=plist;
                item=t->info;
                cout<<"item removed is  "<<item<<"\n";
                plist=t->next;
                delete(t);
 }
}
void list::display()
{
struct node *temp;
if(plist==NULL)
                cout<<"list is empty\n";
else
{
                cout<<"list elements are ";
                for(temp=plist;temp!=NULL;temp=temp->next)
                                cout<<temp->info<<"\t";
                cout<<endl;
}
}
Program Ends

Screen shot:

<![if !vml]><![endif]>



Friday, October 25, 2013
//Courtesy : Avinash G, Computer Science Dept.,Srinivas Institute of Technology,Mangalore
(avinashganiga94@gmail.com)

----------

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#define MALLOC(p,n,type)\
p=(type*) malloc(n*sizeof(type));\
if(p==NULL)\
{\
      printf("Insufficient memory");\
      exit(0);\
}
#define COMPARE(x,y) (((x)==(y))?0:((x)>(y))?1:-1)
struct node
{
int coeff;
int expon;
struct node *link;
};
typedef struct node *NODE;
void display(NODE head)
{
      NODE temp;
      if(head->link==head)
      {
            printf("polynomial does not exit\n");
            return;
      }
      temp=head->link;
      while(temp!=head)
      {
            if(temp->coeff<0)
                  printf("%dX^%d",temp->coeff,temp->expon);
            else
                  printf("+%dX^%d",temp->coeff,temp->expon);
            temp=temp->link;
      }
}
NODE attach(int coeff,int expon,NODE head)
{
      NODE temp,cur;
      MALLOC(temp,1,struct node);
      temp->coeff=coeff;
      temp->expon=expon;
      cur=head->link;
      while(cur->link!=head)
      {
            cur=cur->link;
      }
      cur->link=temp;
      temp->link=head;
      return head;
}
NODE read_poly(NODE head)
{
      int i=1,coeff,expon;
      printf("Enter the coefficient -999to end the polynomial\n");
      while(1)
      {
      printf("Enter the %d term \n",i++);
      printf("coeff=");
      scanf("%d",&coeff);
      if(coeff==-999)
      break;
      printf("pow X=");
      scanf("%d",&expon);
      head=attach(coeff,expon,head);
      }
      return head;
}
NODE poly_add(NODE head1,NODE head2,NODE head3)
{
NODE a,b;
int coeff;
a=head1->link;
b=head2->link;
while(a!=head1 && b!=head2)
{
      switch(COMPARE(a->expon,b->expon))
      {
            case 0:coeff=a->coeff+b->coeff;
                  if(coeff!=0)
                        head3=attach(a->coeff,a->expon,head3);
                  a=a->link;
                  b=b->link;
                  break;
            case 1: head3:attach(a->coeff,a->expon,head3);
                  a=a->link;
                  break;
            default: head3=attach(b->coeff,b->expon,head3);
                  b=b->link;
      }
}
while(a!=head1)
{
      head3=attach(a->coeff,a->expon,head3);
      a=a->link;
}
while(b!=head2)
{
      head3=attach(b->coeff,b->expon,head3);
      b=b->link;
}
return head3;
}
void main()
{
clrscr();
NODE head1,head2,head3;
MALLOC(head1,1,struct node);
MALLOC(head3,1,struct node);
MALLOC(head3,1,struct node);
head1->link=head1;
head2->link=head2;
head3->link=head3;
printf("\nEnter the first polynomial\n");
head1=read_poly(head1);
printf("\nEnter the second polynomial\n");
head2=read_poly(head2);
printf("\npolynomial 1:");
display(head1);
printf("\npolynomial 2:");
display(head2);
head3=poly_add(head1,head2,head3);
printf("\npolynomial 3:");
display(head3);
getch();
}

__
Program Ends





Friday, September 27, 2013
Design,develop,and execute a program in C++ to create a class called OCTAL, which has the characteristics of an octal number, Implement the following operations by writing an appropriate constructor and an overloaded operator +.
<![if !supportLists]>1.       <![endif]>OCTAL h=x;where x is an integer
<![if !supportLists]>2.       <![endif]>Int y=h+k; where h is an OCTAL object and k is an integer.
Display the OCTAL result by overloading the operator<<. Also display the values of h and y.

#include<iostream.h>
#include<conio.h>

class octal
{
  int onum;

  public:
                octal(int);
                friend ostream& operator<<(ostream&,octal&);
                int operator+(int);
};

octal::octal(int x)
{

  //decimal to octal conversion
  int base=1,rem;
  onum=0;
  while(x>0)
  {
    rem=x%8;
    x=x/8;
    onum=onum+rem*base;
    base=base*10;
  }
}

ostream& operator<<(ostream& dout,octal& h)
{
  dout<<h.onum;
  return dout;
}

octal::operator+(int k)
{
  //conversion from octal to conversion
  int rem,base=1,dnum=0,y;

  while(onum>0)
  {
   rem=onum%10;
   onum=onum/10;
   dnum=dnum+rem*base;
   base=base*8;
   y=dnum+k;
  }

 return(y);
}

void main()
{

 int x,k,y;
 clrscr();

 cout<<"enter the value of x \n";
 cin>>x;
 octal h=x;
 cout<<"the value of h is"<<h<<endl;
 cout<<"enter the value of k \n";
 cin>>k;
 y=h+k;
 cout<<"value of y="<<y;
 getch();
}
Screenshot:
Thursday, September 26, 2013
Design, develop and execute a program in C to Evaluate a valid postfix expression using stack. Assume that the postfix expression is read as a single line consisting of non-negative single digit operands and binary arithmetic operators. The arithmetic operators are +(add),-(subtract),*(multiply) and / (divide)

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

 char postfix[20];
 int top=-1;
 float value[20],stack[20];
 float eval();

 void push(float);
 float pop();

 void main()
 {
  int i=0;
  float result;
  clrscr();

  printf("enter a valid postfix expression\n");
  scanf("%s",postfix);
  while(postfix[i]!='\0')
  {
    if(isalpha(postfix[i]))
    {
     printf("enter the value of %c\n",postfix[i]);
     scanf("%f",&value[i]);
    }
   i++;
  }
  result=eval();
  printf("the value of the expression is %.2f\n",result);

  getch();
 }

 float eval()
 {
  int i=0;
  float op1,op2,result;

  while(postfix[i]!='\0')
  {
    if(isalpha(postfix[i]))
      push(value[i]);
    else
    {
      op2=pop();
      op1=pop();

      switch(postfix[i])
      {
                case'+':
                       push(op1+op2);
                       break;
                case'-':
                       push(op1-op2);
                       break;
                case'*':
                       push(op1*op2);
                       break;
                case'/':
                       push(op1/op2);
                       break;
      };
    }
   i++;
  }
 result=pop();
 return result;
}

void push(float value)
{
  top=top+1;
  stack[top]=value;
  return;
}


float pop()
{
  return stack[top--];
}
Screenshot: