Pages

Showing posts with label POINTERS. Show all posts
Showing posts with label POINTERS. Show all posts

Tuesday, September 18, 2012

C++ PROGRAM TO IMPLEMENT BINARY SEARCH TREE.


Write a C++ program to create a class called BIN_TREE ( Binary tree) with member functions to perform inorder, preorder and postorder traversals. Create a BIN_TREE object and demonstrate the traversals.


#include<conio.h>
#include<stdlib.h>
#include<iostream.h>
struct node
{
  int info;
  struct node *left;
  struct node *right;
};
typedef struct node tree ;
tree *root=NULL;
class BIN
{
  int num;
  tree *p,*prev,*temp;
  public:
  void insert();
  void inorder(tree *);
  void postorder(tree *);
  void preorder(tree *);
  void display();
};
void BIN:: insert()
{
  p=new(tree);
  cout<<"\n Enter  number:";
  cin>>num;
  p->info=num;
  p->left=p->right=NULL;
  if(root==NULL)
  {
    root=p;
    return;
  }
  temp=root;
  while(temp!=NULL)
  {
    if(num>=temp->info)
    {
      prev=temp;
      temp=temp->right;
    }
    else
    {
      prev=temp;
      temp=temp->left;
    }
  }
  if(num>=prev->info)
    prev->right=p;
  else
    prev->left=p;
}
void BIN::preorder(tree *temp)
{
  if(temp!=NULL)
  {
    cout<<" "<<temp->info;
    preorder(temp->left);
    preorder(temp->right);
  }
}
void BIN:: inorder(tree *temp)
{
  if(temp!=NULL)
  {
    inorder(temp->left);
    cout<<" "<<temp->info;
    inorder(temp->right);
  }
}
void  BIN::postorder(tree *temp)
{
  if(temp!=NULL)
  {
    postorder(temp->left);
    postorder(temp->right);
    cout<<" "<<temp->info;
  }
}
void BIN:: display()
{
  if(root==NULL)
  {
    cout<<"\n ***EMPTY TREE**** \n";
    return;
  }
  cout<<"\n\n THE PREORDER DISPLAY IS:   ";
  preorder(root);
  cout<<"\n\n THE INORDER DISPLAY IS:   ";
  inorder(root);
  cout<<"\n\n THE POSTORDER DISPLAY IS:   ";
  postorder(root);
}
void main()
{
  BIN o;
  int ch=1;
  int count=0;
  clrscr();
  while(ch)
  {
    cout<<"\n***********MENU***********";
    cout<<"\n1:INSERT-IN-TREE\n2:DISPLAY\n3.QUIT\n";
    cout<<"\nEnter your choice:\n";
    cin>>ch;
    switch(ch)
    {
      case 1:clrscr();
         count++;
         o.insert();
         break;
      case 2:clrscr();
      cout<<"\n\n THE NUMBER OF NODES IN THE BST is "<< count;
      o.display();
      break;
      case 3:exit(0);
    }
  }
  getch();
}


You might also like:


OUTPUT




Saturday, September 15, 2012

LINKED LIST IMPLEMENTATION IN C++


Write a C++ program to create a class called LIST (linked list) with member functions to insert an element at the front of the list as well as to delete an element from the front of the list.
Demonstrate all the functions after creating a list object.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct NOD
 {
   int info;
   struct NOD *next;
 };
 typedef struct NOD node;

class linklist
  {
    node *f;
    public:
    linklist()
    {
      f=NULL;
    }
    void insert(int);
    void del();
    void disp();
  };
void linklist::insert(int num)
  {
    node *p=new node;
    p->info=num;
    p->next=f;
    f=p;
  }
void linklist::del()
  {
    clrscr();
    node *temp=f;
    if(f==NULL)
    cout<<"\n The list is empty";
    else
    {
      cout<<"\n The deleted element is :"<<f->info;
      f=f->next;
      delete temp;
      cout<<"\n Deletion successful";
    }
    return;
  }
void linklist::disp()
  {
    node *temp=f;
    if(f==NULL)
    cout<<"\n The list is empty";
    else
    {
      cout<<"\n The  element in list are:";
        while(temp!=NULL)
      {
        cout<<" "<<temp->info;
        temp=temp->next;
      }
    }
  }
void main()
{
  int num,ch=1;
  linklist ob;
  clrscr();
  while(ch)
  {
    cout<<"\n\n\n\n************** Linked List ************** \n"
    <<"\n-------- Menu ---------"
    <<"\n Enter 1 to pushed "
    <<"\n Enter 2 to popped "
    <<"\n Enter 3 to display "
    <<"\n Enter 4 to exit "
    <<"\n Enter your choice: ";
    cin>>ch;
    switch(ch)
    {
    case 1:clrscr();
      cout<<"\n Enter the number to be inserted ";
      cin>>num;
      ob.insert(num);
      ob.disp();
      break;
    case 2:clrscr();
      ob.del();
      ob.disp();break;
    case 3:clrscr();
      ob.disp();break;
    case 4:exit(0);
    default : cout<<"\nInvalid choice";
    }
  }
  getch();
}

OUTPUT






Saturday, September 1, 2012

ROW SUM AND COLUMN SUM OF A 2-D MATRIX USING POINTER


C++ program to find the row and column sums of a two-dimensional array of integers using pointers. The underlying representation of the 2-d array must be a single-subscripted array of integers with rows*columns number of elements. Calculate row sum and column sum and display the result along with the array. 

 


#include<iostream.h>

#include<conio.h>

void main()

{

  int r,c,i,j;
  clrscr();
  cout<<"Enter the no. of rows and columns"<<endl;
  cin>>r>>c;
  int *a=new int[r*c];
  int *colsum=new int[c];
  int *rowsum=new int[r];
  cout<<"Enter the array elements\n";
  for(i=0;i<r;i++)
  {
    rowsum[i]=0;
    for(j=0;j<c;j++)
    {
      cin>>a[i*c+j];
      rowsum[i]=rowsum[i]+a[i*c+j];
    }
  }
  for(j=0;j<c;j++)
  {
    colsum[j]=0;
    for(i=0;i<r;i++)
    {
      colsum[j]=colsum[j]+a[i*c+j];
    }
  }
  cout<<"\nThe no.s are\n";
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
      cout<<a[i*c+j]<<"\t";
    }
    cout<<endl;
  }
  cout<<"\nRow sum:\n";
  for(i=0;i<r;i++)
  {
    cout<<"row "<<(i+1)<<": "<<rowsum[i]<<endl;
  }
  for(i=0;i<c;i++)
  {
    cout<<"column "<<(i+1)<<":"<<colsum[i]<<endl;
  }
  getch();
}

OUTPUT