Pages

Showing posts with label DATA STRUCTURE. Show all posts
Showing posts with label DATA STRUCTURE. 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






Wednesday, September 12, 2012

C++ PROGRAM TO IMPLEMENT QUEUE USING ARRAY.


#include<iostream.h>
#include<conio.h>
class queue
{
  public:
  int q[5],front,rear,x,result;
  void enq();
  void dque();
  void disp();
  queue()
  {
    front=0;
    rear=0;
  }
};
void queue::enq()
{
  if(rear>=5)
  cout<<"\nQueue overflow!!\n";
  else
  {
    cout<<"\nEnter the number to be inserted: ";
    cin>>x;
    rear++;
    q[rear]=x;
    cout<<"\nNumber pushed in the queue:"<<q[rear];
  }
}
void queue::dque()
{
  if(rear==0)
  cout<<"\nQueue underflow!!\n";
  else
  {
    if(front==rear)
    {
      front=0;
      rear=0;
    }
    else
      front++;
  }
  cout<<"\nDeleted element is:";
  result=q[front];
  cout<<result;
}
void queue::disp()
{
  if(rear==0)
    cout<<"\nQueue underflow!!\n";
  else
    cout<<"\nContents of queue is:";
  for(int i=front+1;i<=rear;i++)
    cout<<q[i]<<"\t";
}
void main()
{
  int c;
  queue qu;
  clrscr();
//  cout<<"\n*****";
//  cout<<"\nQUEUE";
//  cout<<"\n*****";
  do
  {
    cout<<"\n1.Insertion\n2.Deletion\n3.Display\n";
    cout<<"\nEnter your choice:";
    cin>>c;
    switch(c)
    {
      case 1:
    qu.enq();
    break;
      case 2:
    qu.dque();
    break;
      case 3:
    qu.disp();
    break;
      default:
    cout<<"\nInvalid choice!!\n";
    }
  }
  while(c<4);
  getch();
}


OUTPUT


Thursday, August 23, 2012

STACK IMPLEMENTATION USING OPERATOR OVERLOADING IN C++


Write a C++ program to create a class called STACK using an array of integers. Implement the following operations by overloading the operators + and – 
i) s1=s1+element; where s1 is an object of the class STACK and element is aninteger to be pushed on the top of the stack.
ii) s1=s1-; where s1 is an object of the class STACK - operator pops the element.
Handle the STACK empty and STACK full conditions. Also display the contents of the stack after each operation, by overloading the operator <<


#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
const int SIZE=5; //Stack size
//class declaration
class stack
{
   int items[SIZE];
   int top;
   int full();
   int empty();
   public:
   stack()
   {
      top=-1;
   }
   stack operator--(int);
   friend stack operator+(stack s1,int elem);
   friend ostream &operator<<(ostream &os,stack &s1);
};
// checking for Stack overflow
int stack::full()
{
   if(top==SIZE-1)
      return 1;
   else
      return 0;
}
//Checking for stack under flow.
int stack::empty()
{
   if(top==-1)
      return 1;
   else
      return 0;
}
//function for element deletion from the stack

stack stack::operator--(int )
{
   if(empty())
   {
     cout<<"Stack underflow\n";
   }
   else
   {
       cout<<"\nThe element deleted is :"<<items[top];
       stack t;
       t.top=--top;
       for(int i=0;i<=top;i++)
           t.items[i]=items[i];
   }
   return *this;
}
ostream &operator<<(ostream &os,stack &s1)
{
   for(int i=s1.top;i>=0;i--)
     os<<s1.items[i]<<"\n";
   return os;
}
//function for element insertion on to the stack
stack operator+(stack s1,int elem)
{
   if(s1.full())
     cout<<"\nStack overflow\n";
   else
     s1.items[++(s1.top)]=elem;
   return s1;
}
/*Main function*/
void main()
{
   stack s1;
   int choice,elem;
   clrscr();
   for(;;)
   {
     cout<<"\n1:PUSH 2:POP 3:DISPLAY 4:EXIT\n"
     <<"enter your choice:";
     cin>>choice;
     switch(choice)
     {
       case 1:
           cout<<"Enter the element to be inserted:";
           cin>>elem;
           s1=s1+elem;
           break;
       case 2:
           s1=s1--;
           break;
       case 3:
           cout <<"The contents of the stack are :\n"<<s1;
           break;
       case 4: exit(0);
       default: cout <<"Invalid choice\n";
       getch();
       exit(0);
     }
   }
}

output